# syntax=docker/dockerfile:1.7
#
# Multi-arch build for `prop-grid-rs`. wgrib2 isn't in Debian apt — the
# Elixir image builds it from source in its own stage. Rather than
# rebuild it here (~5 min per CI run), copy the binary + libg2c out of
# the already-published Elixir image.
#
# Runtime uid 65534 matches the fsGroup on the NFS scores mount.
ARG WGRIB2_IMAGE=git.mcintire.me/graham/prop:latest

FROM ${WGRIB2_IMAGE} AS wgrib2-src

FROM rust:1.94-trixie AS builder
WORKDIR /src

# Cache deps: copy manifests first so dependency changes alone don't
# invalidate the source-layer cache.
COPY Cargo.toml Cargo.lock* ./
RUN mkdir -p src src/bin \
 && echo 'fn main() {}' > src/bin/worker.rs \
 && echo '' > src/lib.rs \
 && cargo build --release --bin worker \
 && rm -rf src

COPY src ./src
COPY tests ./tests

# Lint + test gate the image build — a regressed scorer or a clippy
# warning never produces a pushable image.
RUN rustup component add clippy \
 && cargo clippy --all-targets -- -D warnings \
 && cargo test --release \
 && cargo build --release --bin worker

# Runtime image: slim debian + the shared libs wgrib2 links against.
# libgfortran5/libaec0/zlib1g/libpng16-16t64/libopenjp2-7 mirror the
# Elixir runtime stage.
FROM debian:trixie-slim AS runtime
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates \
      tini \
      libgfortran5 \
      libaec0 \
      zlib1g \
      libpng16-16t64 \
      libopenjp2-7 \
 && rm -rf /var/lib/apt/lists/*

COPY --from=wgrib2-src /usr/local/bin/wgrib2 /usr/local/bin/wgrib2
COPY --from=wgrib2-src /usr/local/lib/libg2c.so* /usr/local/lib/
RUN ldconfig

COPY --from=builder /src/target/release/worker /usr/local/bin/prop-grid-rs

USER 65534:65534
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/prop-grid-rs"]
