# y5_compositor — Debian 13 (trixie) build/run image.
# Installs the build deps, then clones the prepared self-contained source (bind-mounted at
# /repo via image.sh's `-v`) and compiles the winit binary.
#
# Unlike bookworm (debian-12), trixie carries libdisplay-info-dev in main — no backports
# suite needed, so every dep comes from a single apt-get install.
#
# Debian's packaged rustc may be borderline for edition 2024 (needs >= 1.85), so rust comes
# from rustup for parity with the other apt images; everything else is from apt.
FROM debian:13 AS deps

ENV DEBIAN_FRONTEND=noninteractive
RUN --mount=type=cache,target=/var/cache/apt \
    --mount=type=cache,target=/var/lib/apt \
    apt-get update && apt-get install -y --no-install-recommends \
        build-essential clang libclang-dev pkg-config git curl ca-certificates \
        protobuf-compiler libprotobuf-dev libpam0g-dev \
        libdisplay-info-dev libinput-dev libseat-dev libxkbcommon-dev libpixman-1-dev \
        libsystemd-dev libudev-dev libwayland-dev wayland-protocols \
        libegl-dev libgles-dev libgl-dev libgbm-dev libglvnd-dev libvulkan-dev libdrm-dev \
        libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libavdevice-dev \
        libswscale-dev libswresample-dev \
        libdbus-1-dev libpulse-dev \
        nodejs npm \
        libwebkit2gtk-4.1-dev libsoup-3.0-dev libgtk-3-dev librsvg2-dev libayatana-appindicator3-dev \
        libxcb1-dev libxcb-cursor-dev \
        foot fonts-dejavu-core

# Rust toolchain (stable, edition-2024-capable) via rustup, installed system-wide.
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:$PATH
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
        | sh -s -- -y --no-modify-path --profile minimal --default-toolchain stable

# --- build stage: clone the prepared source + compile winit -----------------------------
FROM deps AS build
ARG PROFILE=debug
# BUNDLE=1 -> build the full install bundle (every shipped component) via prepare.sh, stashed at
# /out (package.tar.gz + SHA256SUMS) — the multiarch-publish CD path. Unset (default) -> the
# dev-loop path: just the nested winit binary at /usr/local/bin/y5_compositor.
ARG BUNDLE=
# Optional release-number stamp written to repo-root VERSION before building so the compositor
# bakes it in at compile time (parity with docs.yml). Empty -> committed VERSION.
ARG VERSION=
WORKDIR /working.directory
# Compile into the host-mounted cache (image.sh: -v <.cache>/<distro>:/y5-target) so a changed
# clone rebuilds incrementally — the compiled dependency graph persists between builds.
ENV Y5_TARGET_DIR=/y5-target
# Skip the workspace lint here: node is present (for the devtool build) so build.sh would run it,
# but ci.yml lints authoritatively — a packaged-node hiccup must not fail a binary build.
ENV Y5_SKIP_LINT=1

# /repo is bind-mounted at build time from the prepared self-contained clone
# (image.sh passes `-v <DIST_SRC>:/repo:ro`). Clone it into the image (offline; no network).
RUN git clone /repo /working.directory

# Stamp the release number (when provided) so the compositor bakes it in at compile time.
RUN if [ -n "$VERSION" ]; then printf '%s\n' "$VERSION" > VERSION; fi

# Two build modes (see BUNDLE): the full install bundle (prepare.sh builds compositor, devtool,
# polkit, MX daemon, xwayland, settings, installer), or the dev-loop winit binary for run.sh.
RUN if [ -n "$BUNDLE" ]; then \
        compositor.installer/prepare.sh --out=/out; \
    else \
        BIN="$(environment/build.sh winit ${PROFILE})" \
        && install -Dm755 "$BIN" /usr/local/bin/y5_compositor; \
    fi
