#!/bin/bash
set -euo pipefail

fail() { printf 'Steam setup: %s\n' "$*" >&2; exit 1; }
mode=${1:---check}
[[ $# -le 1 ]] || fail 'Use tuxenix-steam-setup [--check|--activate-kernel|--launch-check].'
case $mode in
    --check|--activate-kernel|--launch-check) ;;
    --help)
        echo 'Usage: tuxenix-steam-setup [--check|--activate-kernel]'
        echo 'Run as your desktop user after sudo txpk --install steam.'
        echo '--check reports readiness; --activate-kernel uses sudo for the guarded EFI helper.'
        echo 'Exit 10 means save work and reboot; this command never reboots automatically.'
        exit 0 ;;
    *) fail 'Unknown option. Use --help.' ;;
esac
(( EUID != 0 )) || fail 'Run this from your normal desktop account, without sudo.'
[[ -x /usr/lib/steam/bin_steam.sh && -x /usr/bin/steam ]] ||
    fail 'Steam files are missing. Install with sudo txpk --install steam and wait for success.'

shopt -s nullglob
kernels=(/boot/vmlinuz-*-tuxenix-multilib)
running=$(uname -r)
if [[ $mode == --activate-kernel ]]; then
    [[ ${#kernels[@]} == 1 ]] || fail 'Expected exactly one installed multilib kernel in /boot. Check linux-tuxenix-multilib.'
    expected=${kernels[0]##*/vmlinuz-}
    if [[ $running != "$expected" ]]; then
        [[ -x /usr/sbin/tuxenix-update-boot-kernel ]] || fail 'The kernel helper is missing. Check the Steam package installation.'
        echo "Running kernel: $running; installed multilib kernel: $expected"
        sudo /usr/sbin/tuxenix-update-boot-kernel || fail 'Kernel activation failed. Read the helper error above before retrying.'
        echo 'REBOOT REQUIRED: save your work, then run sudo dinit-shutdown -r.'
        echo 'After reboot, log in as your normal user, run startx, then steam.'
        exit 10
    fi
    echo 'The installed multilib kernel is already running; no kernel update is needed.'
fi

[[ -x /lib/ld-linux.so.2 ]] || fail 'The 32-bit loader is missing. Check the lib32-glibc installation.'
if ! /lib/ld-linux.so.2 --help >/dev/null 2>&1; then
    fail '32-bit execution failed. Run tuxenix-steam-setup --activate-kernel, then reboot when instructed.'
fi
[[ -d /dev/fd && -e /dev/stdin && -e /dev/stdout && -e /dev/stderr ]] ||
    fail 'Standard stream aliases are missing. Check multilib-runtime and reboot.'
command -v unshare >/dev/null || fail 'unshare is missing. Check the util-linux installation.'
if ! unshare --user --map-root-user true 2>/dev/null; then
    fail 'User namespaces are unavailable. Run tuxenix-steam-setup --activate-kernel; if already running that kernel, check namespace restrictions.'
fi
[[ -n ${DISPLAY:-} ]] || fail 'No X11 desktop was found. Log in as your normal user, run startx, then launch Steam from its terminal or menu.'
command -v xdpyinfo >/dev/null || fail 'The display checker is missing. Install it with sudo txpk --install xdpyinfo.'
if ! timeout 10 xdpyinfo -display "$DISPLAY" >/dev/null 2>&1; then
    fail 'Cannot access your X11 desktop. Save work, log out of IceWM, log in as your normal user, run startx without sudo, then steam without sudo. No X access settings were changed.'
fi
if [[ $mode != --launch-check ]]; then
    if [[ ${#kernels[@]} == 1 && $running != "${kernels[0]##*/vmlinuz-}" ]]; then
        echo 'A different multilib kernel is installed. Use --activate-kernel to deploy it and receive reboot instructions.'
    fi
    if ! curl --silent --show-error --head --fail --location --connect-timeout 10 --max-time 20 \
        https://store.steampowered.com/ >/dev/null 2>&1; then
        echo 'WARNING: Steam network access could not be verified. First launch needs internet; connect with sudo nmtui.'
    fi
    if ! locale -a 2>/dev/null | grep -Eiq '^en_US\.(utf8|utf-8)$'; then
        echo 'WARNING: en_US.UTF-8 is not generated. Steam may warn about the locale; see README.tuxenix.'
    fi
    echo 'READY: kernel features, 32-bit execution, stream aliases, and X11 access passed. Run steam as this user.'
fi
