#!/bin/bash
# steamify-install <target root> <user> [<skip ids>] [gamescope|desktop]:
# runs Steamify's default setup (minus what the installer's Steam Machine
# page leaves out, where the boot page says) for
# the new user inside the installed system (Calamares, after the users and
# services steps). Steamify refuses root and asks sudo per command, so the
# user gets a NOPASSWD rule for this run only, named to be read last: sudo
# takes the last matching rule, and a later %wheel rule would ask again. Never fails the install: a
# problem leaves the log and Steamify can be run again from the desktop.
root="$1" user="$2"
# The pages' ids, comma-separated; the "Everything" entry is an empty id.
skip="$(tr ',' '\n' <<< "${3:-}" | grep -v '^$' | paste -sd, -)"
boot="${4:-gamescope}"
# Steamify 2.7.0 takes the items to turn on (--options), not the ones to
# leave out: every item of the default setup minus the skipped ones (the
# power-off fix goes with Steam Machine support). Until the Steamify page
# passes the "on" list itself.
options=""
for id in gaming theme glyphs single launcher notify vram cec machine poweroff; do
    [[ ",$skip," == *",$id,"* ]] && continue
    [[ "$id" == poweroff && ",$skip," == *",machine,"* ]] && continue
    options+="${options:+,}$id"
done
log="$root/var/log/steamify-install.log"
rule="$root/etc/sudoers.d/zz-steamify-install"
[[ -d "$root" && -n "$user" ]] || exit 0

install -Dm755 /usr/local/share/steamify/steamify.sh "$root/var/tmp/steamify.sh"
printf '%s ALL=(ALL) NOPASSWD: ALL\n' "$user" > "$rule"
chmod 440 "$rule"
arch-chroot "$root" runuser -u "$user" -- env -i HOME="/home/$user" USER="$user" LOGNAME="$user" \
    PATH=/usr/local/bin:/usr/bin TERM=dumb LANG=C.UTF-8 \
    bash -c 'cd ~ && bash /var/tmp/steamify.sh --defaults --options "$0" --boot "$1" < /dev/null' "$options" "$boot" > "$log" 2>&1
echo "exit: $?" >> "$log"
rm -f "$rule" "$root/var/tmp/steamify.sh"
exit 0
