#!/usr/bin/env bash
# tx.sh: send a command file to tmux session 'amddiag', wait for completion marker, print captured output.
# Usage: tx.sh /path/to/cmdfile
set -u
SESS=amddiag
CMDFILE="$1"
MARK="__DONE_$$_$RANDOM__"
# Clear pane history
tmux send-keys -t "$SESS" "clear" Enter
sleep 0.3
# Source the command file then echo marker
tmux send-keys -t "$SESS" "bash $CMDFILE 2>&1; echo $MARK" Enter
# Wait for marker
for i in $(seq 1 60); do
  sleep 1
  if tmux capture-pane -p -t "$SESS" -S -2000 | grep -q "$MARK"; then
    break
  fi
done
tmux capture-pane -p -t "$SESS" -S -2000 | sed -n "/$MARK/q;p"
