# ---------------------------------------------------------------------------
# Docker image — built from the pre-compiled binaries produced by the
# `build-binaries` CI job (desktop Linux x86_64 + aarch64).  This is the
# image used by the release pipeline: no Rust toolchain, no C compiler, no
# QEMU Rust cross-compile — just `curl` and a static copy.
#
# For a local from-source build instead see `Dockerfile` in this repo.
# ---------------------------------------------------------------------------

ARG TARGETPLATFORM
ARG RELEASE_VERSION

# ── Downloader stage ───────────────────────────────────────────────────────
# Grabs the architecture-appropriate binary from the GitHub Release that
# triggered this build.  Alpine is used purely because it has the smallest
# curl-containing image.
FROM alpine:3.21 AS downloader
ARG TARGETPLATFORM
ARG RELEASE_VERSION

RUN apk add --no-cache curl ca-certificates

WORKDIR /tmp

RUN case "$TARGETPLATFORM" in \
        "linux/amd64") BINARY_NAME="mediaflow-proxy-light-linux-x86_64" ;; \
        "linux/arm64") BINARY_NAME="mediaflow-proxy-light-linux-aarch64" ;; \
        *) echo "Unsupported platform: $TARGETPLATFORM" >&2; exit 1 ;; \
    esac && \
    echo "Downloading $BINARY_NAME for $TARGETPLATFORM (release: ${RELEASE_VERSION})" && \
    curl -fL --retry 3 --retry-delay 2 \
        -o mediaflow-proxy-light \
        "https://github.com/mhdzumair/MediaFlow-Proxy-Light/releases/download/${RELEASE_VERSION}/${BINARY_NAME}" && \
    chmod +x mediaflow-proxy-light

# ── Runtime stage ──────────────────────────────────────────────────────────
# distroless/cc-debian12 provides glibc + libgcc (required by our Rust
# binary, which links libstdc++ via rquest's boring-sys).  No shell, no
# package manager — ~20 MB total.
FROM gcr.io/distroless/cc-debian12

WORKDIR /app

COPY --from=downloader /tmp/mediaflow-proxy-light /app/
COPY config-example.toml /app/config.toml

ENV RUST_LOG=info
ENV CONFIG_PATH=/app/config.toml

EXPOSE 8888

ENTRYPOINT ["/app/mediaflow-proxy-light"]
