ARG NAMESPACE=selenium
ARG VERSION=latest
ARG BASE=node-base
FROM --platform=linux/amd64 ${NAMESPACE}/${BASE}:${VERSION}
ARG AUTHORS
LABEL authors=${AUTHORS}

USER root

#============================================
# Microsoft Edge
#============================================
# can specify versions by EDGE_VERSION;
#  e.g. microsoft-edge-beta=88.0.692.0-1
#============================================
ARG EDGE_VERSION="microsoft-edge-stable"
# Microsoft prunes old versions from packages.microsoft.com, so version-specific
# installs are pulled from the per-version archive (mirrors the Google Chrome
# approach with NDViet/google-chrome-stable). Latest still comes from the repo.
ARG EDGE_ARCHIVE_SITE="https://github.com/NDViet/microsoft-edge-stable/releases/download"
RUN wget -q -O - https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.gpg >/dev/null \
  && echo "deb https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge.list \
  && apt-get update -qqy \
  && if echo "${EDGE_VERSION}" | grep -qE "microsoft-edge-stable[_|=][0-9]*"; \
    then \
      EDGE_VERSION_NUMBER=$(echo "$EDGE_VERSION" | cut -d'=' -f2) \
      && EDGE_VERSION=$(echo "$EDGE_VERSION" | tr '=' '_') \
      && wget -qO microsoft-edge.deb "${EDGE_ARCHIVE_SITE}/${EDGE_VERSION_NUMBER}/${EDGE_VERSION}_$(dpkg --print-architecture).deb" \
      && apt-get -qqy --no-install-recommends install --allow-downgrades ./microsoft-edge.deb \
      && rm -rf microsoft-edge.deb ; \
    else \
      apt-get -qqy --no-install-recommends install ${EDGE_VERSION} ; \
    fi \
  && rm -rf /var/lib/apt/lists/* /var/cache/apt/*

#=================================
# Edge Launch Script Wrapper
#=================================
COPY wrap_edge_binary /opt/bin/wrap_edge_binary
RUN /opt/bin/wrap_edge_binary

#============================================
# Edge webdriver
#============================================
# can specify versions by EDGE_DRIVER_VERSION
# Latest released version will be used by default. Microsoft prunes old
# msedgedriver builds (and the LATEST_RELEASE_<major> pointer), so for pruned
# versions the exact browser version is used and the download falls back to the
# per-version archive (NDViet/microsoft-edge-stable), mirroring the browser deb.
#============================================
ARG EDGE_DRIVER_VERSION
RUN DRIVER_ARCH=$(if [ "$(dpkg --print-architecture)" = "amd64" ]; then echo "linux64"; else echo "linux-aarch64"; fi) \
  && if [ -z "$EDGE_DRIVER_VERSION" ]; \
  then EDGE_MAJOR_VERSION=$(microsoft-edge --version | sed -E "s/.* ([0-9]+)(\.[0-9]+){3}.*/\1/") \
    && EDGE_DRIVER_VERSION=$(wget --no-verbose -O - "https://msedgedriver.microsoft.com/LATEST_RELEASE_${EDGE_MAJOR_VERSION}_LINUX" | tr -cd "\11\12\15\40-\176" | tr -d "\r" || true) ; \
    if [ -z "$EDGE_DRIVER_VERSION" ]; \
    then EDGE_DRIVER_VERSION=$(microsoft-edge --version | sed -E "s/.* ([0-9]+(\.[0-9]+){3}).*/\1/") ; \
    fi ; \
  fi \
  && echo "Using msedgedriver version: "$EDGE_DRIVER_VERSION \
  && ( wget --no-verbose -O /tmp/msedgedriver_${DRIVER_ARCH}.zip "https://msedgedriver.microsoft.com/$EDGE_DRIVER_VERSION/edgedriver_${DRIVER_ARCH}.zip" \
    || wget --no-verbose -O /tmp/msedgedriver_${DRIVER_ARCH}.zip "${EDGE_ARCHIVE_SITE}/${EDGE_DRIVER_VERSION}-1/edgedriver_${DRIVER_ARCH}.zip" ) \
  && rm -rf /opt/selenium/msedgedriver \
  && unzip /tmp/msedgedriver_${DRIVER_ARCH}.zip -d /opt/selenium \
  && rm /tmp/msedgedriver_${DRIVER_ARCH}.zip \
  && mv /opt/selenium/msedgedriver /opt/selenium/msedgedriver-$EDGE_DRIVER_VERSION \
  && chmod 755 /opt/selenium/msedgedriver-$EDGE_DRIVER_VERSION \
  && ln -fs /opt/selenium/msedgedriver-$EDGE_DRIVER_VERSION /usr/bin/msedgedriver

#============================================
# Edge cleanup script and supervisord file
#============================================
COPY edge-cleanup.sh /opt/bin/edge-cleanup.sh
COPY edge-cleanup.conf /etc/supervisor/conf.d/edge-cleanup.conf
COPY fluxbox-menu-browser /etc/X11/fluxbox/

USER ${SEL_UID}

#============================================
# Dumping Browser information for config
#============================================
RUN mkdir -p /opt/selenium/browsers/edge \
    && echo "MicrosoftEdge" > /opt/selenium/browsers/edge/name \
    && microsoft-edge --version | awk '{print $3}' > /opt/selenium/browsers/edge/version \
    && echo '{"ms:edgeOptions": {"binary": "${SE_BROWSER_BINARY_LOCATION:-/usr/bin/microsoft-edge}"}}' > /opt/selenium/browsers/edge/binary_location

ENV SE_OTEL_SERVICE_NAME="selenium-node-edge" \
    SE_NODE_ENABLE_MANAGED_DOWNLOADS="true"