#!/bin/sh

set -e

. ./terraform.conf

if [ "$HWE_KERNEL" = "yes" ]; then
    KERNEL_FLAVORS="generic-hwe-${BASEVERSION}"
else
    KERNEL_FLAVORS="generic"
fi

lb config noauto \
    --architectures "$ARCH" \
    --mode ubuntu \
    --initramfs none \
    --distribution "$BASECODENAME" \
    --parent-distribution "$BASECODENAME" \
    --archive-areas "main restricted universe multiverse" \
    --parent-archive-areas "main restricted universe multiverse" \
    --linux-packages linux-image \
    --linux-flavours "$KERNEL_FLAVORS" \
    --bootappend-live "boot=casper maybe-ubiquity quiet splash" \
    --mirror-bootstrap "$MIRROR_URL" \
    --parent-mirror-bootstrap "$MIRROR_URL" \
    --mirror-chroot-security "http://security.ubuntu.com/ubuntu/" \
    --parent-mirror-chroot-security "http://security.ubuntu.com/ubuntu/" \
    --mirror-binary-security "http://security.ubuntu.com/ubuntu/" \
    --parent-mirror-binary-security "http://security.ubuntu.com/ubuntu/" \
    --mirror-binary "http://archive.ubuntu.com/ubuntu/" \
    --parent-mirror-binary "http://archive.ubuntu.com/ubuntu/" \
    --keyring-packages ubuntu-keyring \
    --apt-options "--yes --option Acquire::Retries=5 --option Acquire::http::Timeout=100" \
    --apt-recommends false \
    --cache-packages false \
    --uefi-secure-boot enable \
    --binary-images iso-hybrid \
    --iso-application "$NAME" \
    --iso-volume "$NAME" \
    --firmware-binary false \
    --firmware-chroot false \
    --zsync false \
    --security true \
    --updates true \
    --debootstrap-options "--exclude=pinephone-tweaks,mobile-tweaks-common,librem5-tweaks,pinetab-tweaks" \
    --checksums md5 \
    "${@}"


# replace channel and suite
# sed -i "s/@CHANNEL/$CHANNEL/" config/archives/*.list*
# sed -i "s/@BASECODENAME/$BASECODENAME/" config/archives/*.list*

DATE=$(date +%Y%m%d)
sed -i "s/@CHANNEL/$CHANNEL/" config/includes.binary/.disk/info
sed -i "s/@CODENAME/$CODENAME/" config/includes.binary/.disk/info
sed -i "s/@ARCH/$ARCH/" config/includes.binary/.disk/info
sed -i "s/@DISTRO_NAME/$NAME/" config/includes.binary/.disk/info
sed -i "s/@VERSION/$VERSION/" config/includes.binary/.disk/info
sed -i "s/@DATE/$DATE/" config/includes.binary/.disk/info

sed -i "s/@DISTRO_NAME/$NAME/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@VERSION/$VERSION/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@CHANNEL/$CHANNEL/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@CODENAME/$CODENAME/" config/hooks/live/004-set-os-info.chroot
sed -i "s/@BASECODENAME/$BASECODENAME/" config/hooks/live/004-set-os-info.chroot

# sed -i "s/@XORG_HWE/$XORG_HWE/" config/package-lists/desktop.list.chroot_install
