#!/bin/sh
# rikkiti-greeter-session — greetd launches THIS as the greeter command (docs/greeter-locker-v2-plan.md, stage 3).
#
# The login screen is rikkiti-greeter (Baldr) in the compositor's login mode: `rikkiti-comp --login` runs it and
# nothing else, the card on the main screen and the wallpaper on the rest. When a login starts a desktop it exits 0,
# and so does this; greetd then starts the desktop.
#
# If it fails instead (a crash, a GPU it can't draw on), this falls back, in order, to the Skald login screen under
# cage (rikkiti-greeter-skald), then to tuigreet on the console — never to nothing: a machine nobody can log into is
# the one thing a login screen mustn't be.
#
# The system's keyboard layout (/etc/default/keyboard, written by the installer) is the XKB default, so the password
# is typed in the chosen layout; the pointer is the session's (no visible change of cursor at login).
[ -r /etc/default/keyboard ] && . /etc/default/keyboard
export XKB_DEFAULT_LAYOUT="${XKBLAYOUT:-us}" XKB_DEFAULT_MODEL="${XKBMODEL:-pc105}"
export XKB_DEFAULT_VARIANT="${XKBVARIANT:-}" XKB_DEFAULT_OPTIONS="${XKBOPTIONS:-}"
export XCURSOR_THEME="${XCURSOR_THEME:-Breeze_Light}" XCURSOR_SIZE="${XCURSOR_SIZE:-24}"
# Safe-graphics recovery boot (docs/47 Layer D): software rendering, so the login screen itself comes up even when
# hardware GL/Vulkan is broken — otherwise the recovery session couldn't be reached.
if grep -q 'rikkiti.safe=1' /proc/cmdline 2>/dev/null; then
	export WLR_RENDERER_ALLOW_SOFTWARE=1 LIBGL_ALWAYS_SOFTWARE=1
fi

say() { echo "rikkiti-greeter-session: $*" >&2; }

# The console under the login screen (its VT) is what shows between the login screen going and the desktop coming
# (the login screen fades to black, the desktop fades in): keep it black — no blinking cursor, nothing written on it.
# What the login screen and its compositor say goes to the journal (journalctl -t rikkiti-login) instead.
printf '\033[?25l\033[2J\033[H' 2>/dev/null || true
LOG=""
command -v systemd-cat >/dev/null 2>&1 && LOG="systemd-cat -t rikkiti-login"

# The packaged programs, by their full paths: a stray older copy earlier in PATH (a hand-installed rikkiti-comp in
# /usr/local/bin that knows no --login) would start a whole desktop as greetd's user, with no login screen on it.
COMP=/usr/bin/rikkiti-comp GREETER=/usr/bin/rikkiti-greeter OLD=/usr/bin/rikkiti-greeter-skald
# tries CMD…: runs it, and again if it fails within a few seconds of starting, for up to five tries. Started the moment
# a session ends, the screen can still be held by it ("Could not take device: Device or resource busy"): on
# 2026-10-26, after the desktop's compositor ended, both login screens failed within that second and tuigreet came up.
# One that ran a while and then failed has really failed, and isn't tried again.
tries() {
	n=0
	while :; do
		t0=$(date +%s)
		"$@" && return 0
		status=$?
		n=$((n + 1))
		if [ $(( $(date +%s) - t0 )) -ge 3 ] || [ "$n" -ge 5 ]; then return "$status"; fi
		say "it ended with $status at once — again in a second (the screen may still be the last session's)"
		sleep 1
	done
}
if [ -x "$COMP" ] && [ -x "$GREETER" ]; then
	tries $LOG "$COMP" --login -- "$GREETER"
	status=$?
	[ "$status" = 0 ] && exit 0
	say "the login screen ended with $status — the Skald one instead"
fi
if [ -x /usr/bin/cage ] && [ -x "$OLD" ]; then
	tries $LOG /usr/bin/cage -s -- "$OLD" && exit 0
	say "the Skald login screen failed too — tuigreet"
fi
if [ -x /usr/bin/tuigreet ]; then
	printf '\033[?25h' 2>/dev/null || true # a text login wants its cursor
	exec /usr/bin/tuigreet --time --remember --remember-session --sessions /usr/share/wayland-sessions --greeting 'Rikkiti OS'
fi
say "no login screen could start"
exit 1
