#!/bin/bash
# intergen-welcome launcher.
#
# Default invocation (no flags) is the first-login autostart path: it
# checks the per-user done-marker and exits 0 if already complete.
#
# Invocation with --force / -f bypasses the marker check (the app-grid
# .desktop entry uses this) so users can re-run the welcomer any time
# via the "InterGenOS Welcome" application icon.
#
# On a clean completion (rc=0) the wrapper writes the marker regardless
# of how it was invoked, so a user who runs the wizard via the app grid
# before the autostart fires still gets first-run behavior on subsequent
# logins.
#
# The one exception is the re-arm sentinel, handled at the bottom: the
# AI-assistant page writes it when the user starts an NVIDIA driver
# install, because that install requires a reboot and the page tells the
# user the welcomer will be back afterwards so they can finish setting
# InterGen up. The marker has to be cleared HERE rather than by the app,
# precisely because of the rule above -- this wrapper writes the marker
# on every clean exit, so a marker deleted inside the app would be
# re-created moments later and the page's promise would be false.
set -e

force_run=0
case "$1" in
    -f|--force)
        force_run=1
        shift
        ;;
esac

done_marker="${HOME}/.config/intergen-welcome/done"
rearm_marker="${HOME}/.config/intergen-welcome/rearm"

# Clear a re-arm request left over from a previous run before deciding
# anything. The request is written when a driver install starts and is
# meant to be consumed by the SAME run that wrote it; one can survive if
# that run never reached its own bookkeeping -- the user rebooted from
# the terminal without closing the window, or abandoned the install at
# the password prompt. A stale request costs one extra welcomer
# appearance, which is self-correcting, but it is still a request that
# no longer corresponds to anything, so it does not get to persist.
rm -f "${rearm_marker}"

if [ "${force_run}" -eq 0 ] && [ -e "${done_marker}" ] ; then
    exit 0
fi

# Live-ISO guard. The marker check above relies on a persistent $HOME
# (per-user state survives across logins). The live ISO has $HOME on an
# overlay-on-squashfs upper layer that's ephemeral -- every "Try
# InterGenOS" boot looks like a first login, so the marker check passes
# and the welcomer re-fires every time. The welcomer is for installed
# systems ONLY; skip on ANY ISO session. The ISO UKIs set igos.mode=
# {live,install-gui,install-tui} (installer also recognizes igos.mode=try);
# an INSTALLED system carries NO igos.mode (Forge writes root=+verity, no
# igos.mode). So the gate is "skip if igos.mode= present at all" -- not just
# 'live'. GBC001.3-rebuild fix mirroring intergen-firstboot's extension.js:
# the old 'live'-only check let the welcomer's gate miss install-gui (there
# Forge's Hidden=true autostart shadow still suppressed it, but defense-in-
# depth wants the guard correct on its own).
#
# --force/-f STILL bypasses this guard so the app-grid invocation works
# in the unlikely event a user runs the welcomer manually from a live
# session (force_run==1 path skips both gates). Read-failure on
# /proc/cmdline falls through to "no guard match" -- a missed skip on
# an unusual host is preferable to refusing to launch on an installed
# system where /proc/cmdline is unreadable for some reason.
if [ "${force_run}" -eq 0 ] && grep -qE 'igos\.mode=' /proc/cmdline 2>/dev/null ; then
    exit 0
fi

# Force GSK's cairo (software) renderer. GTK4's default GL renderer goes
# through Mesa, which on virtualized GPUs (QXL, virtio-vga) tries the ZINK
# Vulkan-on-GL backend and segfaults on context creation when the host
# can't expose a real Vulkan device:
#   "MESA: error: ZINK: failed to choose pdev"
#   -> "libEGL warning: egl: failed to create dri2 screen"
#   -> Aborted (core dumped)
# Symptom: the welcomer launches, "Next" works (no context recreation
# needed), but the first click that triggers a re-paint with a fresh GL
# context (e.g., a theme preview row, an extension toggle) core-dumps
# the process. Cairo is software-only — slower, but never touches the
# GL stack. For a 7-page wizard that's launched once on first login
# (and rarely re-run from the app grid), the perceptual cost on real
# hardware is negligible.
export GSK_RENDERER=cairo
# set -e is lifted across the app run so a non-zero exit does not abort the
# wrapper before the marker bookkeeping below. Without this, an app that
# crashed or was killed would skip the re-arm check entirely and the driver
# install's "you'll be shown this again after reboot" promise would quietly
# not happen. The rc is still propagated unchanged at the end.
set +e
python3 /usr/libexec/intergen-welcome/intergen-welcome.py "$@"
rc=$?
set -e

# Re-arm: a driver install was started from the AI-assistant page, which
# needs a reboot to take effect. Clear the done-marker instead of writing
# it, so the next login shows the welcomer again and the user can finish
# setting InterGen up on hardware the system can finally read.
if [ -e "${rearm_marker}" ] ; then
    rm -f "${rearm_marker}" "${done_marker}"
    exit "${rc}"
fi

if [ "${rc}" -eq 0 ] ; then
    mkdir -p "$(dirname "${done_marker}")"
    touch "${done_marker}"
fi
exit "${rc}"
