#!/bin/sh
# Prism Launcher (Minecraft). Installed to /usr/local/bin/prism-launch.
#
# Prism rather than the official launcher: it runs offline once an account has been
# authenticated, it manages several instances/modpacks side by side, and it is packaged
# on Flathub, so it comes from the same channel as Spotify here and Steam Link on the
# thin client. 0400-flatpak-apps.hook.chroot installs it.
#
# THIS RUNS THROUGH STEAM, NOT BESIDE IT
# --------------------------------------
# Called with no arguments, this does not start Prism directly — it asks Steam to start
# it, via the non-Steam-game shortcut that /usr/local/bin/steam-shortcut-prism
# registers. That indirection is the entire point: a Prism launched by Steam runs inside
# the Steam Runtime with Steam Input active, so the Steam Controller API is present for
# it. Gamepads arrive as configurable Steam controllers with per-game bindings, the
# overlay works, gyro and back-buttons on Deck-style pads work, and Minecraft appears in
# Big Picture's library like any other title instead of being a hole you fall out of the
# UI into. Launched directly, Minecraft sees a raw evdev pad and none of that exists.
#
# `--steam` is what Steam passes back in when it runs the shortcut (it is the shortcut's
# LaunchOptions), and it is how this script knows not to bounce the request back to
# Steam a second time. Without that flag the two would call each other forever.
#
# FALLBACKS, IN ORDER: if Steam is not installed, or nobody has logged into it yet (so
# there is no shortcut and no game id), this runs Prism directly. That is a working
# Minecraft with worse controller support, which is the right outcome — better than
# refusing to start on a box where somebody has not signed into Steam.
#
# Java: the image ships openjdk-21-jre and openjdk-8-jre, and the hook grants the
# sandbox read access to /usr/lib/jvm, so Prism's auto-detection finds both without
# needing to download a runtime on first launch. 21 covers current Minecraft, 8 covers
# anything before 1.17 — which is most of what a modpack older than a couple of years
# actually wants.
#
# Login is interactive and on-device the first time (a Microsoft account is required —
# Mojang's device-code flow, so it is a short code typed on a phone rather than a
# password typed with a gamepad). The Flatpak's own persistent data directory
# (~/.var/app/org.prismlauncher.PrismLauncher) keeps that across restarts, and nothing
# here reseeds it — unlike the kiosk Firefox profiles, staying logged in is the point.
#
# Instances live under ~/Games if that is where you put them; see the README on
# mounting a real disk there, since this is the other thing on this box (with the Steam
# library) that will not fit in a live session's RAM overlay.
set -eu

GAME_ID_FILE="${HOME:-/home/$(id -un)}/.local/state/steamtv/prism-gameid"
DIRECT=""

# Strip the marker so it is never passed on to Prism itself, and keep any real
# arguments (an instance name, a .mrpack to import) for the direct launch below.
if [ "${1:-}" = "--steam" ]; then
  DIRECT="yes"
  shift
fi

run_direct() {
  if ! flatpak info org.prismlauncher.PrismLauncher >/dev/null 2>&1; then
    echo "prism-launch: Prism Launcher is not installed. Install it with:" >&2
    echo "  flatpak install -y flathub org.prismlauncher.PrismLauncher" >&2
    exit 1
  fi
  exec flatpak run org.prismlauncher.PrismLauncher "$@"
}

# Steam is already the parent — run the real thing.
if [ -n "$DIRECT" ]; then
  run_direct "$@"
fi

# Arguments mean somebody is opening a specific instance or file from a shell; hand
# that straight to Prism rather than through a Steam URL that cannot carry it.
if [ -n "${1:-}" ]; then
  run_direct "$@"
fi

if ! command -v steam >/dev/null 2>&1; then
  echo "prism-launch: Steam is not installed; launching Prism directly (no Steam Input)."
  run_direct
fi

if [ ! -r "$GAME_ID_FILE" ]; then
  # steam-shortcut-prism writes this on every session, so its absence means that script
  # has never run — not that Steam is unusable.
  echo "prism-launch: no Steam shortcut registered yet; launching Prism directly."
  echo "  Run /usr/local/bin/steam-shortcut-prism (with Steam closed) to fix that."
  run_direct
fi

GAME_ID="$(head -n 1 "$GAME_ID_FILE" | tr -dc '0-9')"

if [ -z "$GAME_ID" ]; then
  echo "prism-launch: $GAME_ID_FILE is malformed; launching Prism directly." >&2
  run_direct
fi

# `steam -applaunch` does not work for non-Steam shortcuts — the rungameid URL is the
# only handle Steam exposes for them. A running client picks this up immediately; a
# stopped one starts first and then launches it.
exec steam "steam://rungameid/${GAME_ID}"
