#!/bin/bash
# NexusOS boot-time branding: the Plymouth boot splash and the LightDM greeter
# (which is also what you see on lock -- light-locker just switches to it).
#
# Both are system-level and need root, which is why this is a standalone script
# rather than part of assets/themes/install-theme.sh.
#
# Usage: sudo bin/boot-branding.sh [--uninstall]
set -euo pipefail

NEXUS_ROOT="${NEXUS_ROOT:-$(cd "$(dirname "$(realpath "$0")")/.." && pwd)}"

# Plymouth. A real copy is placed under /usr/share/plymouth/themes -- NOT a
# symlink into /home. The stock initramfs plymouth hook copies the theme path
# verbatim, so a symlink into an unmounted /home dangles at early boot and
# Plymouth falls back to text mode. That is what
# assets/boot/initramfs-hook-my-custom-logo exists to work around; with a real
# copy the hook is unnecessary.
SRC="$NEXUS_ROOT/assets/boot/my-custom-logo"
DEST=/usr/share/plymouth/themes/my-custom-logo
THEME="$DEST/my-custom-logo.plymouth"

# Greeter. slick-greeter cannot lay out a custom login screen the way the
# NexusOS-QML SDDM theme does, so the logo and wordmark are baked into the
# background image instead and the greeter's own centered login box lands under
# them. The SVG is used as-is: gdk-pixbuf's SVG loader is installed, so there is
# nothing to rasterize. Copied out of the repo because the greeter runs as user
# `lightdm` and the user home is 0750, so nothing in here is readable to it.
GREETER_ART="$NEXUS_ROOT/assets/themes/greeter/greeter-background.svg"
GREETER_DIR=/usr/share/nexusos
GREETER_CONF=/etc/lightdm/slick-greeter.conf

# SDDM. Its greeter is a QML app, so unlike slick-greeter it can draw the real
# layout (logo, wordmark, login box, clock) -- this is the theme the design was
# written for. Installed whenever sddm is present, regardless of which display
# manager is currently active, so switching between the two needs no re-run.
SDDM_SRC="$NEXUS_ROOT/assets/themes/KDE/sddm/NexusOS-QML"
SDDM_DEST=/usr/share/sddm/themes/NexusOS-QML
SDDM_CONF=/etc/sddm.conf.d/nexusos.conf

[ "$(id -u)" -eq 0 ] || { echo "run with sudo"; exit 1; }

if [ "${1:-}" = "--uninstall" ]; then
	update-alternatives --remove default.plymouth "$THEME" 2>/dev/null || true
	rm -rf "$DEST" "$GREETER_DIR" "$GREETER_CONF" "$SDDM_DEST" "$SDDM_CONF"
	command -v update-initramfs >/dev/null && update-initramfs -u
	echo "reverted to the distro defaults"
	exit 0
fi

# --- Plymouth boot splash -----------------------------------------------------
if [ ! -d "$SRC" ]; then
	echo "missing theme source: $SRC -- skipping boot splash"
elif ! command -v update-initramfs >/dev/null || [ ! -d /usr/share/plymouth/themes ]; then
	echo "no Plymouth on this box -- skipping boot splash"
# update-initramfs is slow, and a restore runs this every time. Bail out when the
# installed copy already matches the repo and the alternative already points here.
elif diff -rq "$SRC" "$DEST" >/dev/null 2>&1 &&
     [ "$(readlink -f /usr/share/plymouth/themes/default.plymouth)" = "$THEME" ]; then
	echo "boot splash already installed"
else
	rm -rf "$DEST"
	cp -aL "$SRC" "$DEST"
	chown -R root:root "$DEST"
	chmod -R a+rX,go-w "$DEST"

	update-alternatives --install /usr/share/plymouth/themes/default.plymouth \
		default.plymouth "$THEME" 300
	update-alternatives --set default.plymouth "$THEME"
	update-initramfs -u
	echo "boot splash installed"
fi

# --- LightDM greeter (login + lock) -------------------------------------------
# Only slick-greeter is configured here; it's what Mint/XFCE ships and it reads
# this file directly. theme-name is deliberately left alone -- the NexusOS GTK
# theme lives in ~/.themes, which the lightdm user cannot read, so pointing at it
# would just fall back silently. Colors below are the NexusOS-QML palette
# (theme.conf: bgDark).
if [ ! -f "$GREETER_ART" ]; then
	echo "missing $GREETER_ART -- skipping greeter"
elif ! command -v slick-greeter >/dev/null; then
	echo "no slick-greeter on this box -- skipping greeter"
else
	install -D -m 644 -o root -g root "$GREETER_ART" "$GREETER_DIR/greeter-background.svg"
	cat > "$GREETER_CONF" <<EOF
[Greeter]
background=$GREETER_DIR/greeter-background.svg
background-color=#0a0e1a
draw-user-backgrounds=false
draw-grid=false
content-align=center
stretch-background-across-monitors=false
EOF
	chmod 644 "$GREETER_CONF"
	echo "greeter background installed"
fi

# --- SDDM greeter -------------------------------------------------------------
if [ ! -d "$SDDM_SRC" ]; then
	echo "missing $SDDM_SRC -- skipping SDDM theme"
elif [ ! -d /usr/share/sddm/themes ]; then
	echo "no SDDM on this box -- skipping SDDM theme"
else
	rm -rf "$SDDM_DEST"
	cp -aL "$SDDM_SRC" "$SDDM_DEST"
	chown -R root:root "$SDDM_DEST"
	chmod -R a+rX,go-w "$SDDM_DEST"
	install -d -m 755 /etc/sddm.conf.d
	cat > "$SDDM_CONF" <<EOF
[Theme]
Current=NexusOS-QML
EOF
	chmod 644 "$SDDM_CONF"
	echo "SDDM theme installed"
fi

echo "done -- reboot to see it."
