#!/bin/sh
# Starts the official Steam client (steam/, from Valve's steam-launcher
# package) in its gamepad UI, inside gamescope: the client and the games are
# X11 programs, gamescope gives them an X server and shows them as one
# Wayland window on the TV.
#
# Output: the TV's current mode. Games render at most at 1080p (an N100 can't
# do 4K) and gamescope scales them up.
APP="${SMARTFOX_APP_DIR:-$(cd "$(dirname "$0")/.." && pwd)}"

mode=$(wlr-randr 2>/dev/null | awk '/current/ {print $1; exit}')
W=${mode%x*}
H=${mode#*x}
case "$W:$H" in
    *[!0-9:]* | :* | *:) W=1920 H=1080 ;;
esac
GW=$W GH=$H
if [ "$H" -gt 1080 ]; then
    GW=$((W * 1080 / H))
    GH=1080
fi

GAMESCOPE=$(command -v gamescope || echo /usr/games/gamescope)
exec "$GAMESCOPE" -e -f -W "$W" -H "$H" -w "$GW" -h "$GH" -- "$APP/steam/steam" -gamepadui "$@"
