# Installer-environment container for bootc-image-builder's `bootc-installer`
# image type. This is NOT the quine-linux OS -- it's the throwaway Anaconda
# environment the ISO boots into, which then installs the real quine-linux
# bootc image (passed separately via --installer-payload-ref). See
# docs/patched-anaconda-installer.md for the full workflow this fits into.

# Builds the patched anaconda-webui from the vendored source in
# anaconda-webui/vendor/ (see anaconda-webui/PROVENANCE.md) with upstream's
# own `make install`, i.e. exactly the files its RPM would ship. Runs on the
# build host's native platform: the output is platform-independent (web
# assets and scripts), and emulated builds of this stage are both slow and
# unreliable (tar fails under emulation on Apple Silicon).
FROM --platform=$BUILDPLATFORM registry.fedoraproject.org/fedora:rawhide AS anaconda-webui

# Upstream version the vendored source corresponds to, and the gitlink its
# node_modules submodule pinned (the npm package cache, fetched at build).
# Update both when re-vendoring.
ARG ANACONDA_WEBUI_VERSION=86
ARG NODE_MODULES_REF=99a26de1065b0c39bad3d11197a37e1a8b0edb99

RUN dnf install -y git make npm diffutils gettext rpm-build && dnf clean all

COPY anaconda-webui/vendor/ /src/
WORKDIR /src

# The upstream build assumes a git checkout: tools/node-modules reads the
# node_modules gitlink from the index, and the Makefile fetches cockpit's
# shared lib into the repo's object store. Recreate just enough of one.
RUN set -eux; \
    git init -q; \
    git add -A; \
    git update-index --add --cacheinfo "160000,${NODE_MODULES_REF},node_modules"; \
    git -c user.name=build -c user.email=build@localhost commit -q -m vendored

RUN set -eux; \
    make package-lock.json; \
    # node_modules ships only x86_64's esbuild binary; add the host's if needed.
    v="$(node -p 'require("./node_modules/esbuild/package.json").version')"; \
    case "$(uname -m)" in \
      aarch64) npm install --no-save --ignore-scripts "@esbuild/linux-arm64@${v}" ;; \
    esac; \
    make install DESTDIR=/out VERSION="${ANACONDA_WEBUI_VERSION}"

FROM quay.io/fedora/fedora-bootc:rawhide

# Stock Fedora anaconda-webui pulls in the right dependencies; the patched
# build is laid over it below.
RUN set -eux; \
    dnf install -y \
      anaconda-webui \
      anaconda \
      anaconda-install-env-deps \
      anaconda-dracut \
      dracut-config-generic \
      dracut-network \
      net-tools \
      squashfs-tools \
      grub2-efi-x64-cdboot \
      python3-mako \
      lorax-templates-generic \
      biosdevname \
      prefixdevname; \
    dnf clean all

# Replace the packaged web UI wholesale so no stale upstream assets linger.
RUN rm -rf /usr/share/cockpit/anaconda-webui
COPY --from=anaconda-webui /out/ /

# Fedora 43+ path fixup -- image-builder expects the EFI directory under
# /boot/efi, not wherever the fedora-bootc base puts it.
RUN mkdir -p /boot/efi && cp -ra /usr/lib/efi/*/*/EFI /boot/efi

# lorax wants to symlink /mnt -> /var/mnt, which doesn't exist on this base.
RUN mkdir -p /var/mnt
