#!/bin/bash
# Restart ONLY the shim python process, e.g. to pick up a code update.
# Unlike run.sh this does NOT touch cloudflared/tailscale — the announced
# public URL stays stable. Moves no money, opens no new exposure, installs
# nothing; the tunnel already exists.
set -u
DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$DIR"
PORT=8402

pkill -f "x402-shim/shim.py" 2>/dev/null && echo "old shim stopped" || echo "no shim running"
sleep 1
nohup python3 "$DIR/shim.py" >>"$DIR/shim.log" 2>&1 &
echo "shim pid $!"

ok=""
for i in $(seq 1 15); do
  sleep 1
  if curl -s -m 3 "http://127.0.0.1:$PORT/health" | grep -q '"ok": true'; then
    ok=1; break
  fi
done
[ -n "$ok" ] || { echo "SHIM FAILED"; tail -20 "$DIR/shim.log"; exit 1; }
curl -s -m 3 "http://127.0.0.1:$PORT/health"
echo
echo "restart done"
