# Scale the GRAPHICAL installer's text, from the boot parameter
# scutarius/gui-zoom=<percent>. Installed as
# /usr/lib/debian-installer.d/S66scutarius-gui-zoom by arch_tree (patch 0021).
#
# WHY NOT A SMALLER SCREEN MODE. harma19 offered "Graphical install (800x600)"
# entries that put video=800x600 on the kernel command line. They did not work
# and could not: rootskel-gtk's S62Xorg starts Xorg with NO configuration, the
# modesetting driver takes the connector's PREFERRED mode straight from EDID,
# and it does that after the kernel has already set the console mode. So the
# text console shrank and the graphical installer came up at the panel's own
# resolution anyway -- measured in a VM on 2026-09-18 and then confirmed by the
# owner on a ROCK 5B with a 3440x1440 monitor, which is the screen the whole
# problem came from.
#
# Constraining Xorg instead would mean writing an xorg.conf.d snippet with a
# per-connector PreferredMode and a CVT modeline, and a mode the panel rejects
# is a BLACK SCREEN -- the failure that blacklisting the display driver already
# cost this project once. Scaling the text needs no mode change at all.
#
# WHY THIS IS A DEBCONF PARAMETER AND NOT A KERNEL ONE. It is the input to
# cdebconf's OWN zoom mechanism, the one behind its ZoomIn/ZoomOut keyboard
# shortcut (di_change_font_size, cdebconf 0.280 src/modules/frontend/gtk/di.c):
# the factor is kept in /var/lib/cdebconf/zoom and gtk-font-name in
# /etc/gtk-2.0/gtkrc is re-derived from it. We write the factor and then let
# rootskel-gtk's own gtk-set-font do the deriving, so the language-specific
# faces and the theme=dark size bump keep working and we own none of that
# arithmetic. The factor is stored in the file cdebconf's own shortcut reads
# and writes, so + and - compose with what we set here by construction; that
# said, only the pre-seeded half has been observed working on a screen.
#
# Order matters: this runs at S66, AFTER S65theme (whose gtk-set-theme calls
# gtk-set-font before we have written the factor) and BEFORE S70menu, which is
# where cdebconf starts and reads the gtkrc.
#
# THIS FILE IS SOURCED, by usr/sbin/debian-installer, under `set -e`:
#
#   for script in /lib/debian-installer.d/S[0-9][0-9]*; do . $script; done
#
# So it must not call exit -- that would end the installer rather than this
# fragment -- and every command must either succeed or be guarded, or a person
# in front of the screen gets no installer at all because a font size could
# not be parsed. Hence the `|| true` on everything that talks to debconf or
# the filesystem, and the digit-count check before the numeric comparison:
# `[ 99999999999999999999 -gt 400 ]` is a shell error, not a false.

if [ "$DEBIAN_FRONTEND" = gtk ]; then
	scutarius_zoom=$(debconf-get scutarius/gui-zoom 2>/dev/null || true)

	# One to three digits only, so the comparison below cannot overflow.
	case "$scutarius_zoom" in
	    ""|100)
		scutarius_zoom=
		;;
	    [0-9]|[0-9][0-9]|[0-9][0-9][0-9])
		if [ "$scutarius_zoom" -lt 100 ] || [ "$scutarius_zoom" -gt 400 ]; then
			logger -t scutarius-gui-zoom \
			  "ignoring scutarius/gui-zoom=$scutarius_zoom: outside 100-400" || true
			scutarius_zoom=
		fi
		;;
	    *)
		logger -t scutarius-gui-zoom \
		  "ignoring scutarius/gui-zoom=$scutarius_zoom: not a percentage of 100-400" || true
		scutarius_zoom=
		;;
	esac

	if [ -n "$scutarius_zoom" ]; then
		mkdir -p /var/lib/cdebconf || true
		echo "$scutarius_zoom" > /var/lib/cdebconf/zoom || true
		# rootskel-gtk's own script: reads the factor we just wrote,
		# multiplies its base size of 9 by it, and applies the
		# language and theme=dark adjustments on top.
		gtk-set-font || true
		logger -t scutarius-gui-zoom "graphical installer text at $scutarius_zoom%" || true
	fi

	unset scutarius_zoom
fi
