#!/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.
set -e

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

done_marker="${HOME}/.config/intergen-welcome/done"
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
python3 /usr/libexec/intergen-welcome/intergen-welcome.py "$@"
rc=$?
if [ "${rc}" -eq 0 ] ; then
    mkdir -p "$(dirname "${done_marker}")"
    touch "${done_marker}"
fi
exit "${rc}"
