#!/usr/bin/env bash
# Autostart Steam Big Picture Mode for the kid's account only.
# Other accounts (parent, RDP admin sessions) get a normal desktop.
#
# Deliberately does NOT use bazzite-steam-bpm: that wrapper has a race
# (waits on Steam PIDs that may already be gone) plus a bash `! cmd`
# status-clobbering bug, producing a zenity "ERROR: code=0; This is
# strange..." popup at login whenever Steam lingered from a previous
# session. We do the same quit-first dance robustly instead.
# Which account gets the console treatment; override the default after a
# rename with: echo 'CONSOLE_KID_USER=newname' > /etc/bc250-console.conf
[ -r /etc/bc250-console.conf ] && . /etc/bc250-console.conf
[ "$(id -un)" = "${CONSOLE_KID_USER:-kid}" ] || exit 0

if pgrep -u "$(id -u)" -f 'steam\.sh|ubuntu12_32/steam' >/dev/null 2>&1; then
    /usr/bin/steam +quit >/dev/null 2>&1 || true
    for _ in $(seq 1 30); do
        pgrep -u "$(id -u)" -f 'steam\.sh|ubuntu12_32/steam' >/dev/null 2>&1 || break
        sleep 1
    done
fi

exec /usr/bin/bazzite-steam steam://open/bigpicture
