# Tongs RPM builder image, published as ghcr.io/andre-motta/tongs-rpm-builder
# by .github/workflows/rpm-builder-image.yml and pinned in image.json.
#
# The base is the Fedora 44 image pinned by digest; the workflow resolves the
# current digest and passes it in, so the image never builds from a moving tag.
# packages.txt is generated by build_requires.py from the RPM sources.  The dnf
# repository metadata stays in the image so the rebuild layers start warm.
ARG FEDORA_DIGEST
FROM registry.fedoraproject.org/fedora:44@${FEDORA_DIGEST}

ARG FEDORA_DIGEST
ARG SOURCE_COMMIT

LABEL org.opencontainers.image.title="Tongs RPM builder"
LABEL org.opencontainers.image.description="Fedora 44 with the Tongs and companion RPM build dependencies preinstalled"
LABEL org.opencontainers.image.source="https://github.com/andre-motta/tongs"
LABEL org.opencontainers.image.url="https://github.com/andre-motta/tongs"
LABEL org.opencontainers.image.vendor="Tongs"
LABEL org.opencontainers.image.licenses="MIT"
LABEL org.opencontainers.image.revision="${SOURCE_COMMIT}"
LABEL org.opencontainers.image.base.name="registry.fedoraproject.org/fedora:44"
LABEL org.opencontainers.image.base.digest="${FEDORA_DIGEST}"

COPY packages.txt /usr/share/tongs-rpm-builder/packages.txt

RUN set -euo pipefail \
    && printf '%s\n' "${FEDORA_DIGEST}" | grep -Eqx 'sha256:[0-9a-f]{64}' \
    && printf '%s\n' "${SOURCE_COMMIT}" | grep -Eqx '[0-9a-f]{40}' \
    && grep -v '^#' /usr/share/tongs-rpm-builder/packages.txt >/tmp/packages \
    && xargs --no-run-if-empty --delimiter='\n' --arg-file=/tmp/packages \
        dnf install --assumeyes --setopt=install_weak_deps=False \
    && xargs --delimiter='\n' --arg-file=/tmp/packages \
        rpm --query --whatprovides >/dev/null \
    && rm -f /tmp/packages \
    && dnf clean packages \
    && rpm --query --all --queryformat '%{NAME}|%{EPOCHNUM}|%{VERSION}|%{RELEASE}|%{ARCH}\n' \
        | sort >/usr/share/tongs-rpm-builder/installed-packages.txt
