#!/usr/bin/env bash
# One background task: boot -> mission -> bind the transform -> fly the pilot,
# with periodic screenshots so the outcome has visual evidence and not just a log.
#
# It is one task on purpose: the display and the emulator both die on their own
# every few minutes in this container, so nothing may depend on surviving
# between tool calls.
set -u
export HOME=/sylph-home/re SDL_AUDIODRIVER=dummy DISPLAY=:98
export PYTHONPATH=/sylph-home/.local/lib/python3.12/site-packages
SD="$(cd "$(dirname "$0")" && pwd)"
SECS="${1:-240}"
TAG="${2:-pilot}"
SHOTS=/sylph-home/re/shots
CFG=/tmp/nav-live.json

"$SD/launch_mission.sh" fly || { echo "BOOT FAILED"; exit 1; }
python3 "$SD/entities2.py" self 0x130 "$CFG" || { echo "BIND FAILED"; exit 1; }
echo "--- config: $(cat "$CFG")"
python3 "$SD/own_state.py" "$CFG" 3 "/tmp/$TAG-own.json"

( for i in $(seq 1 12); do sleep 25; screenshot "$SHOTS/$TAG-$i.png" >/dev/null 2>&1; done ) &
SHOTTER=$!
python3 "$SD/pilot.py" "$CFG" "$SECS"
kill $SHOTTER 2>/dev/null
screenshot "$SHOTS/$TAG-end.png" >/dev/null 2>&1
echo "SESSION DONE"
