# Use ubuntu:22.04 as the base image
FROM ubuntu:22.04

# Set working directory
WORKDIR /app

# Set timezone and non-interactive frontend for apt
ARG DEBIAN_FRONTEND=noninteractive
ARG TZ=Europe/Minsk
ENV TZ=${TZ} LANG=C.UTF-8 LC_ALL=C.UTF-8

# Install necessary system packages for Playwright, GeoIP, Xvfb, and VNC
RUN apt-get update && apt-get install -y --no-install-recommends \
    # From user example
    vim lsof unzip wget ca-certificates \
    # From existing Dockerfile, kept for completeness
    libgeoip1 \
    dbus-x11 \
    xvfb \
    xserver-common \
    xauth \
    x11-xkb-utils \
    xfonts-base \
    procps \
    libgl1-mesa-dri \
    x11vnc \
    fluxbox \
    libnss3 libnspr4 libdbus-1-3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libpango-1.0-0 libcairo2 libasound2 \
    libgtk-3-0 libx11-xcb1 fonts-liberation tzdata \
    xauth util-linux  x11-xserver-utils \
    && \
    # Configure timezone
    ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata && \
    rm -rf /var/lib/apt/lists/*

# Add build-time argument for VNC password
ARG VNC_PASSWORD="vncpassword"

# Set up VNC password from build argument
RUN mkdir -p /root/.vnc && \
    x11vnc -storepasswd "${VNC_PASSWORD}" /root/.vnc/passwd

# Install Miniconda
RUN wget --no-check-certificate https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda.sh && \
    bash /tmp/miniconda.sh -b -p /opt/conda && \
    rm /tmp/miniconda.sh

ENV PATH="/opt/conda/bin:$PATH"

# Create conda environment and configure it
RUN conda init bash && \
    conda config --set always_yes yes && \
    conda tos accept --override-channels --channel defaults && \
    conda create -n camo python=3.11 -y

# Install Python dependencies in conda environment
COPY requirements.txt .
RUN conda run -n camo pip install --no-cache-dir -r requirements.txt

# Install Playwright browsers for version 1.49
RUN conda run -n camo playwright install --with-deps

# Copy the server script into the image
COPY camoufox_server.py .

# Create directory for extensions and copy them
RUN mkdir /app/extensions
COPY google_sign_in_popup_blocker-1.0.2.xpi /app/extensions/
COPY spoof_timezone-0.3.4.xpi /app/extensions/
COPY youtube_ad_auto_skipper-0.6.0.xpi /app/extensions/

# Expose the default port Camoufox might use (adjust if needed)
# This is informational; the actual port mapping is in docker-compose.
EXPOSE 12345
# Expose VNC port
EXPOSE 5900

# Copy the wrapper script and make it executable
COPY start_camoufox.sh /app/
RUN chmod +x /app/start_camoufox.sh && \
    sed -i 's/\r$//' /app/start_camoufox.sh

# Configure Xvfb resolution via build arguments
ARG RESOLUTION="1920x1080x24"
ENV XVFB_RES="${RESOLUTION}" \
    DISPLAY=":99" \
    XAUTHORITY="/tmp/.Xauth"

# Create Xauth setup (mcookie installed in previous apt-get)
RUN touch /tmp/.Xauth && \
    chmod 644 /tmp/.Xauth && \
    echo "#!/bin/bash" > /init_x11.sh && \
    echo "xauth add \$DISPLAY . \$(mcookie)" >> /init_x11.sh && \
    echo "xhost +local:" >> /init_x11.sh && \
    chmod +x /init_x11.sh

# Proper ENTRYPOINT using shell form
#ENTRYPOINT ["/bin/bash", "-c", "source /init_x11.sh && exec xvfb-run --auto-servernum --server-args \"-screen 0 ${XVFB_RES} ${XVFB_ARGS}\" /app/start_camoufox.sh"]

ENTRYPOINT ["/bin/bash", "-c", "\
    rm -f /tmp/.X99-lock && \
    Xvfb :99 -screen 0 ${XVFB_RES} -ac & \
    export DISPLAY=:99 && \
    sleep 1 && \
    touch /tmp/.Xauth && \
    xauth add :99 . $(mcookie) && \
    xhost +local: && \
    source /init_x11.sh && \
    exec /app/start_camoufox.sh \"$@\"", "camoufox-entrypoint"]
