#!/bin/sh
# Starchaser owns crash notices for this window.
export QS_DISABLE_CRASH_HANDLER=1 STARCHASER_QML_APP=welcome
# summary: Open or manage the first-start welcome window.
# Supply the private session proof to Starchaser's own reminders.
system_notice() {
    token="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/starchaser-notifications/token"
    if [ -r "$token" ]; then starchaser notify --credential-fd=3 "$@" 3<"$token"
    else starchaser notify "$@"; fi
}
# starchaser welcome: the Starchaser Linux welcome (make it yours: look, coding agent, apps, security choices).
#   starchaser welcome               open it
#   starchaser welcome --first-run   at login (hyprland.lua): only the first time after installing; finishes the theme
#                                    first (the installer sets it up without a running desktop)
for a in "$@"; do case "$a" in -h|--help|help)
    echo "starchaser welcome: open the welcome (first-run setup) again. --first-run: only if it hasn't been done yet."
    exit 0 ;;
esac; done
case "$#:${1:-}" in
    0:|1:--first-run) ;;
    *) bad=$1; [ "$1" = --first-run ] && bad=$2
       echo "starchaser welcome: unknown option '$bad'" >&2
       echo 'Usage: starchaser welcome [--first-run]' >&2
       echo "Run 'starchaser welcome --help' for more help." >&2
       exit 2 ;;
esac
# Opened from the taskbar: drop what its keeper set for the taskbar alone (drawing without the GPU after full graphics
# memory, the driver's implicit sync), so this window keeps the GPU. A QT_QUICK_BACKEND set by hand stays. QA r179.
if [ "${STARCHASER_KEEPER_PART:-}" = cockpit ]; then unset QT_QUICK_BACKEND __NV_DISABLE_EXPLICIT_SYNC STARCHASER_KEEPER_PART; fi
DONE=${XDG_STATE_HOME:-$HOME/.local/state}/starchaser/welcome-done
# Retire old cross-sign-in setup queues. They held no credentials; downloads are now explicit retries.
# Keep one plain notice for people who selected optional apps before the change.
retire_waiting() {
    pending=no
    for marker in welcome-waiting welcome-waiting-later; do
        [ ! -s "${DONE%/*}/$marker" ] || pending=yes
        rm -f "${DONE%/*}/$marker"
    done
    if [ "$pending" = yes ]; then
        system_notice -a Starchaser 'Earlier setup downloads were not finished' \
            'Your browser is included with Starchaser. Add any remaining apps in Settings > Software.' >/dev/null 2>&1 || true
    fi
}
UNFINISHED=${DONE%/*}/welcome-unfinished
# M11: retire older sign-in reminder markers; Getting started reports what is set up.
unfinished_note() {
    [ -e "$UNFINISHED" ] || return 0
    # Standard accounts skip machine setup in the welcome. Drop an older reminder
    # rather than sending them back to pages they cannot use.
    case " $(id -Gn) " in
        *" wheel "*) ;;
        *) rm -f "$UNFINISHED"; return 0 ;;
    esac
    # M11: the live Getting started card replaces sign-in nags. Retire the older marker quietly.
    rm -f "$UNFINISHED"

    return 0
}
if [ "${1:-}" = --first-run ]; then
    if [ -f "${DONE%/*}/fleet-install.json" ]; then
        starchaser fleet stick join > "${DONE%/*}/fleet-install-result.json" 2>/dev/null &
    fi
    if [ -e "$DONE" ]; then retire_waiting; unfinished_note; exit 0; fi
    starchaser theme reapply >/dev/null 2>&1 || true
fi
# Opening the welcome again ends the desktop tour, whose card would otherwise cover setup controls.
timeout 1 starchaser tour stop >/dev/null 2>&1 </dev/null || true
# The welcome takes over from the Super+K / Super+Space menu: close it, or the welcome opens underneath it (the menu
# sits on the overlay layer and keeps the keyboard). After the first-run check, so a --first-run that does nothing
# leaves the menu alone. hide does nothing when the menu is closed; no menu running is fine too. QA r37.
# timeout: a menu that stopped answering must not keep the welcome from opening.
timeout 1 qs -p "$HOME/.config/quickshell/cockpit" ipc call -- menu hide >/dev/null 2>&1 </dev/null || true
# The same for the desktop's own pop-ups (picker, Quick settings, Power, top panel): Help opened under them.
timeout 1 qs -p "$HOME/.config/quickshell/cockpit" ipc call -- cockpit dismissTransient >/dev/null 2>&1 </dev/null || true
# The `--` matters: without it Quickshell reads the function name "show" as its own `ipc show` subcommand, prints
# the target's functions and exits 0, so nothing is sent and a minimized or hidden welcome stays where it is.
qs -p ~/.config/quickshell/welcome ipc call -- welcome show >/dev/null 2>&1 && exit 0
exec qs -p ~/.config/quickshell/welcome
