FROM registry.access.redhat.com/ubi10: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-10/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 10 --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 \
        libusbx \
        dnf \
        rootfiles \
        --releasever 10 --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"
ARG BASE_URL=""
ARG REPO_BASE_URL=""

VOLUME /var/tmp
VOLUME /var/log

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 \
                   zstd qemu-kvm-core virtiofsd \
                   automotive-image-builder \
                   image-builder \
                   fuse-overlayfs && \
    dnf clean all

# Configure the container storage to use fuse-overlayfs
# This is needed for skopeo and bootc images to work properly
RUN cat <<EOF > /etc/containers/storage.conf
[storage]
driver = "overlay"
runroot = "/run/containers/storage"
graphroot = "/var/lib/containers/storage"

[storage.options.overlay]
mount_program = "/usr/bin/fuse-overlayfs"
EOF

# Override the baseurl in the repo file for release builds.
# When REPO_BASE_URL is set, replace the build-time BASE_URL with the release URL
# so the published image points to the correct package repository.
ARG BASE_URL
ARG REPO_BASE_URL
ARG DISTRO
RUN if [ -n "${REPO_BASE_URL}" ] && [ -n "${BASE_URL}" ] && \
      [ -f "/etc/yum.repos.d/${DISTRO}.repo" ]; then \
      sed -i "s|${BASE_URL}|${REPO_BASE_URL}|" "/etc/yum.repos.d/${DISTRO}.repo"; \
    fi

CMD ["/bin/bash"]

RUN rm -rf /var/log/*
