#!/bin/bash
# ingame-auto.sh [DELAY=45] [SECONDS=120] -- start BEFORE launching the game via godot-nsys.
# Waits (15 min max) for the 'ingame' nsys session, waits DELAY s (loading + getting to the
# spot), records SECONDS with CPU sampling, then saves while the game keeps running.
OUT="${PERF_OUT:-$HOME/.cache/hyper-tps-perf}"
mkdir -p "$OUT"
DELAY="${1:-45}"; SECS="${2:-120}"
for _ in $(seq 1 900); do nsys sessions list 2>/dev/null | command grep -q ' ingame' && break; sleep 1; done
nsys sessions list 2>/dev/null | command grep -q ' ingame' || { echo "no game session appeared"; exit 1; }
echo "$(date +%T) game session seen; recording starts in $DELAY s"; sleep "$DELAY"
F="$OUT/ingame_$(date +%H%M%S)"
nsys start --session=ingame -s process-tree --cpuctxsw=process-tree -o "$F" -f true || exit 1
echo "$(date +%T) RECORDING -> $F ($SECS s)"; sleep "$SECS"
nsys stop --session=ingame; echo "$(date +%T) SAVED"
nsys export -t sqlite -f true -o "$F.sqlite" "$F.nsys-rep" > /dev/null 2>&1 && echo "sqlite=$F.sqlite"