#!/bin/sh
# nfmc-session — session wrapper for graphical-login (display-manager) launches.
#
# Point an xsessions .desktop Exec= at this script (see nfmc.desktop). It
# guarantees the per-user X session environment is set up before the window
# manager starts, then hands off to nfmc (NeoFlux Media Center).
#
# neoflux itself does NOT source .xprofile or run xrdb (by design — the WM reads
# the *effects* of the session env, e.g. the RESOURCE_MANAGER property, but does
# not own session plumbing). Most display managers run an Xsession wrapper that
# already does this, but it is not universal; doing it here makes neoflux behave
# consistently regardless of how thorough the DM is. Both operations are
# idempotent, so re-doing work the DM already did is harmless.
#
# This mirrors the role startfluxbox plays for Fluxbox.

# Make the user's session environment available (PATH, locale, $EDITOR, ssh/gpg
# agents, autostarted env, etc.). Sourced, not executed, so exported vars stick.
if [ -r "$HOME/.xprofile" ]; then
    . "$HOME/.xprofile"
fi

# Populate the X RESOURCE_MANAGER property so neoflux's Xresources overlay (and
# every other X app that honors Xresources) sees the user's settings.
if command -v xrdb >/dev/null 2>&1; then
    if [ -r "$HOME/.Xresources" ]; then
        xrdb -merge "$HOME/.Xresources"
    elif [ -r "$HOME/.Xdefaults" ]; then
        xrdb -merge "$HOME/.Xdefaults"
    fi
fi

# The window manager to run. (Packaging rewrites this line to an absolute path.)
WM=/usr/local/bin/nfmc

# Run the window manager (no --replace: from a DM there is no prior WM), and
# start it again if it crashes.
#
# This script, not the window manager, is the session: when it exits, the
# display manager ends the login and closes every program. A crashed window
# manager leaves its windows intact (the X server hands them back to the root
# window), so starting a new one picks them all up again — the same thing a
# Restart does — and nothing is lost.
#
# Only a crash is retried: a Rust panic (exit status 101) or death by a fatal
# signal. Quit (0), an error exit such as "cannot connect to the X server" or
# "another window manager is running" (1), and bad arguments (2) end the
# session as before. Three crashes inside a minute also end it, so a window
# manager that cannot stay up does not spin forever.
MAX_CRASHES=3
CRASH_WINDOW=60

wm_pid=
stopping=
# The display manager ends a session by signalling this script: pass it on.
trap 'stopping=1; [ -n "$wm_pid" ] && kill "$wm_pid" 2>/dev/null' HUP INT TERM

crashes=0
window_start=$(date +%s)
while :; do
    "$WM" "$@" &
    wm_pid=$!
    wait "$wm_pid"
    status=$?
    if [ -n "$stopping" ]; then
        wait "$wm_pid" 2>/dev/null
        exit 0
    fi
    case $status in
        # panic; then 128+signal: ILL, ABRT, BUS, FPE, KILL (OOM killer), SEGV
        101 | 132 | 134 | 135 | 136 | 137 | 139) ;;
        *) exit "$status" ;;
    esac

    now=$(date +%s)
    if [ $((now - window_start)) -gt "$CRASH_WINDOW" ]; then
        crashes=0
        window_start=$now
    fi
    crashes=$((crashes + 1))
    if [ "$crashes" -ge "$MAX_CRASHES" ]; then
        echo "nfmc-session: $WM crashed $crashes times in ${CRASH_WINDOW}s (last status $status); giving up" >&2
        exit "$status"
    fi
    echo "nfmc-session: $WM crashed (status $status); starting it again" >&2
    # As on a Restart: adopt the existing windows, and do not run the startup
    # script a second time (its programs are still running).
    NEOFLUX_RESTART=1
    export NEOFLUX_RESTART
    sleep 1
done
