FROM registry.access.redhat.com/ubi9:latest as builder

# remove any previous repositories
RUN rm -fv "/etc/yum.repos.d/"*

# disable the subscription manager
RUN dnf --noplugins remove -y -q subscription-manager dnf-plugin-subscription-manager

RUN mkdir -p /mnt/rootfs

ARG BASE_URL="https://autosd.sig.centos.org/AutoSD-9/nightly"
ARG DISTRO="AutoSD"
ARG RELEASE_PKG="centos-release-autosd"
ARG GPG_KEY="RPM-GPG-KEY-centosofficial"

# create the $DISTRO}.repo
RUN cat <<EOF > /etc/yum.repos.d/${DISTRO}.repo
[${DISTRO}]
name=${DISTRO}
baseurl=${BASE_URL}/repos/${DISTRO}/compose/${DISTRO}/\$basearch/os/
#TODO: Enable it back with the automotive packages get signed
gpgcheck=0
enabled=1
skip_if_unavailable=1
EOF

#TODO: Enable it back with the automotive packages get signed
## Install redhat-release and import the GPG key. We can then later install more packages and verify the signatures
#RUN \
#    dnf install --installroot /mnt/rootfs \
#        ${RELEASE_PKG} \
#        --releasever 9 --setopt install_weak_deps=false --nodocs --nogpgcheck -y && \
#    rpm --root=/mnt/rootfs --import /mnt/rootfs/etc/pki/rpm-gpg/${GPG_KEY}

RUN \
    dnf install --installroot /mnt/rootfs --setopt=reposdir=/etc/yum.repos.d/ \
        bash \
        coreutils-single \
        curl-minimal \
        glibc-minimal-langpack \
        langpacks-en \
        libcurl-minimal \
        libusb1 \
        dnf \
        rootfiles \
        --releasever 9 --setopt install_weak_deps=false --nodocs -y && \
    dnf --installroot /mnt/rootfs clean all

# Remove some random help txt files
RUN rm -f /mnt/rootfs/usr/share/gnupg/help*.txt

# We lose presets by removing /usr/lib/systemd,
# but systemd should not be installed anyway (fail if it is).
RUN if rpm --dbpath=/mnt/rootfs/var/lib/rpm/ -q systemd; then echo "systemd installed, exiting" && exit 5; fi
RUN rm -rfv /mnt/rootfs/usr/lib/systemd

# Final pruning
RUN rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/dnf* /mnt/rootfs/var/log/yum.* /mnt/rootfs/var/lib/dnf/ /mnt/rootfs/var/lib/rhsm

# Set install langs macro so that new rpms that get installed will
# only install langs that we limit it to.
RUN echo "%_install_langs C.utf8" > /mnt/rootfs/etc/rpm/macros.image-language-conf && \
    echo "LANG=C.utf8" > /mnt/rootfs/etc/locale.conf

# Double check this is needed
RUN rm -f /mnt/rootfs/etc/sysconfig/network-scripts/ifcfg-*

# Systemd isn't installed so we don't need to mask off units and services, but we still
# remove machine-id on pre generated images
RUN rm -f /mnt/rootfs/etc/machine-id && touch /mnt/rootfs/etc/machine-id && chmod 0444 /mnt/rootfs/etc/machine-id

# Create the /run/lock file
RUN install -d /mnt/rootfs/run/lock -m 0755 -o root -g root

FROM scratch

ENV container oci
ARG DISTRO="AutoSD"

COPY --from=builder /mnt/rootfs/ /
# remove all repositories to keep just the one we want
RUN rm -fv "/etc/yum.repos.d/"*
COPY --from=builder /etc/yum.repos.d/${DISTRO}.repo /etc/yum.repos.d/

# Install the Automotive build environment
RUN dnf install -y --setopt install_weak_deps=false --nodocs \
                   make sudo git jq qemu-img \
                   osbuild osbuild-luks2 osbuild-lvm2 osbuild-ostree \
                   android-tools osbuild-auto \
                   automotive-image-builder && \
    dnf clean all

CMD ["/bin/bash"]

#FIXME: workaround for a missing osbuild runner for RHIVOS
# Remove it after it gets fixed upstream.
RUN sed -i 's/ID="rhivos"/ID="rhel"/' /etc/os-release

RUN rm -rf /var/log/*
