#!/usr/bin/env bash set -euo pipefail # Idempotent daemonized launcher for the emulate.dev Google emulator + portless # proxy. Shared host-wide across every agent worktree. Logs go to # ~/.autumn-emulate.log, PID at ~/.autumn-emulate.pid. ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" SEED="$ROOT/emulate.config.yaml" LOG="$HOME/.autumn-emulate.log" PID_FILE="$HOME/.autumn-emulate.pid" reachable() { curl -sf -o /dev/null --max-time 1 "https://google.emulate.localhost/.well-known/openid-configuration" } if reachable; then echo "[emulate] already reachable at https://google.emulate.localhost" exit 0 fi if ! command -v bunx >/dev/null 2>&1; then echo "[emulate] bunx not found; install bun" >&2 exit 1 fi if ! command -v portless >/dev/null 2>&1; then echo "[emulate] installing portless globally via bun" bun install -g portless >/dev/null fi if ! command -v emulate >/dev/null 2>&1; then echo "[emulate] installing emulate globally via bun" bun install -g emulate >/dev/null fi if ! curl -sf -o /dev/null --max-time 1 -k "https://localhost" 2>/dev/null; then echo "[emulate] starting portless proxy (may prompt for sudo for port 443)" portless proxy start fi # Clean up any stale pid file pointing at a dead process. if [[ -f "$PID_FILE" ]]; then if ! kill -0 "$(cat "$PID_FILE" 2>/dev/null)" 2>/dev/null; then rm -f "$PID_FILE" fi fi echo "[emulate] spawning emulate daemon → $LOG" nohup emulate --portless --service google --seed "$SEED" \ >"$LOG" 2>&1 "$PID_FILE" disown # Block briefly until the emulator is actually serving so callers can race. for _ in $(seq 1 30); do if reachable; then echo "[emulate] ready at https://google.emulate.localhost (pid $(cat "$PID_FILE"))" exit 0 fi sleep 0.3 done echo "[emulate] emulator failed to come up within 9s; tail of $LOG:" >&2 tail -n 20 "$LOG" >&2 || true exit 1