#!/bin/sh
# apt-setup generator, installed on arm64 as
# /usr/lib/apt-setup/generators/02scutarius-sbc-firmware (patches 0017, 0018).
# Writes no sources; it runs before apt-mirror-setup's 50mirror, and well
# before grub-installer, because apt-setup runs its generators in name order
# and the main menu runs apt-setup first. Everything here is per board, from
# the board table (/usr/share/scutarius/sbc-boards via sbc-lib); a board not
# in the table is left alone.
#
# nff=yes: the board's UEFI firmware, written to SPI flash at the end of the
# installation, is in non-free-firmware. This follows upstream's own pattern
# (owner 2026-09-18): hw-detect's post-base-installer.d/50install-firmware
# pre-sets apt-setup/non-free-firmware to true when the hardware needs firmware
# from that component (CPU microcode on x86), and apt-mirror-setup's 50mirror
# asks the question at LOW priority -- so a normal install takes the default
# and an expert install shows it. We do the same for these boards: default
# true, no question of our own, no raised priority. What we add is the WORDING:
# the question is re-bound (db_register) to a template that says why this
# board needs the component. REGISTER keeps the question's value and flags
# (cdebconf 0.280 command_register only swaps q->template), and a value the
# user already gave -- preseeded, so marked seen -- is left alone.
#
# removable=yes: GRUB goes to the EFI removable media path as well, without
# asking. A board whose firmware was just written starts with an empty variable
# store and finds the installed system only there (ROCK 5B, 2026-09-17: the
# NVRAM held no Scutarius entry after the SPI write).
. /usr/share/debconf/confmodule
. /usr/lib/scutarius/sbc-lib
sbc_board || exit 0
log() { logger -t scutarius-sbc "$@" 2>/dev/null || true; }

if [ "$SBC_REMOVABLE" = yes ]; then
	db_set grub-installer/force-efi-extra-removable true
	db_fset grub-installer/force-efi-extra-removable seen true
	log "$SBC_COMPAT: GRUB to the removable media path (board table)"
fi

[ "$SBC_NFF" = yes ] || exit 0
TPL=/tmp/scutarius-sbc-apt.templates
cat > $TPL <<'EOT'
Template: scutarius/sbc-non-free-firmware
Type: boolean
Default: true
Description: Use non-free firmware? This board needs it to boot.
 Firmware is a kind of software providing low-level control of certain
 hardware components, which may not function fully or at all without it.
 .
 This board starts through UEFI firmware stored in its SPI flash. So that it
 boots the installed system without the installation media, the installer
 writes the UEFI firmware built for this distribution there at the end of the
 installation. That firmware is in the non-free-firmware component, because
 it contains Rockchip's closed memory-initialisation code: without this
 component it cannot be installed or written, and the board may not boot the
 installed system.
EOT
debconf-loadtemplate scutarius $TPL || true
db_register scutarius/sbc-non-free-firmware apt-setup/non-free-firmware || true
db_fget apt-setup/non-free-firmware seen
if [ "$RET" = true ]; then
	db_get apt-setup/non-free-firmware
	log "$SBC_COMPAT: non-free-firmware already answered ($RET), left alone"
else
	db_set apt-setup/non-free-firmware true
	log "$SBC_COMPAT: non-free-firmware defaults to true (board table)"
fi
exit 0
