# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

FROM python:3.12-slim as moz-central

RUN apt-get -qq update && apt-get -qq install -y git

RUN git clone https://github.com/mozilla/gecko-dev.git --depth 1

FROM python:3.12-slim

USER root

ARG RESOLUTION="1920x1080x24"
ARG XARGS=""
ARG FIREFOX_VERSION=""

# Set env
ENV DEBIAN_FRONTEND=noninteractive \
    LC_ALL=C.UTF-8 \
    LANG=C.UTF-8 \
    GECKODRIVER_VERSION=0.34.0 \
    XVFB_RES="${RESOLUTION}" \
    XVFB_ARGS="${XARGS}"

# Install requirements to install tools
RUN dependencies=' \
    bzip2 \
    curl \
    gcc \
    gnupg2 \
    libasound2 \
    libatspi2.0-0 \
    libdbus-glib-1-2 \
    libdrm2 \
    libfontconfig1 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libgbm1 \
    libglib2.0-0 \
    libgtk-3-0 \
    libgconf-2-4 \
    libnss3 \
    libxt6 \
    libx11-xcb1 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrender1 \
    libxext6 \
    libxrandr2 \
    musl-dev \
    wget \
    xauth \
    xvfb \
    xz-utils \
    ' \
    && set -x \
    && apt-get -qq update && apt-get -qq install -y software-properties-common \
    && apt-get -qq update && apt-get -qq install --no-install-recommends -y $dependencies \
    && apt-get -y purge firefox \
    && rm -rf /var/lib/apt/lists/* /var/cache/apt/*

ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Copy all files to the container
COPY . /code

WORKDIR /code

# Download Firefox
ARG FIREFOX_DOWNLOAD_URL=https://download.mozilla.org/?product=firefox${FIREFOX_VERSION}-latest-ssl&lang=en-US&os=linux64
RUN wget --no-verbose -O /tmp/firefox $FIREFOX_DOWNLOAD_URL \
    && rm -rf /opt/firefox \
    && tar -C /opt -xf /tmp/firefox \
    && rm /tmp/firefox \
    && ln -fs /opt/firefox/firefox /usr/bin/firefox

# @hackebrot
ARG GECKODRIVER_DOWNLOAD_URL=https://github.com/mozilla/geckodriver/releases/download/v$GECKODRIVER_VERSION/geckodriver-v$GECKODRIVER_VERSION-linux64.tar.gz
RUN rm -rf /opt/geckodriver \
    && wget --no-verbose -O /tmp/geckodriver.tar.gz $GECKODRIVER_DOWNLOAD_URL \
    && tar -C /opt -zxf /tmp/geckodriver.tar.gz \
    && rm /tmp/geckodriver.tar.gz \
    && mv /opt/geckodriver /opt/geckodriver-$GECKODRIVER_VERSION \
    && chmod 755 /opt/geckodriver-$GECKODRIVER_VERSION \
    && ln -fs /opt/geckodriver-$GECKODRIVER_VERSION /usr/bin/geckodriver

RUN mv /usr/bin/geckodriver /usr/bin/geckodriver2 \
    && mv ./utilities/geckodriver /usr/bin/geckodriver \
    && chmod +x /usr/bin/geckodriver

# Install python deps
RUN pip install poetry \
    && poetry install --no-root

# Download older firefox nightly
RUN FIREFOX_OLD_DOWNLOAD_URL=$(python3 utilities/download_old_firefox.py) \
    && wget -q -O /tmp/firefox_old $FIREFOX_OLD_DOWNLOAD_URL \
    && mkdir utilities/firefox-old-nightly \
    && mkdir utilities/firefox-old-nightly-disable-test \
    && tar -C utilities/firefox-old-nightly -xf /tmp/firefox_old \
    && tar -C utilities/firefox-old-nightly-disable-test -xf /tmp/firefox_old \
    && rm /tmp/firefox_old

# Create profile used for update tests
RUN utilities/firefox-old-nightly/firefox/firefox -CreateProfile -headless "klaatu-profile-old-base /code/utilities/klaatu-profile-old-base"

RUN utilities/firefox-old-nightly-disable-test/firefox/firefox -CreateProfile -headless "klaatu-profile-disable-test /code/utilities/klaatu-profile-disable-test"

RUN firefox -CreateProfile -headless "klaatu-profile-firefox-base /code/utilities/klaatu-profile-firefox-base"

RUN firefox -CreateProfile -headless "klaatu-profile-current-base /code/utilities/klaatu-profile-current-base"

# Copy prefs needed for test
RUN cp utilities/user.js utilities/klaatu-profile-old-base

RUN cp utilities/user.js utilities/klaatu-profile-current-base

RUN cp utilities/user.js utilities/klaatu-profile-disable-test

RUN cp utilities/user.js utilities/klaatu-profile-firefox-base

COPY --from=moz-central gecko-dev/browser/components/search/test/browser/telemetry/ /code/search_files/

RUN mv search_server/* search_files/

RUN sed -i 's/\r$//' xvfb-startup.sh

ENTRYPOINT ["/bin/bash", "xvfb-startup.sh"]
