#!/bin/bash
# SSID launcher - starts the Go server and opens the browser
DIR="$(cd "$(dirname "$0")" && pwd)"

# Use a persistent data directory in Application Support
DATA_DIR="$HOME/Library/Application Support/SSID"
mkdir -p "$DATA_DIR"
cd "$DATA_DIR"

# Start the server in background
"$DIR/ssid" &
SERVER_PID=$!

# Wait for the server to be ready (up to 15 seconds)
for i in $(seq 1 30); do
    if curl -sk https://localhost:5443 > /dev/null 2>&1; then
        break
    fi
    sleep 0.5
done

# Open in default browser
open https://localhost:5443

# Wait for the server process (keeps the app running in dock)
wait $SERVER_PID
