#!/usr/bin/env bash
# The live user's login shell. On the first console, where it is logged in at boot, it opens
# BlueRune's installer: its app full screen on Hyprland (bluerune-live-installer), or the text
# installer when Hyprland or the app can't start, or when the boot menu's text entry was
# chosen (bluerune.text). Anywhere else, and after the installer, it is bash.

if [[ $(tty) != /dev/tty1 || -n ${BLUERUNE_LIVE_STARTED:-} ]]; then
    exec bash --login "$@"
fi
export BLUERUNE_LIVE_STARTED=1

status_file=${XDG_RUNTIME_DIR:-/tmp}/bluerune-installer.status
rm -f -- "$status_file"

text_installer() {
    printf '\n%s\n\n' "$1"
    sudo bluerune-install --interactive
}

if grep -qw bluerune.text /proc/cmdline; then
    text_installer "BlueRune's text installer."
elif command -v start-hyprland >/dev/null; then
    start-hyprland -- --config /etc/bluerune-live/hyprland.lua >"${XDG_RUNTIME_DIR:-/tmp}/hyprland.log" 2>&1
    status=$(cat -- "$status_file" 2>/dev/null || echo none)
    case $status in
        0) printf '\nThe installer is closed. Run bluerune-live-shell to open it again, or\n"sudo bluerune-install --interactive" for the text installer.\n\n' ;;
        none) text_installer "The installer's screen couldn't start (Hyprland: ${XDG_RUNTIME_DIR:-/tmp}/hyprland.log); here is the text installer." ;;
        *) text_installer "The installer's app couldn't start (it ended with $status); here is the text installer." ;;
    esac
else
    text_installer "Hyprland isn't on this system; here is the text installer."
fi
exec bash --login
