#!/bin/bash
# local-setup.sh.example — copy to local-setup.sh and fill in YOUR values.
# local-setup.sh is gitignored; this example file is committed.
#
# Usage:
#   cp local-setup.sh.example local-setup.sh
#   # Edit: set YOUR_HOST, YOUR_USER below
#   chmod +x local-setup.sh
#   bash local-setup.sh

set -euo pipefail

# ── Fill these in ─────────────────────────────────────────────────────────
SSH_HOST="YOUR-MACHINE.tail02a1a0.ts.net"   # Tailscale hostname (or LAN IP)
SSH_USER="yourusername"
NTFY_TOPIC="codingAgentWaitingOnYou"        # change if you use a different topic
NTFY_USAGE_RESET_TOPIC="codexbarUsageResets" # subscribe here for reset-only alerts
REPO="$HOME/projects/CodexBar-ESP32"        # path to this repo
# ──────────────────────────────────────────────────────────────────────────

PLIST="$HOME/Library/LaunchAgents/com.codexbar.companion.plist"
LABEL="com.codexbar.companion"
GUI="gui/$(id -u)"

# ── 1. Reload companion launchd service ───────────────────────────────────
echo "==> Reloading companion service..."
launchctl bootout "$GUI/$LABEL" 2>/dev/null || true
launchctl load "$PLIST"
launchctl kickstart "$GUI/$LABEL"
sleep 1

# ── 2. Verify it's healthy ────────────────────────────────────────────────
echo "==> Health check..."
curl -sf http://127.0.0.1:7842/health && echo " OK" || echo " FAILED — check: tail -50 /tmp/codexbar-companion.log"

echo ""
echo "==> ntfy attention topic:    $NTFY_TOPIC"
echo "==> ntfy usage reset topic:  $NTFY_USAGE_RESET_TOPIC"
echo "==> SSH button:              ssh://$SSH_USER@$SSH_HOST"

# ── 3. tmux session ───────────────────────────────────────────────────────
# Run manually when you want a persistent session for agent work:
#
#   tmux new-session -A -s agents
#
# From iPhone (Termius/Blink via Tailscale):
#   ssh $SSH_USER@$SSH_HOST
#   tmux attach -t agents

echo ""
echo "==> To start/attach to a persistent tmux session:"
echo "    tmux new-session -A -s agents"
echo ""
echo "Done."
