#!/bin/bash
# IR-66 Linux gate leg — clean worktree at PR #161 sha, both attach tests 3x.
set -u
SHA=d04b922dfcae0b2d663e4a33b25446e2a241dbe1
REPO="$HOME/projects/spt-core/spt-core"
WT="$REPO/.worktrees/gate-ir66-d04b922"
cd "$REPO" || { echo 9 > /tmp/GIR66L_setup.exit; exit 9; }
git fetch origin "$SHA" > /tmp/GIR66L_fetch.raw 2>&1
echo $? > /tmp/GIR66L_fetch.exit
if [ ! -d "$WT" ]; then
  git worktree add --detach "$WT" "$SHA" > /tmp/GIR66L_wt.raw 2>&1
  echo $? > /tmp/GIR66L_wt.exit
else
  echo reused > /tmp/GIR66L_wt.exit
fi
cd "$WT" || { echo 9 > /tmp/GIR66L_setup.exit; exit 9; }
if [ -x "$REPO/target/debug/xtask" ]; then
  "$REPO/target/debug/xtask" pool-claim --pool target --label gate-ir66-161-linux > GIR66L_claim.raw 2>&1
  echo $? > GIR66L_claim.exit
else
  echo no-xtask > GIR66L_claim.exit
fi
FILTER="test(loopback_attach_to_a_prepopulated_ring_delivers_without_deadlock) or test(cross_node_cold_attach_to_alt_screen_gets_clean_repaint)"
for i in 1 2 3; do
  cargo nextest run -p spt-daemon -E "$FILTER" > "GIR66L_run$i.raw" 2>&1
  echo $? > "GIR66L_run$i.exit"
done
if [ -x "$REPO/target/debug/xtask" ]; then
  "$REPO/target/debug/xtask" pool-release --pool target > GIR66L_release.raw 2>&1
  echo $? > GIR66L_release.exit
fi
echo DONE > GIR66L.done
