#!/bin/sh
# alloy-layer-notice — tell the person at the machine what the blank boot is.
#
# alloy-layer-components.service lays the console and terminal down as layered
# packages, then reboots. Two boots therefore show nothing a user can act on:
# the first one after an install, and the first one after adopting a rebuilt
# image, because `bootc switch` discards the layer and the unit re-fires to
# install the new image's copy (measured, build/layertest/README.md). The
# second of those is the common case and the one nobody expects, since the
# machine was working when they typed the command.
#
# Installed machines boot with `quiet loglevel=3` so kernel and audit output
# does not overwrite tuigreet (usr/lib/bootc/kargs.d/10-alloy.toml), and there
# is no plymouth. So that boot is a genuinely blank screen: systemd's own
# status lines are suppressed by `quiet`, and the unit's Description never
# reaches anyone. Nothing tells the user, hence this.
#
# Writing to /dev/console rather than to stdout, and it is the point rather
# than a detail. The unit's output goes to the journal, which is exactly where
# a person with no console and no terminal cannot read it. `quiet` filters
# kernel printk and does not touch a userspace write to the console device, so
# this lands on the foreground VT whatever the cmdline says.
#
# `|| true` on every write, and no `set -e`. This script is on the boot path in
# front of the only thing that gives the machine a console: a notice that fails
# must never be the reason the components are not installed. Same argument as
# alloy-session's, one unit earlier.
#
# Deliberately dependency-free: sh, printf, one redirect. It runs on a machine
# that does not have its own console yet, so anything it reaches for is
# something that can be missing.

say() {
    printf '%s\n' "$1" > /dev/console 2>/dev/null || true
}

case "${1:-start}" in
    start)
        say ""
        say "Alloy is installing its console and terminal."
        say ""
        say "This runs once after an install, and again after you adopt a"
        say "rebuilt image. It takes a few minutes, and the machine restarts"
        say "by itself when it finishes."
        say ""
        say "Nothing is wrong. There is no login until it is done."
        say ""
        ;;
    fail)
        say ""
        say "Alloy could not install its console and terminal."
        say ""
        say "The next restart tries again. If it keeps failing, the reason is"
        say "in the journal, which you can read over ssh:"
        say ""
        say "    journalctl -u alloy-layer-components"
        say ""
        ;;
    *)
        # An unknown argument is a caller bug, and the unit is the only caller.
        # Saying nothing is right: the alternative is a machine whose first
        # words to its owner are a usage message.
        ;;
esac
