# Gitea act runner image for meatshell.
#
# This is still a Linux container. It can build the Windows GNU target
# (`x86_64-pc-windows-gnu`) with MinGW, and can run the resulting .exe through
# Wine for simple smoke tests. It is not a replacement for a real Windows/MSVC
# runner (`x86_64-pc-windows-msvc`).

FROM ghcr.io/catthehacker/ubuntu:full-latest

USER root
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ENV DEBIAN_FRONTEND=noninteractive

# Use Tsinghua Ubuntu mirrors, matching the base image codename instead of
# hard-coding noble.
RUN . /etc/os-release \
    && { \
        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME} main restricted universe multiverse"; \
        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-updates main restricted universe multiverse"; \
        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-backports main restricted universe multiverse"; \
        echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-security main restricted universe multiverse"; \
    } > /etc/apt/sources.list

# Build tools:
# - Linux GUI/dev libs match the official release workflow's Linux build deps.
# - MinGW provides the Windows GNU linker, archiver and windres.
# - Wine + Xvfb let cargo run/test Windows binaries via a virtual display.
RUN dpkg --add-architecture i386 \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        git \
        build-essential \
        pkg-config \
        cmake \
        clang \
        lld \
        libfontconfig1-dev \
        libfreetype6-dev \
        libxcb1-dev \
        libxcb-render0-dev \
        libxcb-shape0-dev \
        libxcb-xfixes0-dev \
        libxkbcommon-dev \
        libxkbcommon-x11-dev \
        libwayland-dev \
        libgl1-mesa-dev \
        libegl1-mesa-dev \
        libgtk-3-dev \
        libudev-dev \
        mingw-w64 \
        gcc-mingw-w64-x86-64 \
        g++-mingw-w64-x86-64 \
        binutils-mingw-w64-x86-64 \
        xvfb \
        x11-utils \
        x11-xserver-utils \
        wine64 \
        wine32 \
        wine \
        wine32:i386 \
        fonts-wine \
        p7zip-full \
        zip \
        unzip \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Rust toolchain. Use rustup so the Windows target can be installed cleanly.
ENV RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup
ENV RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup
ENV RUSTUP_HOME=/root/.rustup
ENV CARGO_HOME=/root/.cargo
ENV PATH=/root/.cargo/bin:${PATH}

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
        | sh -s -- -y --profile minimal --default-toolchain stable \
    && rustup target add x86_64-pc-windows-gnu x86_64-unknown-linux-gnu \
    && rustc --version \
    && cargo --version

# Cross-compilation configuration for cargo and build scripts.
ENV CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc
ENV CARGO_TARGET_X86_64_PC_WINDOWS_GNU_AR=x86_64-w64-mingw32-ar
ENV CC_x86_64_pc_windows_gnu=x86_64-w64-mingw32-gcc
ENV CXX_x86_64_pc_windows_gnu=x86_64-w64-mingw32-g++
ENV AR_x86_64_pc_windows_gnu=x86_64-w64-mingw32-ar
ENV WINDRES=x86_64-w64-mingw32-windres
ENV PKG_CONFIG_ALLOW_CROSS=1

# Wine runtime setup for optional smoke tests.
ENV DISPLAY=:99
ENV WINEDLLOVERRIDES=winemenubuilder.exe=d
ENV WINEDEBUG=-all
ENV CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER=/usr/local/bin/wine-xvfb

RUN wine --version \
    && xvfb-run -a wineboot -u \
    && printf '%s\n' '#!/usr/bin/env bash' 'exec xvfb-run -a wine "$@"' \
        > /usr/local/bin/wine-xvfb \
    && chmod +x /usr/local/bin/wine-xvfb \
    && printf '%s\n' \
        '#!/usr/bin/env bash' \
        'set -euo pipefail' \
        'cargo build --release --target x86_64-pc-windows-gnu "$@"' \
        > /usr/local/bin/build-win-gnu \
    && chmod +x /usr/local/bin/build-win-gnu

WORKDIR /workspace
