#!/bin/sh

set -eux

os="${1:-fedora}"
cd "${0%/*}"

../common/check-config

if [ "${os}" != 'fedora' ]; then
    echo '*** only fedora is supported'
    false
fi

cargo build --release

cp ../../target/release/cfsctl .
cp ../../target/release/composefs-setup-root extra/usr/lib/dracut/modules.d/37composefs/
CFSCTL='./cfsctl --repo tmp/sysroot/composefs'

rm -rf tmp
rm -rf tmp/efi tmp/sysroot/composefs/images
${CFSCTL} init --algorithm=fsverity-sha256-12 --erofs-version 1

# For debugging, add --no-cache to podman command
mkdir -p tmp/internal-sysroot
podman build \
    --iidfile=tmp/iid \
    -v "${PWD}/tmp/internal-sysroot:/tmp/sysroot:z,U" \
    .

IMAGE_ID="$(cat tmp/iid)"
${CFSCTL} oci pull containers-storage:"${IMAGE_ID}"
${CFSCTL} oci prepare-boot "@${IMAGE_ID}" --bootdir tmp/efi

../common/install-systemd-boot
../common/make-image fedora-unified-efi.qcow2
