#!/bin/sh
# systemd generator: wire VTs to cruft or leave kernel gettys alone.
# Reads /etc/default/console (CONSOLE, CONSOLE_TTYS, CONSOLE_FONT / CRUFT_FONT).
#
# Boot: only the first CONSOLE_TTYS entry (usually tty1) is started.
# Further VTs are started on demand by logind as autovt@ttyN (symlink → cruft@).
#
# Usage (systemd): cruft-generator <normal-dir> [early-dir] [late-dir]
# Self-check:      cruft-generator --self-check

set -eu

CONSOLE=cruft
CONSOLE_TTYS="1 2 3 4 5 6"
CONSOLE_FONT=default
CRUFT_FONT=
UNITDIR="/usr/lib/systemd/system"

self_check=0
normal_dir="${1:-}"

if [ "${1:-}" = "--self-check" ]; then
	self_check=1
	normal_dir=$(mktemp -d)
	# shellcheck disable=SC2064
	trap 'rm -rf "$normal_dir"' EXIT
	CONSOLE=cruft
	CONSOLE_TTYS="1 2"
	CONSOLE_FONT=miniwi
elif [ -z "$normal_dir" ]; then
	echo "cruft-generator: missing output directory" >&2
	exit 1
fi

if [ "$self_check" -eq 0 ] && [ -f /etc/default/console ]; then
	# shellcheck disable=SC1091
	. /etc/default/console
fi

# Upgrade alias: CONSOLE=yaft means cruft.
case "$CONSOLE" in
yaft) CONSOLE=cruft ;;
esac

case "$CONSOLE" in
cruft) ;;
kernel)
	if [ "$self_check" -eq 1 ]; then
		echo "cruft-generator: --self-check expects CONSOLE=cruft" >&2
		exit 1
	fi
	exit 0
	;;
*)
	echo "cruft-generator: unknown CONSOLE='$CONSOLE' (use cruft or kernel)" >&2
	exit 1
	;;
esac

# Explicit CRUFT_FONT wins; YAFT_FONT alias; else CONSOLE_FONT profile.
if [ -z "${CRUFT_FONT:-}" ] && [ -n "${YAFT_FONT:-}" ]; then
	CRUFT_FONT=$YAFT_FONT
fi
if [ -z "${CRUFT_FONT:-}" ]; then
	case "${CONSOLE_FONT:-default}" in
	default|console|"") CRUFT_FONT=/usr/share/cruft/console.cruftfont ;;
	miniwi) CRUFT_FONT=/usr/share/cruft/miniwi.cruftfont ;;
	unifont) CRUFT_FONT=/usr/share/cruft/unifont.cruftfont ;;
	*)
		echo "cruft-generator: unknown CONSOLE_FONT='$CONSOLE_FONT' (default|miniwi|unifont)" >&2
		exit 1
		;;
	esac
fi

# shellcheck disable=SC2086
set -- $CONSOLE_TTYS
boot_tty=$1
if [ -z "$boot_tty" ]; then
	echo "cruft-generator: CONSOLE_TTYS is empty" >&2
	exit 1
fi

wants="$normal_dir/getty.target.wants"
mkdir -p "$wants"

font_dropin() {
	mkdir -p "$1"
	printf '%s\n' '[Service]' \
		"Environment=TERM=cruft-256color" \
		"Environment=CRUFT_FONT=$CRUFT_FONT" \
		> "$1/10-font.conf"
}

# Drop-ins for both names: boot uses cruft@ttyN; on-demand uses autovt@ttyN
# (autovt@.service → cruft@.service, but drop-in lookup follows the job name).
font_dropin "$normal_dir/cruft@.service.d"
font_dropin "$normal_dir/autovt@.service.d"

# On-demand VTs: logind starts autovt@ttyN when the user switches to that VT.
ln -sf "$UNITDIR/cruft@.service" "$normal_dir/autovt@.service"

# Boot VT only — do not pull tty2+ into getty.target.
ln -sf "$UNITDIR/cruft@.service" "$wants/cruft@tty${boot_tty}.service"

for n in $CONSOLE_TTYS; do
	ln -sf /dev/null "$normal_dir/getty@tty${n}.service"
done

if [ "$self_check" -eq 1 ]; then
	[ -L "$wants/cruft@tty1.service" ] || { echo "missing cruft@tty1 wants" >&2; exit 1; }
	[ ! -e "$wants/cruft@tty2.service" ] || { echo "tty2 should be on-demand, not in wants" >&2; exit 1; }
	[ "$(readlink "$normal_dir/autovt@.service")" = "$UNITDIR/cruft@.service" ] || {
		echo "autovt@.service should point at cruft@.service" >&2
		exit 1
	}
	[ "$(readlink "$normal_dir/getty@tty1.service")" = "/dev/null" ] || {
		echo "getty@tty1 not masked" >&2
		exit 1
	}
	[ "$(readlink "$normal_dir/getty@tty2.service")" = "/dev/null" ] || {
		echo "getty@tty2 not masked" >&2
		exit 1
	}
	grep -q 'CRUFT_FONT=/usr/share/cruft/miniwi.cruftfont' \
		"$normal_dir/cruft@.service.d/10-font.conf" || {
		echo "cruft@.service.d missing miniwi path" >&2
		exit 1
	}
	grep -q 'CRUFT_FONT=/usr/share/cruft/miniwi.cruftfont' \
		"$normal_dir/autovt@.service.d/10-font.conf" || {
		echo "autovt@.service.d missing miniwi path" >&2
		exit 1
	}
	grep -q 'TERM=cruft-256color' \
		"$normal_dir/cruft@.service.d/10-font.conf" || {
		echo "font drop-in missing TERM=cruft-256color" >&2
		exit 1
	}
	echo "cruft-generator: self-check ok"
fi

exit 0
