#!/usr/bin/env bash
# Headless UBER perf capture for the Amiga (FS-UAE).
#
# Stages the built Amiga binaries + DATA into a scratch directory-HD
# with an s/startup-sequence that auto-runs Uber, boots A500/OCS from
# the real Kickstart on a private Xvfb display, polls the staged
# joeylog.txt until UBER prints its "press any key to exit" completion
# marker (or a wall-clock timeout), then kills the emulator and copies
# the log to build/amiga/bin/joeylog.txt for tools/uber-perf-table.
#
#   scripts/bench-amiga.sh              # default 150s timeout
#   TIMEOUT=240 scripts/bench-amiga.sh  # override
#
# Real Kickstart (toolchains/emulators/support/kickstart.rom) is
# required -- the HAL drives OCS directly and AROS is unreliable for it.

set -euo pipefail

repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
bin_dir=$repo/build/amiga/bin
support=$repo/toolchains/emulators/support
kickstart=$support/kickstart.rom
timeout_s=${TIMEOUT:-150}

if [[ ! -f $bin_dir/Uber ]]; then
    echo "Uber not built. Run 'make amiga' first." >&2
    exit 1
fi
if [[ ! -f $kickstart ]]; then
    echo "Kickstart ROM missing: $kickstart" >&2
    exit 1
fi

work=$(mktemp -d -t joeylib-amiga-bench.XXXXXX)
mkdir -p "$work/s"
find "$bin_dir" -maxdepth 1 -type f -executable -exec cp -t "$work/" {} +
if [[ -d "$bin_dir/DATA" ]]; then
    cp -r "$bin_dir/DATA" "$work/"
fi
# ':' anchors to the boot volume root. 32 KB task stack: the shell
# default (~4 KB) left UBER one deep call chain from overflow -- the
# Phase-1 sprite-compile path found it (emitter accumulator frame +
# vsnprintf underneath = nondeterministic wandering crashes).
printf 'stack 32768\n:Uber\n' > "$work/s/startup-sequence"

log=$work/joeylog.txt

display=:$((90 + RANDOM % 9))
Xvfb "$display" -screen 0 800x600x24 -nolisten tcp >/dev/null 2>&1 &
xvfb_pid=$!
sleep 0.4
cleanup() {
    kill "$fs_pid"   2>/dev/null || true
    kill "$xvfb_pid" 2>/dev/null || true
    wait 2>/dev/null || true
    rm -rf "$work"
}
trap cleanup EXIT

echo "bench-amiga: running UBER headless (timeout ${timeout_s}s)..." >&2
DISPLAY=$display fs-uae \
    --amiga_model=A500 \
    --fast_memory=2048 \
    --hard_drive_0="$work" \
    --hard_drive_0_label=JOEYLIB \
    --kickstart_file="$kickstart" \
    --automatic_input_grab=0 \
    --fullscreen=0 \
    --fade_out_duration=0 \
    >/tmp/bench-amiga-fsuae.log 2>&1 &
fs_pid=$!

waited=0
while (( waited < timeout_s )); do
    if [[ -f $log ]] && grep -q "press any key to exit" "$log" 2>/dev/null; then
        break
    fi
    sleep 2
    (( waited += 2 )) || true
done

if [[ ! -f $log ]]; then
    echo "bench-amiga: FAIL -- no joeylog.txt produced" >&2
    exit 1
fi

cp "$log" "$bin_dir/joeylog.txt"
ops=$(grep -c "ops/sec" "$log" || true)
done_marker=$(grep -c "press any key to exit" "$log" || true)
if (( done_marker == 0 )); then
    echo "bench-amiga: WARNING -- timed out before completion marker (partial capture)" >&2
fi
echo "bench-amiga: captured $ops op lines (complete=$done_marker) in ${waited}s" >&2
echo "  -> $bin_dir/joeylog.txt" >&2
grep "UBER:" "$log" | sed 's/ | hash=.*//' || true
