#!/usr/bin/env bash
# SDDM supplies this ordinary-user X11 session and its Xauthority cookie.
# Only the existing launcher may request installer privileges.
set -euo pipefail

fail() { printf 'Meowven installer session: %s\n' "$1" >&2; exit 1; }

(( EUID != 0 )) || fail 'Start this session as the live desktop user, not root.'
[[ -f /usr/lib/meowven/live-setup ]] || fail 'This session is available only on the Meowven live image.'
[[ ${XDG_SESSION_TYPE:-} == x11 && -n ${DISPLAY:-} ]] || fail 'Select Meowven installer (X11) at the live login screen.'
[[ -x /usr/bin/openbox ]] || fail 'The Openbox window manager is missing.'
[[ -x /usr/bin/meowven-install ]] || fail 'The Meowven installer launcher is missing.'

window_manager_pid=''
cleanup() {
    if [[ -n $window_manager_pid ]]; then
        kill "$window_manager_pid" 2>/dev/null || true
        wait "$window_manager_pid" 2>/dev/null || true
    fi
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM

/usr/bin/openbox --sm-disable &
window_manager_pid=$!
# Opening the wizard does not execute partitioning jobs. Closing or cancelling
# it ends this session and returns to the SDDM login screen.
status=0
/usr/bin/meowven-install || status=$?
exit "$status"
