# The TEXT installer in this distribution's colours (scheme "A-night", owner
# 2026-09-18): charcoal ground, dark-grey dialog, bone text, bronze titles and
# selection. Installed as /usr/lib/debian-installer.d/S67scutarius-newt-colors
# by arch_tree (patch 0025).
#
# TWO HALVES, because the console only gives us two knobs:
#
#  1. WHICH colour each part of the UI uses -- a role-to-colour-NAME map, in
#     NEWT_COLORS. cdebconf used to throw that variable away (its newt
#     frontend re-applied the default palette right after newtInit()), which
#     is why profiles/cdebconf exists: its patch 0001 makes the frontend honour
#     it. Without that rebuilt cdebconf this fragment's NEWT_COLORS is inert
#     and the installer simply stays Devuan blue -- never broken.
#
#  2. WHAT each colour name looks like on a monitor. On a framebuffer the
#     installer runs inside bterm (S70menu), which draws with its own 16-colour
#     table and reads BTERM_PALETTE for it (profiles/bogl patch 0001 adds that);
#     on a plain kernel console the same slots are redefined with the console's
#     ESC ] P n rrggbb sequence. Either way "blue" below means tile grey, not
#     blue, on the monitor. Both renderers draw bold as the bright slot
#     (colour + 8), which is how "white" and "yellow" reach slots 15 and 11.
#
# SERIAL, and why this is monitor-only (harma27). harma26 exported
# NEWT_COLORS on serial consoles too, on the belief that d-i drives serial as
# monochrome vt102 and the names would be inert there. That was wrong: on the
# install-test guests the serial console renders colour, the selected row
# became black-on-brown (ESC[30m ESC[43m) instead of white-on-red (ESC[41m),
# and scripts/image/drive-di.sh -- which finds the highlighted item by exactly
# that escape before any destructive answer, and refuses when it cannot --
# refused at partitioning on both arches. Serial output is now left exactly as
# upstream draws it, which also keeps every tool that reads it working.
#
# Constraints that shaped the map, all measured:
#  - the Linux console has only EIGHT background colours (bright SGR 100-107
#    backgrounds fold onto 40-47), so every background below is one of
#    black/red/green/brown/blue/magenta/cyan/lightgray;
#  - newt draws the dialog text and the drop shadow as SEPARATE roles only if
#    they get separate names, which is the whole reason the stock palette
#    could not do a dark dialog: there both are "black";
#  - "red" is left alone on purpose: cdebconf turns the ground red for error
#    notes, and an error should still look like one;
#  - FOCUS on a button is the "button" role, not "actbutton": on a colour
#    terminal newt draws a compact button -- every button cdebconf makes --
#    with "compactbutton" when unfocused and "button" when focused
#    (newt 0.52.25 button.c, buttonDrawIt; "actbutton" is only for monochrome).
#    harma27 gave both the same colours, so focus was invisible on every
#    Yes/No and Continue/Go Back (measured on g3's HDMI console); the owner
#    ruled that a focused button uses the selected list item's bronze.
#  - newt's scale colours: "fullscale" is the COMPLETED part and "emptyscale"
#    the rest (libnewt's own comments have them swapped; scale.c is the
#    authority), and the percentage is always drawn in "white".
#
# ONE ROLE PER SLOT WHERE IT MATTERS. Five newt roles wanted background slot 0
# -- root, shadow, entry, helpline and roottext -- and nothing distinguished
# them, so anything that treats "the ground" specially would have treated the
# drop shadows, the entry fields, the help line and the root text as ground
# too. The backdrop (S68) is exactly such a thing: it replaces the pixels of
# every cell whose background is the ROOT slot. So all of them EXCEPT root move
# to a spare background slot, which is then given the ground's own RGB: the
# screen looks the same to the eye, and slot 0 now means root and nothing else.
#
# Which spare: backgrounds are slots 0-7 only (bold is foreground + 8). 0, 3,
# 4, 6 and 7 are taken below, 1 (red) is deliberately left stock for error
# notes, so the spare is 2 -- "green" in newt's names, charcoal on the screen.
#
# It is done UNCONDITIONALLY, not only when a backdrop is enabled. This
# fragment runs BEFORE the one that decides whether a backdrop may run, so it
# cannot know; and a colour map that changes shape depending on another
# fragment is a second version of the map to keep in step. Since slot 2 carries
# the ground's own colour, the unconditional form is what makes "with a
# backdrop" and "without one" look identical rather than merely similar.
#
# The palette (branding/scutarius/MANIFEST.md and the desktop colour map):
#   slot  0 black     deep charcoal #15171a   the ground (root), and it alone
#   slot  2 green     deep charcoal #15171a   shadow, entry fields, help line,
#                                             root text: ground to the eye,
#                                             not the ground to the backdrop
#   slot  3 brown     bronze        #c9a266   selection, completed progress
#   slot  4 blue      tile          #2e3237   the dialog
#   slot  6 cyan      line          #3f444a   remaining progress
#   slot  7 lightgray slate         #a9a397   help line and secondary text
#   slot 11 yellow    bronze        #c9a266   titles
#   slot 15 white     bone          #ece8df   text
#
# EVERY BACKGROUND SLOT ALSO SETS ITS BRIGHT TWIN, and that is not decoration.
# A cell is drawn inverted when the cursor is on it: bterm swaps the cell's two
# slots (fg = the cell's background, bg = its foreground) and THEN applies bold
# -- `if (bd) fg += 8` in bogl-term.c's put_char. Every role whose foreground
# is "white" or "yellow" is bold, because those names ARE slots 7 and 3 drawn
# bold. So under the cursor a cell that was white-on-X draws its glyph in slot
# X+8. With only X set, X+8 is whatever bterm's stock table had there, and on
# an entry field (white on the spare slot 2) that is stock slot 10, PURE GREEN
# #00ff00 -- seen on every entry field of the 2026-09-20 test build. The same
# defect was in harma27/harma28 with entry on slot 0: there it drew stock slot
# 8 #555555, dark grey on charcoal, and nobody could see it.
#
# So each slot used as a BACKGROUND by the map above gets its twin set to its
# own RGB, which makes an inverted cell keep its colour:
#   0 black  -> slot  8   2 green -> slot 10   3 brown -> slot 11 (already set)
#   4 blue   -> slot 12   6 cyan  -> slot 14
# Slot 7 is a FOREGROUND only (no role has a lightgray background), so its twin
# 15 is free to be bone, which is what "white" means here; that is why the rule
# has no exception. Slot 11 was already bronze for "yellow" and bronze is also
# slot 3, so it satisfies the rule as it stands. None of 8, 10, 12 and 14 is
# named as a foreground by any role -- "gray", "brightgreen", "brightblue" and
# "brightcyan" are unused -- so setting them changes nothing but inverted cells.
# Slot 1 (red) is not a background here either, so its twin 9 stays stock along
# with it, and cdebconf's red error screen is untouched.
# tests/newt-colors-harness.sh enforces the rule against the map.
#
# NOT APPLIED when the accessible dark theme is on (theme=dark, which S65theme
# turns into FRONTEND_BACKGROUND=dark): that palette is for people who need
# high contrast and it is not ours to restyle. The patched cdebconf refuses
# NEWT_COLORS in that case too; skipping here also keeps the console's stock
# colours, which that palette was designed against.
#
# THIS FILE IS SOURCED, by usr/sbin/debian-installer, under `set -e`, like
# every S* here -- so it never calls exit (that would end the INSTALLER), and
# nothing in it may return non-zero unguarded. It runs at S67: after S60
# (DEBIAN_FRONTEND is chosen), S30 (TERM_TYPE is known) and S65 (the theme),
# before S70menu starts cdebconf.

# ONLY on a real console (TERM_TYPE=virtual): the monitor, drawn by bterm or
# the kernel console, which is the only place this scheme was verified. On a
# serial console or a network-console pseudo-terminal nothing is exported and
# nothing is written, so they look and behave exactly as before -- see
# "SERIAL" in the header for what went wrong the one time it was otherwise.
if [ "$DEBIAN_FRONTEND" = newt ] && [ "${FRONTEND_BACKGROUND:-}" != dark ] \
   && [ "${TERM_TYPE:-}" = virtual ]; then
	# One line, ';'-separated (libnewt's grammar), built in pieces for
	# readability: a multi-line value in the environment is the sort of thing
	# that later confuses a script walking `set` output.
	# "green" is the spare slot 2, given the ground's colour below: it is what
	# the four non-root roles that used to share the ground's slot 0 use now.
	NEWT_COLORS="root=white,black;border=white,blue;window=white,blue;shadow=black,green;"
	NEWT_COLORS="${NEWT_COLORS}title=yellow,blue;button=black,brown;actbutton=black,brown;compactbutton=white,blue;"
	NEWT_COLORS="${NEWT_COLORS}checkbox=white,blue;actcheckbox=black,brown;entry=white,green;disentry=lightgray,blue;"
	NEWT_COLORS="${NEWT_COLORS}label=white,blue;listbox=white,blue;actlistbox=black,brown;sellistbox=black,brown;"
	NEWT_COLORS="${NEWT_COLORS}actsellistbox=black,brown;textbox=white,blue;acttextbox=black,brown;helpline=lightgray,green;"
	NEWT_COLORS="${NEWT_COLORS}roottext=lightgray,green;emptyscale=,cyan;fullscale=,brown;"
	export NEWT_COLORS

	# The palette, once, as "Nrrggbb" items (slot N, colour rrggbb). The same
	# string feeds both renderers of the text installer on a monitor:
	#  - bterm, which S70menu runs the installer inside whenever the console
	#    is a UTF-8 framebuffer -- the usual case. bterm draws the screen
	#    itself with its own table, so the kernel console's palette never
	#    applies there; bogl from profiles/bogl reads BTERM_PALETTE instead.
	#  - the plain kernel console (no framebuffer, or no UTF-8), which takes
	#    the same items as ESC ] P escapes.
	# Measured 2026-09-18: with cdebconf's half alone the dialog followed this
	# map in bterm's stock colours (navy, yellow, olive); the palette is the
	# other half.
	# Slot 2 carries the SAME rrggbb as slot 0 on purpose (see "ONE ROLE PER
	# SLOT" above): the four roles moved off the ground's slot keep the
	# ground's colour, on both renderers, so nothing on the screen moves.
	# Slots 8, A, C and E are the bright twins of the background slots 0, 2,
	# 4 and 6, each carrying its base slot's own RGB: that is what an
	# INVERTED cell draws, which is to say the cell under the cursor (see
	# "EVERY BACKGROUND SLOT ALSO SETS ITS BRIGHT TWIN" above). B is both
	# "yellow" and the twin of background slot 3, and bronze either way.
	scutarius_palette="015171a 215171a 3c9a266 42e3237 63f444a 7a9a397 \
815171a A15171a Bc9a266 C2e3237 E3f444a Fece8df"
	BTERM_PALETTE=$scutarius_palette
	export BTERM_PALETTE

	for scutarius_p in $scutarius_palette; do
		printf '\033]P%s' "$scutarius_p" || true
	done
	unset scutarius_p
	unset scutarius_palette

	# WHICH slots the backdrop must treat as ground and as dialog. They are
	# facts about the map above -- root's background and the dialog's -- so
	# they are stated here, where a change to the map is a change to them, and
	# not in the fragment that decides WHETHER a backdrop may run. bterm needs
	# BTERM_BACKDROP=1 as well, and only that other fragment sets it, so these
	# two on their own change nothing.
	BTERM_BACKDROP_ROOT=0
	BTERM_BACKDROP_TILE=4
	export BTERM_BACKDROP_ROOT BTERM_BACKDROP_TILE
else
	# The three variables that are OURS are cleared where this fragment
	# says no, so that what S68 and bterm read is what this fragment
	# decided and not something inherited. NEWT_COLORS is deliberately NOT
	# among them: it is libnewt's own variable, upstream d-i honours one
	# that arrives set, and the promise made to serial and pts consoles is
	# to be byte-identical to upstream and not merely quiet.
	unset BTERM_PALETTE BTERM_BACKDROP_ROOT BTERM_BACKDROP_TILE
fi
