#!/usr/bin/env bash
set -euo pipefail
# Start on this thread's terminal screen in the paired Android development app.
# Arguments: checkout isolated-T3-home server-port thread-json device-session output-parent
checkout=$1
proof_home=$2
proof_port=$3
thread_json=$4
device_session=$5
output_parent=$6
driver_dir=$(cd "$(dirname "$0")" && pwd)
run_dir="$output_parent/$(date -u +%Y%m%dT%H%M%SZ)"
mkdir -p "$run_dir"
terminal() { node "$driver_dir/terminal-driver.mjs" "$checkout" "$proof_home" "$proof_port" "$thread_json" "$@"; }
device() { agent-device "$@" --session "$device_session"; }
device press 'label="Navigate up"' --settle
terminal write $'\003'
terminal clear
terminal write $'printf \'Emitting CSI = c\\r\\n\\033[=cReady to reopen\\r\\n\'\r'
# The query is emitted while the native terminal view is unmounted, so no live reply occurs.
device record start "$run_dir/reopen.mp4" --hide-touches
device press 'label="Terminal"' --settle
device press 'text="Terminal 1"' --settle
device wait stable 500 10000
device screenshot "$run_dir/stable-1.png"
device wait 500
device screenshot "$run_dir/stable-2.png"
device record stop
printf 'Run saved: %s\n' "$run_dir"
