FROM rockylinux:9

ARG RUST_TOOLCHAIN=stable
ARG NFPM_VERSION=2.47.0
ARG NFPM_RPM_SHA256=5ad3287a0b1d0c068e556bd84965a9866ab6451ef861506a3c09b8971ca5326f
ARG APTLY_VERSION=1.6.3
ARG APTLY_ZIP_SHA256=535ed24eb706bc25a316b59d709a7e74e10165f2ffab8552097ba7e4816a3e7e

RUN dnf -y install --allowerasing \
  bash binutils ca-certificates cmake coreutils createrepo_c curl findutils \
  gcc gcc-c++ git gnupg2 gzip make openssl pkgconf-pkg-config python3 python3.11 \
  rpm rsync shadow-utils sqlite-devel tar unzip xz \
  && dnf clean all \
  && rm -rf /var/cache/dnf

RUN set -eux; \
  curl -fsSL -o /tmp/nfpm.rpm \
  "https://github.com/goreleaser/nfpm/releases/download/v${NFPM_VERSION}/nfpm-${NFPM_VERSION}-1.x86_64.rpm"; \
  echo "${NFPM_RPM_SHA256}  /tmp/nfpm.rpm" | sha256sum -c -; \
  rpm -Uvh /tmp/nfpm.rpm; \
  rm -f /tmp/nfpm.rpm; \
  nfpm --version

RUN set -eux; \
  curl -fsSL -o /tmp/aptly.zip \
  "https://github.com/aptly-dev/aptly/releases/download/v${APTLY_VERSION}/aptly_${APTLY_VERSION}_linux_amd64.zip"; \
  echo "${APTLY_ZIP_SHA256}  /tmp/aptly.zip" | sha256sum -c -; \
  mkdir -p /tmp/aptly; \
  unzip -q /tmp/aptly.zip -d /tmp/aptly; \
  install -m 0755 "$(find /tmp/aptly -type f -name aptly | head -n1)" /usr/local/bin/aptly; \
  rm -rf /tmp/aptly /tmp/aptly.zip; \
  aptly version

ENV RUSTUP_HOME=/opt/rustup \
  CARGO_HOME=/opt/cargo \
  RUST_PROXY_PYTHON=/usr/bin/python3.11 \
  PATH=/opt/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

RUN set -eux; \
  curl --proto '=https' --tlsv1.2 -fsSL https://sh.rustup.rs -o /tmp/rustup-init.sh; \
  sh /tmp/rustup-init.sh -y --no-modify-path --profile minimal --default-toolchain "${RUST_TOOLCHAIN}" --component rustfmt --component clippy; \
  rm -f /tmp/rustup-init.sh; \
  rustc --version; \
  cargo --version

# Registry/git/target cache is persistent, while rustup/toolchains stay in the image.
ENV CARGO_HOME=/var/lib/package-builder/cargo

COPY scripts/ /opt/rust-package-builder/scripts/
RUN chmod 0755 /opt/rust-package-builder/scripts/*.sh

VOLUME ["/var/lib/package-builder"]
ENTRYPOINT ["/opt/rust-package-builder/scripts/entrypoint.sh"]
