#!/usr/bin/env bash
set -u
repo=$1
data=$2
out=$3
cd "$repo"
printf '\033[?25l'
exec > >(tee "$out/probe.txt") 2>&1
printf '$ git rev-parse HEAD\n'
git rev-parse HEAD
printf '\n$ ss -H -ltn (web and backend ports)\n'
ss -H -ltn '( sport = :18543 or sport = :18544 )'
printf '\n$ t3 pair --base-dir <fresh-data> --tailscale\n'
node apps/server/src/bin.ts pair --base-dir "$data" --tailscale > "$out/pair-private.log" 2>&1
printf 'Exit status: %s\n' "$?"
printf '\n$ tailscale serve status --json | jq handlers\n'
tailscale serve status --json | jq '[.Web[]?.Handlers[]?]'
printf '\n$ curl --noproxy "*" http://[::1]:18543/\n'
curl --noproxy '*' -sS -o /dev/null -w 'HTTP %{http_code}\n' 'http://[::1]:18543/'
printf '\n$ curl --noproxy "*" http://127.0.0.1:18543/\n'
curl --noproxy '*' -sS -o /dev/null -w 'HTTP %{http_code}\n' 'http://127.0.0.1:18543/'
printf '\n$ curl --noproxy "*" "$PAIRING_URL"  # private URL omitted\n'
PAIRING_URL=$(sed -n 's/^Pairing URL: //p' "$out/pair-private.log")
curl --noproxy '*' --max-time 25 -sS -o "$out/https-body.txt" -w 'HTTPS pairing HTTP %{http_code}\n' "$PAIRING_URL"
printf '\nDone. Hostname, pairing token and local paths withheld.\n'
touch "$out/done"
sleep 8
