# FFAStrans-on-Fargate runtime image: Ubuntu + Wine, running FFAStrans's real
# Win32 binaries (rest_service.exe/FFAStrans_Queuer.exe) directly rather than via
# a Windows container. Replaced an earlier Windows Server Core-based image (see
# wine_handoff.md for the feasibility investigation and change_to_wine.md for the
# migration itself, both still in this repo as historical record) -- Linux
# Fargate's much faster cold start (~2.9s container-start-to-listening measured
# here vs. ~4-5 min image pull + provisioning on Windows) was the entire
# motivation. Validated end-to-end with a real job run through job-runner.ps1
# against real SQS/S3/DynamoDB (see wine_handoff.md's 2026-09-17 milestone).
FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive

# WineHQ's own repo, not Ubuntu's bundled wine package -- matches the WSL setup in
# wine_handoff.md. Wine 10.0 specifically: 11.0 has a confirmed RtlVirtualUnwind2
# regression that crashes these AutoIt-compiled binaries (see wine_handoff.md
# Finding 1) -- 8.0-1.el9 was also ruled out for a different reason on Rocky 9.
# xvfb: bare Docker containers have no display server at all, unlike WSL(g), which
# auto-supplies one -- confirmed live: without it, rest_service.exe/Queuer.exe both
# hit "nodrv_CreateWindow ... no driver could be loaded" / "Could not create tray
# window" and exit immediately, never reaching the listening state.
RUN dpkg --add-architecture i386 && \
    apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates curl gpg unzip iproute2 xvfb apt-transport-https software-properties-common && \
    mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://dl.winehq.org/wine-builds/winehq.key -o /etc/apt/keyrings/winehq-archive.key && \
    curl -fsSL https://dl.winehq.org/wine-builds/ubuntu/dists/noble/winehq-noble.sources -o /etc/apt/sources.list.d/winehq-noble.sources && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
      winehq-stable=10.0.0.0~noble-1 wine-stable=10.0.0.0~noble-1 \
      wine-stable-amd64=10.0.0.0~noble-1 wine-stable-i386=10.0.0.0~noble-1 && \
    rm -rf /var/lib/apt/lists/*

# Native Linux AWS CLI v2 -- job-runner.ps1's own SQS/S3/DynamoDB calls run as a
# native Linux process under pwsh, never through Wine. This is a SEPARATE install
# from the Windows AWS CLI v2 below (that one runs under Wine, for the workflow's
# own internal `aws s3 cp` shell-out) -- don't conflate the two.
RUN curl -sL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o /tmp/awscliv2.zip && \
    unzip -q /tmp/awscliv2.zip -d /tmp && \
    /tmp/aws/install && \
    rm -rf /tmp/awscliv2.zip /tmp/aws

# PowerShell Core -- job-runner.ps1 stays PowerShell (cross-platform in PS7,
# no rewrite needed); pwsh runs natively on Linux, this is not Wine-related.
# Microsoft's own apt repo, not a Wine/AutoIt concern.
RUN curl -sL -o /tmp/packages-microsoft-prod.deb https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb && \
    dpkg -i /tmp/packages-microsoft-prod.deb && \
    rm -f /tmp/packages-microsoft-prod.deb && \
    apt-get update && \
    apt-get install -y --no-install-recommends powershell && \
    rm -rf /var/lib/apt/lists/*

# FFAStrans (free/no license required) -- no installer, a portable zip.
#
# 32-bit binaries are dead weight: the binaries under test are confirmed 64-bit-only
# (wine_handoff.md), and this image's Wine install only targets win64 (WINEARCH
# below) -- an x86 prefix/process is never used. Deleted in this SAME layer as the
# extraction (not a separate RUN) -- a delete in a later Docker layer only adds a
# whiteout marker, it doesn't shrink the image; the bytes stay committed in the
# earlier layer regardless. Trims ~160MB+ across ffmpeg/x86 and every
# avs_plugins/*/x86 directory.
RUN curl -sL -o /tmp/ffastrans.zip https://github.com/steipal/FFAStrans-Public/archive/refs/heads/1.4.2.zip && \
    unzip -q /tmp/ffastrans.zip -d /tmp/ffastrans-extracted && \
    mv /tmp/ffastrans-extracted/* /opt/FFAStrans && \
    rm -rf /tmp/ffastrans.zip /tmp/ffastrans-extracted && \
    rm -rf /opt/FFAStrans/Processors/ffmpeg/x86 && \
    find /opt/FFAStrans/Processors/avs_plugins -maxdepth 2 -iname x86 -type d -exec rm -rf {} +

# AVICAP32.dll/MSVFW32.dll: ffmpeg/ffprobe's avdevice-*.dll import these
# unconditionally, even for e.g. `-version` -- not yet confirmed necessary under
# Wine (Wine ships its own stub avicap32/msvfw32 and testing so far worked without
# these being placed explicitly), but bundled anyway for parity/safety. x64 only --
# the x86 ffmpeg build was deleted above, never used by this win64-only Wine prefix.
COPY redist/AVICAP32.dll redist/MSVFW32.dll /opt/FFAStrans/Processors/ffmpeg/x64/

# python_portable + workflow_helpers -- the workflow's cmd_run node shells out to
# this bundled portable Python to build the actual ffmpeg/BMX encode command
# (gpu_encoding_cmd.py). Not part of the public FFAStrans-Public distribution or
# the tracked FFAStrans source checkout; separate internal tooling with nowhere
# else to fetch it from, so it's copied in from this repo's tools/.
COPY tools /opt/FFAStrans/tools

# Pre-seed the ffastrans.json first-launch marker only (avoids the blocking
# first-run welcome MsgBox, see job-runner.ps1). The actual per-host
# web_enable/local_processing config file is NOT written here: job-runner.ps1
# writes it itself at container start, since the hostname isn't known at build
# time (each container/task gets a fresh one).
RUN mkdir -p /opt/FFAStrans/Processors/db/configs/hosts && \
    echo '{}' > /opt/FFAStrans/Processors/db/configs/ffastrans.json

# Bake an already-initialized Wine prefix into the image so containers never pay
# the ~17-19s cold `wineboot --init` cost at task-launch time (measured in
# wine_handoff.md) -- this is the actual answer to "can wineboot --init be
# optimized": do it once at image build time, not per-container-launch.
#
# The Windows AWS CLI v2 install (for the workflow's own internal `aws s3 cp`
# shell-out under Wine -- a separate install from the native Linux AWS CLI above,
# which job-runner.ps1 itself uses) is baked into this SAME prefix, installed via
# Wine's own bundled msiexec under the same Xvfb display used for
# wineboot --init. Confirmed working (`wine aws.exe --version` resolves via
# WINEPATH with no full path needed). WINEPATH is set before wineboot --init so
# the installed aws.exe's directory is baked into the prefix's own Windows PATH
# registry.
ENV WINEARCH=win64
ENV WINEPREFIX=/root/.wine64
ENV WINEDLLOVERRIDES="mscoree=d;mshtml=d"
ENV WINEPATH="C:\\Program Files\\Amazon\\AWSCLIV2"
RUN curl -fsSL -o /tmp/AWSCLIV2.msi https://awscli.amazonaws.com/AWSCLIV2.msi && \
    Xvfb :99 -screen 0 1024x768x16 & \
    XVFB_PID=$! && \
    export DISPLAY=:99 && \
    sleep 2 && \
    wineboot --init && \
    wine msiexec /i /tmp/AWSCLIV2.msi /qn /norestart && \
    wineserver -k && \
    sleep 1 && \
    kill $XVFB_PID && \
    rm -f /tmp/.X99-lock /tmp/.X11-unix/X99 /tmp/AWSCLIV2.msi

# bootstrap.sh itself IS baked in (rarely changes) -- job-runner.ps1 is fetched
# from S3 at container startup instead, so changes to actual job logic don't
# require an image rebuild/push.
COPY bootstrap.sh /opt/FFAStrans/bootstrap.sh
RUN chmod +x /opt/FFAStrans/bootstrap.sh

WORKDIR /opt/FFAStrans/Processors
ENTRYPOINT ["/opt/FFAStrans/bootstrap.sh"]
