#!/usr/bin/env bash
# Waits for the iOS sim runtime, then builds, installs, boots and screenshots TetherApp.
set -euo pipefail
cd "$(dirname "$0")/TetherApp"
echo "waiting for iOS simulator runtime…"
until xcrun simctl list runtimes 2>/dev/null | grep -qi "iOS"; do sleep 30; done
echo "runtime ready: $(xcrun simctl list runtimes | grep -i iOS | tail -1)"
DEV=$(xcrun simctl create tether-test "iPhone 16 Pro" 2>/dev/null || \
      xcrun simctl list devices available | grep -i iphone | head -1 | grep -oE '[0-9A-F-]{36}')
xcrun simctl boot "$DEV" 2>/dev/null || true
open -a Simulator
xcodebuild build -scheme TetherApp -destination "id=$DEV" -derivedDataPath /tmp/tether-dd | tail -3
APP=$(find /tmp/tether-dd/Build/Products -name "TetherApp.app" -maxdepth 3 | head -1)
xcrun simctl install "$DEV" "$APP"
xcrun simctl launch "$DEV" io.pecord.tether
sleep 3
xcrun simctl io "$DEV" screenshot /tmp/tether-launch.png
echo "DONE device=$DEV shot=/tmp/tether-launch.png"
