#!/usr/bin/env fish

set REPO /users/a2andrad/backgammon_bot
set EXPECTED_COMMIT 336d7eca593cdf08461c917cbc213b8c4cee4668
set BATCH_ID sage_vs_gnubg_show10_20260717T182417Z
set RUN_ROOT /users/a2andrad/www/matches/plan1_dualseat_tests

if test -f ~/bg-env/bin/activate.fish
    source ~/bg-env/bin/activate.fish
end

cd $REPO; or exit 1

set ACTUAL_COMMIT (git rev-parse HEAD)

echo "repo=$REPO"
echo "expected_commit=$EXPECTED_COMMIT"
echo "actual_commit=$ACTUAL_COMMIT"
echo "branch="(git branch --show-current)
echo "batch_id=$BATCH_ID"
echo "started_utc="(date -u +%Y-%m-%dT%H:%M:%SZ)

if test "$ACTUAL_COMMIT" != "$EXPECTED_COMMIT"
    echo "FATAL: repository commit does not match approved commit"
    exit 1
end

for n in (seq -w 1 10)
    set pair_id {$BATCH_ID}_pair{$n}
    set pair_dir $RUN_ROOT/mirrored-7pt-$pair_id

    if test -e $pair_dir
        echo "FATAL: refusing to reuse existing pair directory"
        echo "$pair_dir"
        exit 2
    end

    echo "PAIR_START $pair_id "(date -u +%Y-%m-%dT%H:%M:%SZ)

    fish plan1_scripts/3_referee_7pt_dualseat_pair_safe.fish $pair_id
    set run_status $status

    echo "PAIR_DONE $pair_id status=$run_status "(date -u +%Y-%m-%dT%H:%M:%SZ)

    if test $run_status -ne 0
        echo "BATCH_STOPPED pair=$pair_id status=$run_status"
        exit $run_status
    end
end

echo "batch_complete_utc="(date -u +%Y-%m-%dT%H:%M:%SZ)
