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 coreutils createrepo_c curl file findutils \
  gcc gcc-c++ git gnupg2 gzip make openssl openssl-devel pkgconf-pkg-config python3 python3.11 \
  rpm rsync shadow-utils tar unzip xz bzip2 zstd \
  && 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 \
  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}"; \
  rm -f /tmp/rustup-init.sh; \
  rustc --version; \
  cargo --version

ENV DATA_DIR=/var/lib/rustbox-package-builder \
  CARGO_HOME=/var/lib/rustbox-package-builder/cargo \
  CARGO_TARGET_DIR=/var/lib/rustbox-package-builder/cargo/target

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

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