#!/bin/bash
set -euo pipefail
usage() {
    echo 'Usage: tuxenix-intel-gaming-setup [--check|--update]'
    echo '--check runs read-only graphics diagnostics in your desktop session.'
    echo '--update installs the latest Intel profile components using sudo txpk.'
    echo 'Package confirmations are accepted for these named components; sudo may ask for your password.'
}
[[ $# -le 1 ]] || { usage >&2; exit 64; }
case ${1:---check} in
    --help) usage; exit 0 ;;
    --check) exec /usr/bin/tuxenix-gaming-check ;;
    --update) ;;
    *) usage >&2; exit 64 ;;
esac
(( EUID != 0 )) || { echo 'Run this command as your normal user, without sudo.' >&2; exit 64; }
# txpk currently treats an installed dependency as satisfied without upgrading
# it. Request the components as roots so existing Steam installs update too.
for package in intel-gpu-firmware mesa lib32-mesa vulkan-loader lib32-vulkan-loader \
    multilib-tests linux-tuxenix-multilib; do
    echo "Updating Intel gaming component: $package"
    printf 'y\n' | sudo txpk --install "$package"
done
echo 'Graphics package updates completed.'
if [[ -x /usr/bin/tuxenix-steam-setup ]]; then
    echo 'Next: run tuxenix-steam-setup --activate-kernel and follow its reboot instruction.'
else
    echo 'Activate the installed multilib kernel using your bootloader before testing.'
fi
echo 'After booting the intended kernel, run tuxenix-gaming-check in your desktop terminal.'
