#!/usr/bin/env bash
#
# deckport install helper — FreeSpace 2 Open
# status: needs-test · engine: other · type: native
#
# Generated from freespace-2-open.toml on the deckport recipe site.
# Recipes describe how to CONFIGURE a game — never where to obtain it.
#
# Community guides for this game:
#   - Manually Installing FreeSpace 2 Open — FreeSpace Wiki: https://wiki.hard-light.net/index.php/Manually_Installing_FreeSpace_2_Open
#   - Fs2 Open on Linux / Acquiring the Game Data — FreeSpace Wiki: https://wiki.hard-light.net/index.php/Fs2_open_on_Linux/Acquiring_the_Game_Data
#   - Getting Started with Knossos — PXO: https://pxo.nottheeye.com/support/how-to/knossos
#   - Knossos.NET launcher for FreeSpace Open (video overview) — YouTube: https://www.youtube.com/watch?v=2cpMEhLgqV4
#   - Steam Deck — I will pay a nerd to get Knossos working (HLP forum thread): https://www.hard-light.net/forums/index.php?topic=98407.0
#   - Guide to portable FreeSpace 1 & 2 FSO installation — Steam discussions: https://steamcommunity.com/app/273620/discussions/0/1744518601281555506/
#
# ON YOUR STEAM DECK (Desktop Mode), with STEAM CLOSED:
#   1. Copy the game's whole folder into ~/Games/
#   2. Put the importer in your home folder:  ~/deckport.py
#   3. Run:  bash freespace-2-open.deckport.sh
#
set -euo pipefail

GAMES_DIR="${GAMES_DIR:-$HOME/Games}"
IMPORTER="${IMPORTER:-$HOME/deckport.py}"
TAG="${TAG:-Ported Games}"

GAME_TITLE="FreeSpace 2 Open"
BINARY="fs2_open"
GAME_TYPE="native"
REQUIRES=("root_fs2.vp" "smarty_fs2.vp" "sparky_fs2.vp" "stu_fs2.vp" "tango1_fs2.vp" "tango2_fs2.vp" "tango3_fs2.vp" "warble_fs2.vp")

say()  { printf '  %s\n' "$*"; }
die()  { printf 'error: %s\n' "$*" >&2; exit 1; }

printf '\n=== deckport: %s ===\n' "$GAME_TITLE"

[ -d "$GAMES_DIR" ] || die "drop directory $GAMES_DIR not found — create it and copy the game folder in"
[ -f "$IMPORTER" ]  || die "importer not found at $IMPORTER — copy deckport.py to your home folder"

# Refuse to clobber shortcuts.vdf while Steam is running (it rewrites on exit).
if pgrep -x steam >/dev/null 2>&1; then
  die "Steam is running — close it fully first, then re-run this script"
fi

# Locate the folder that holds the game binary.
GAME_DIR=""
if [ -n "$BINARY" ]; then
  GAME_DIR="$(find "$GAMES_DIR" -maxdepth 4 -name "$BINARY" -printf '%h\n' 2>/dev/null | head -n1 || true)"
fi
[ -n "$GAME_DIR" ] || die "could not find '$BINARY' under $GAMES_DIR — copy the game folder there first"
say "found game folder: $GAME_DIR"

# Required data files must sit beside the binary.
for f in "${REQUIRES[@]:-}"; do
  [ -z "$f" ] && continue
  if [ -e "$GAME_DIR/$f" ]; then say "ok: $f present"
  else die "missing required file: $f (must sit beside the binary)"; fi
done

# Write this game's recipe to a temp folder and hand it to the importer with
# --recipes, so it applies the PINNED binary, launch options and Proton version
# (not just auto-detect). Cleaned up on exit. Needs Python 3.11+ for TOML; if
# that's missing the importer says so and still registers the shortcut.
RECIPE_DIR="$(mktemp -d)"
trap 'rm -rf "$RECIPE_DIR"' EXIT
cat > "$RECIPE_DIR/freespace-2-open.toml" <<'DECKPORT_RECIPE_TOML'
[game]
title = "FreeSpace 2 Open"
slug = "freespace-2-open"
type = "native"
engine = "other"
aliases = ["FreeSpace 2 Open", "FSO", "Freespace 2 Open", "fs2_open", "FreeSpace Open"]
steam_appid = "273620"

[launch]
binary = "fs2_open"
launch_options = ""
requires_files = ["root_fs2.vp", "smarty_fs2.vp", "sparky_fs2.vp", "stu_fs2.vp", "tango1_fs2.vp", "tango2_fs2.vp", "tango3_fs2.vp", "warble_fs2.vp"]

[proton]
version = ""
winetricks = []

[meta]
status = "needs-test"
DECKPORT_RECIPE_TOML

say "registering with Steam (applying recipe)…"
python3 "$IMPORTER" --games-dir "$GAMES_DIR" --recipes "$RECIPE_DIR" --tag "$TAG"

say "native Linux title — no Proton needed."

printf '\nDone. Reopen Steam / return to Game Mode — look under "%s".\n\n' "$TAG"
