#!/bin/sh
# auto/config — live-build configuration for bee ISO
# Runs automatically when lb config is called.
# See: man lb_config

set -e

. "$(dirname "$0")/../VERSIONS"

# Pin the exact kernel ABI detected by build.sh so the ISO kernel matches
# the kernel headers used to compile NVIDIA modules. Falls back to meta-package
# when lb config is run manually without the environment variable.
if [ -n "${BEE_KERNEL_ABI:-}" ] && [ "${BEE_KERNEL_ABI}" != "auto" ]; then
    LB_LINUX_PACKAGES="linux-image-${BEE_KERNEL_ABI}"
else
    LB_LINUX_PACKAGES="linux-image"
fi

if [ -n "${BEE_ISO_VOLUME:-}" ]; then
    LB_ISO_VOLUME="${BEE_ISO_VOLUME}"
else
    LB_ISO_VOLUME="EASY_BEE_${BEE_GPU_VENDOR_UPPER:-NVIDIA}"
fi

LB_HOSTNAME="${BEE_HOSTNAME:-easy-bee}"
LB_MIRROR="${BEE_MIRROR:-http://deb.debian.org/debian/}"

lb config noauto \
    --distribution bookworm \
    --architectures amd64 \
    --binary-images iso-hybrid \
    --bootloaders "grub-efi,syslinux" \
    --debian-installer none \
    --archive-areas "main contrib non-free non-free-firmware" \
    --mirror-bootstrap "${LB_MIRROR}" \
    --mirror-chroot "${LB_MIRROR}" \
    --mirror-binary "${LB_MIRROR}" \
    --security true \
    --linux-flavours "amd64" \
    --linux-packages "${LB_LINUX_PACKAGES}" \
    --memtest memtest86+ \
    --iso-volume "${LB_ISO_VOLUME}" \
    --iso-application "EASY-BEE-${BEE_GPU_VENDOR_UPPER:-NVIDIA}" \
    --bootappend-live "boot=live live-media=/dev/disk/by-label/${LB_ISO_VOLUME} live-media-label=${LB_ISO_VOLUME} components video=1920x1080 console=ttyS0,115200n8 console=tty0 loglevel=3 systemd.show_status=1 username=bee user-fullname=Bee modprobe.blacklist=nouveau,snd_hda_intel,snd_hda_codec_realtek,snd_hda_codec_generic,soundcore" \
    --debootstrap-options "--include=ca-certificates" \
    --apt-recommends false \
    --chroot-squashfs-compression-type zstd \
    "${@}"

# live-build (1:20230502, bookworm) has no `lb config --hostname` flag: the
# chroot_hostname helper zeroes /etc/hostname to "localhost.localdomain" for
# the duration of package installation and restores it afterwards from this
# includes file, so this is the only supported way to set it.
mkdir -p config/includes.chroot_before_packages/etc
echo "${LB_HOSTNAME}" > config/includes.chroot_before_packages/etc/hostname
