#!/bin/sh

set -e

# Include common options
. ./options

# live-build configuration
lb config noauto \
    --architectures "$ARCH" \
    --mode debian \
    --distribution "$BASECODENAME" \
    --parent-distribution "$BASECODENAME" \
    --archive-areas "main contrib non-free non-free-firmware" \
    --parent-archive-areas "main contrib non-free non-free-firmware" \
    --linux-packages linux-image-lts linux-headers-lts \
    --bootappend-live "boot=live username=tileos hostname=tileos quiet splash" \
    --mirror-bootstrap "$MIRROR_URL" \
    --parent-mirror-bootstrap "$MIRROR_URL" \
    --mirror-chroot-security "$MIRROR_SECURITY" \
    --parent-mirror-chroot-security "$MIRROR_SECURITY" \
    --apt-source-archives false \
    --parent-mirror-binary "$MIRROR_URL" \
    --keyring-packages "debian-keyring tileos-archive-keyring" \
    --apt-options "--yes --option Acquire::Retries=5 --option Acquire::http::Timeout=100" \
    --cache-packages false \
    --checksums md5 \
    --uefi-secure-boot enable \
    --binary-images iso-hybrid \
    --iso-application "$NAME" \
    --iso-volume "$NAME" \
    --firmware-chroot false \
    --zsync false \
    --security true \
    --updates true \
    --debootstrap-options="--extractor=ar --include=ca-certificates" \
    "${@}"

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

# replace desktop
sed -i "s/@DESKTOP/$DESKTOP/" config/package-lists.calamares/*.chroot_install
sed -i "s/@DESKTOP/$DESKTOP/" config/package-lists.calamares/*.chroot_live

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
