#!/usr/bin/env bash
# ReversePuck Steam Deck forwarder launcher (uv-based; nothing installed system-wide).
#
# Add as a NON-STEAM GAME:
#   Target:      /home/deck/…/ReversePuck/ReversePuck
#   Start In:    /home/deck/…/ReversePuck/    (the directory this script is in)
# Then, in that shortcut's gear -> Properties -> Controller, set "Disable Steam Input" so Steam doesn't
# fight us for the built-in pad while we grab it.
#
# Dependencies are pulled by uv on first run (pyserial + pygame + pyusb -- prebuilt wheels; pyusb uses
# the system libusb-1.0.so). uv also provisions its own Python, so nothing touches the read-only rootfs.
#
# USB-level detach (DeckSource) needs access to the controller's usbfs node, and the CDC link needs
# /dev/ttyACM*. Run `sudo ./setup.sh` once per boot to open both (see README). Without it, fall back to
# `./ReversePuck --evdev` (no USB detach; Steam/QAM won't forward, trackpads are lower fidelity).
set -euo pipefail
cd "$(dirname "$(readlink -f "$0")")"

find_uv() {
	local uv
	uv="$(command -v uv || true)"
	[ -n "$uv" ] || { [ -x "$HOME/.local/bin/uv" ] && uv="$HOME/.local/bin/uv"; }
	[ -x "$uv" ] && { printf '%s' "$uv"; return 0; }
	return 1
}

UV="$(find_uv || true)"
if [ -z "$UV" ]; then
	# Bootstrap uv on first run. SteamOS' rootfs is read-only, but the official installer drops a
	# self-contained binary under ~/.local/bin (writable), so nothing touches the system.
	echo "uv not found — installing it (one-time) via https://astral.sh/uv …"
	if command -v curl >/dev/null 2>&1; then
		curl -LsSf https://astral.sh/uv/install.sh | sh
	elif command -v wget >/dev/null 2>&1; then
		wget -qO- https://astral.sh/uv/install.sh | sh
	else
		echo "Cannot bootstrap uv: neither curl nor wget is available." >&2
		echo "Install uv manually: https://docs.astral.sh/uv/" >&2
		exit 1
	fi
	UV="$(find_uv || true)"
	[ -n "$UV" ] || { echo "uv install completed but the binary was not found on PATH or in ~/.local/bin." >&2; exit 1; }
	echo "uv installed: $UV"
fi

exec "$UV" run --with pyserial --with pygame --with pyusb python openctrl.py "$@"
