# Decide whether the text installer may show a moving backdrop behind itself,
# and say so in the environment. Installed as
# /usr/lib/debian-installer.d/S68scutarius-installer-backdrop by arch_tree.
#
# WHAT THIS FILE DOES AND DOES NOT DO. It does not draw anything and it does
# not start anything. bterm (profiles/bogl) starts the backdrop program, reads
# its pictures and composites them; it does that only when it finds
# BTERM_BACKDROP=1 in its environment together with the two palette slots the
# colour fragment exports. This file is the only place that sets the first of
# those three, so it is the switch, and everything below is the list of
# conditions under which the switch may be closed.
#
# AND IT IS THE ONLY PLACE THAT CLEARS IT. Everything the variable buys -- an
# untouched byte stream on a serial line, a console that is never sent a
# sequence it would print at the user -- rests on bterm and cdebconf finding
# NO BTERM_BACKDROP in their environment when the answer is no. Nothing that
# builds the installer's environment sets it (sshd for network-console builds
# a fresh one), so no path is known by which it could arrive already set; the
# `unset` on every negative arm below costs nothing and does not depend on
# that staying true.
#
# It runs at S68: after S30 (TERM_TYPE), S35 (TERM_FRAMEBUFFER), S40 (TERM_UTF8
# and the UTF-8 console), S60 (the frontend), S65 (the theme) and S67 (the
# colour map, which exports the slots), and before S70menu, which is the shell
# that execs bterm -- so bterm inherits what is exported here exactly as it
# inherits BTERM_PALETTE.
#
# WHY IT MUST NOT SAY YES WHERE BTERM WILL NOT RUN, which is more than a
# missed picture. With BTERM_BACKDROP=1 in its environment the patched
# cdebconf newt frontend writes private OSC sequences (ESC ] 7770;busy) to
# tell the backdrop whether the installer is working. bterm understands them
# and swallows them. The LINUX CONSOLE does not: console_codes(4) gives it
# only P and R after ESC ], so the rest of a private OSC is left to land as
# text on the screen a person is reading. S70menu runs bterm only when there
# is a bterm binary and a font, the console is UTF-8 and a framebuffer, and
# the frontend is not gtk; this file therefore asks the same questions, in the
# same way, before it says yes.
#
# THE CONDITIONS, and what each one is read from:
#
#  - the newt frontend on a real console. TERM_TYPE=virtual is the monitor
#    (S30's detect-console); a serial line and a network-console pseudo-
#    terminal are left byte-identical to upstream, which the install-driving
#    scripts depend on. DEBIAN_FRONTEND=newt also excludes the text frontend
#    that screen-reader installs switch to.
#  - the accessible dark theme is off. theme=dark reaches here as
#    FRONTEND_BACKGROUND=dark (S65). That palette exists for people who need
#    contrast; a picture behind the text is the opposite of that.
#  - no screen reader and no braille display. A loaded speakup_* module is
#    what S20 looks for, speakup.synth= on the command line is what starts
#    one, and brltty on the command line is what starts a braille display.
#    Any of the three means no backdrop, however the console looks.
#  - no backdrop=off on the kernel command line. The one switch a person at
#    the boot menu has; it is exact, so a parameter that merely contains the
#    word does not trip it.
#  - not a low-memory install. The installer's own marker is /var/lib/lowmem,
#    and lowmem= is its own boot parameter. A backdrop costs RAM for the
#    pictures and for the program producing them, and the install itself must
#    have it first.
#  - the colour fragment ran and gave two DIFFERENT slots. If it did not run,
#    the map that says which cells are ground and which are dialog is not
#    there, and without it a backdrop would paint over the wrong cells.
#
# AND ONE MORE THAT IS NOT DECIDED HERE: whether this machine can afford it.
# bterm measures that itself before it starts the backdrop (bogl's
# backdrop-probe.h: a few seconds of an invisible picture of its own, its
# processor load and the host's steal time against thresholds). The four
# knobs of that measurement come from the kernel command line and are passed
# on from here, as plain decimal numbers only -- bterm checks their ranges
# again and falls back to its own defaults:
#
#    backdrop.probe=<ms>          BTERM_BACKDROP_PROBE_MS     (0: no probe)
#    backdrop.maxload=<percent>   BTERM_BACKDROP_MAXLOAD      (0: never)
#    backdrop.maxsteal=<percent>  BTERM_BACKDROP_MAXSTEAL
#    backdrop.probechange=<pct>   BTERM_BACKDROP_PROBE_CHANGE
#
# 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 never exit -- that would end the INSTALLER, not this fragment --
# and no command in it may return non-zero unguarded. A person in front of the
# screen must not lose the installer because a moving picture could not be
# decided upon. Hence the guard on every command that reads a file or asks the
# kernel, and the plain shell tests everywhere else.

# WHERE THE FILES BELOW ARE READ FROM. Empty on a booted installer, which is
# every case but one: the tests, which have no /proc/cmdline of their own to
# write and no installer to put a bterm in. The installer sets TESTSUITE
# nowhere; d-i's own user-params takes the same hook for the same reason.
scutarius_pfx=${TESTSUITE:-}

if [ "$DEBIAN_FRONTEND" = newt ] && [ "${FRONTEND_BACKGROUND:-}" != dark ] \
   && [ "${TERM_TYPE:-}" = virtual ] \
   && [ -n "${TERM_UTF8:-}" ] && [ -n "${TERM_FRAMEBUFFER:-}" ] \
   && [ -x "$scutarius_pfx/usr/bin/bterm" ] && [ -e "$scutarius_pfx/lib/unifont.bgf" ] \
   && [ -n "${BTERM_BACKDROP_ROOT:-}" ] && [ -n "${BTERM_BACKDROP_TILE:-}" ] \
   && [ "${BTERM_BACKDROP_ROOT:-}" != "${BTERM_BACKDROP_TILE:-}" ]; then
	scutarius_backdrop=yes

	# The kernel command line, read once. An unreadable /proc/cmdline leaves
	# the word list empty, which switches nothing off by itself -- the
	# checks that follow it still apply.
	scutarius_cmdline=$(cat "$scutarius_pfx/proc/cmdline" 2>/dev/null || true)

	# Exact parameters, word by word, so that neither a longer word ending
	# in "backdrop=off" nor a preseed value containing it can decide this.
	scutarius_probe= scutarius_maxload= scutarius_maxsteal= scutarius_change=
	for scutarius_w in $scutarius_cmdline; do
		case "$scutarius_w" in
		    backdrop=off|lowmem=*)
			scutarius_backdrop=
			;;
		    backdrop.probe=*)
			scutarius_probe=${scutarius_w#backdrop.probe=}
			;;
		    backdrop.maxload=*)
			scutarius_maxload=${scutarius_w#backdrop.maxload=}
			;;
		    backdrop.maxsteal=*)
			scutarius_maxsteal=${scutarius_w#backdrop.maxsteal=}
			;;
		    backdrop.probechange=*)
			scutarius_change=${scutarius_w#backdrop.probechange=}
			;;
		esac
	done
	unset scutarius_w

	# Accessibility, as the installer's own fragments look for it: a
	# substring, because that is what starts brltty and speakup and because
	# a missed screen reader is worse than a missed picture.
	case "$scutarius_cmdline" in
	    *brltty*|*speakup.synth=*)
		scutarius_backdrop=
		;;
	esac
	unset scutarius_cmdline

	# The screen reader may also have been started by something other than
	# the command line; the module is the evidence either way.
	if lsmod 2>/dev/null | grep -q '^speakup_'; then
		scutarius_backdrop=
	fi

	if [ -e "$scutarius_pfx/var/lib/lowmem" ]; then
		scutarius_backdrop=
	fi

	# The probe's knobs, each only as a plain decimal number; anything
	# else is left unset, which is bterm's default.
	unset BTERM_BACKDROP_PROBE_MS BTERM_BACKDROP_MAXLOAD \
	      BTERM_BACKDROP_MAXSTEAL BTERM_BACKDROP_PROBE_CHANGE
	if [ -n "$scutarius_backdrop" ]; then
		BTERM_BACKDROP=1
		export BTERM_BACKDROP
		case "$scutarius_probe" in
		    ''|*[!0-9]*) ;;
		    *) BTERM_BACKDROP_PROBE_MS=$scutarius_probe
		       export BTERM_BACKDROP_PROBE_MS ;;
		esac
		case "$scutarius_maxload" in
		    ''|*[!0-9]*) ;;
		    *) BTERM_BACKDROP_MAXLOAD=$scutarius_maxload
		       export BTERM_BACKDROP_MAXLOAD ;;
		esac
		case "$scutarius_maxsteal" in
		    ''|*[!0-9]*) ;;
		    *) BTERM_BACKDROP_MAXSTEAL=$scutarius_maxsteal
		       export BTERM_BACKDROP_MAXSTEAL ;;
		esac
		case "$scutarius_change" in
		    ''|*[!0-9]*) ;;
		    *) BTERM_BACKDROP_PROBE_CHANGE=$scutarius_change
		       export BTERM_BACKDROP_PROBE_CHANGE ;;
		esac
	else
		unset BTERM_BACKDROP
	fi
	unset scutarius_backdrop scutarius_probe scutarius_maxload \
	      scutarius_maxsteal scutarius_change
else
	unset BTERM_BACKDROP BTERM_BACKDROP_PROBE_MS BTERM_BACKDROP_MAXLOAD \
	      BTERM_BACKDROP_MAXSTEAL BTERM_BACKDROP_PROBE_CHANGE
fi
unset scutarius_pfx
