FROM golang:1.25-alpine AS builder
WORKDIR /build
COPY api/ .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o node-api .

FROM debian:bookworm-slim AS ffmpeg
ARG TARGETARCH
ARG FFMPEG_RELEASE=autobuild-2026-09-30-13-08
ARG FFMPEG_BUILD=ffmpeg-n8.1.3-9-g29e619e767
ARG FFMPEG_SHA256_AMD64=76649c19a7a4615b367251ba4a8adc643f65831278a00e7a0c38398f7f06484b
ARG FFMPEG_SHA256_ARM64=d5b52fcb4f8d86d997bb75f6c4b383ca0be3f4cd9efd94ab3b4f27b22984f91c
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl xz-utils \
    && rm -rf /var/lib/apt/lists/*
RUN set -eu; \
    case "${TARGETARCH:-amd64}" in \
      amd64) platform=linux64; sha="$FFMPEG_SHA256_AMD64" ;; \
      arm64) platform=linuxarm64; sha="$FFMPEG_SHA256_ARM64" ;; \
      *) echo "unsupported architecture ${TARGETARCH}" >&2; exit 1 ;; \
    esac; \
    name="${FFMPEG_BUILD}-${platform}-gpl-shared-8.1"; \
    curl -fsSL -o /tmp/ffmpeg.tar.xz "https://github.com/BtbN/FFmpeg-Builds/releases/download/${FFMPEG_RELEASE}/${name}.tar.xz"; \
    echo "${sha}  /tmp/ffmpeg.tar.xz" | sha256sum -c -; \
    tar -xJf /tmp/ffmpeg.tar.xz -C /tmp; \
    mkdir -p /opt/ffmpeg/bin /opt/ffmpeg/lib; \
    cp "/tmp/${name}/bin/ffmpeg" "/tmp/${name}/bin/ffprobe" /opt/ffmpeg/bin/; \
    cp -a /tmp/${name}/lib/*.so.* /opt/ffmpeg/lib/; \
    cp "/tmp/${name}/LICENSE.txt" /opt/ffmpeg/LICENSE.txt; \
    rm -rf /tmp/ffmpeg.tar.xz "/tmp/${name}"

FROM mcr.microsoft.com/playwright:v1.49.1-noble
LABEL version="0.1.4"

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3-venv \
    xvfb \
    x11vnc \
    socat \
    xdotool \
    adwaita-icon-theme \
    x11-apps \
    x11-xserver-utils \
    imagemagick \
    && rm -rf /var/lib/apt/lists/*

COPY --from=builder /build/node-api /usr/local/bin/node-api
COPY --from=ffmpeg /opt/ffmpeg /opt/ffmpeg
RUN ln -s /opt/ffmpeg/bin/ffmpeg /usr/local/bin/ffmpeg \
    && ln -s /opt/ffmpeg/bin/ffprobe /usr/local/bin/ffprobe \
    && ffmpeg -hide_banner -devices 2>/dev/null | grep -q x11grab \
    && ffmpeg -hide_banner -encoders 2>/dev/null | grep -q libx264 \
    && command -v xsetroot

RUN python3 -m venv /opt/profile && /opt/profile/bin/pip install --no-cache-dir playwright==1.49.1

COPY cursor-theme /usr/share/icons/browserious
RUN for name in left_ptr arrow top_left_arrow left_arrow; do \
      ln -s default "/usr/share/icons/browserious/cursors/$name"; \
    done \
    && update-alternatives --install /usr/share/icons/default/index.theme x-cursor-theme \
      /usr/share/icons/browserious/cursor.theme 200 \
    && update-alternatives --set x-cursor-theme /usr/share/icons/browserious/cursor.theme \
    && grep -qx 'Inherits=browserious' /usr/share/icons/default/index.theme \
    && ! grep -qs 'gtk-cursor-theme-name' /etc/gtk-3.0/settings.ini /etc/gtk-4.0/settings.ini \
    && test -d /usr/share/icons/Adwaita/cursors

COPY profile_archive.py /profile_archive.py
COPY extension_archive.py /extension_archive.py
COPY profile_state.py /profile_state.py
COPY profile_upload.sh /profile_upload.sh
COPY process_supervision.sh /process_supervision.sh
COPY callback_headers.py /callback_headers.py
COPY exit_report.py /exit_report.py
COPY exit_report.sh /exit_report.sh
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENV DISPLAY=:99
ENV RESOLUTION=1920x1080x24

EXPOSE 5900
EXPOSE 8080
EXPOSE 9222

ENTRYPOINT ["/entrypoint.sh"]
