#!/usr/bin/env bash
set -euo pipefail
NAME="${1:?Usage: burnboard-setup.sh NAME [SERVER_URL]}"; SERVER_URL="${2:-https://burnboard-public.vercel.app}"
command -v node >/dev/null || { echo "Node.js is required." >&2; exit 1; }
INSTALL_DIR="$HOME/.burnboard"; CONFIG="$INSTALL_DIR/config.json"; mkdir -p "$INSTALL_DIR" "$HOME/.claude"
printf '[1/5] Preparing BurnBoard profile...\n'
if [[ -f "$CONFIG" ]] && node -e 'const c=JSON.parse(require("fs").readFileSync(process.argv[1]));process.exit(c.serverUrl===process.argv[2]&&c.token?0:1)' "$CONFIG" "$SERVER_URL"; then
  TOKEN="$(node -e 'console.log(JSON.parse(require("fs").readFileSync(process.argv[1])).token)' "$CONFIG")"
  PROFILE="$(node -e 'const c=JSON.parse(require("fs").readFileSync(process.argv[1]));console.log((c.profileUrl||(`/builder/${process.argv[2].toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"")||"builder"}`)).replace(/^\/intern\//,"/builder/"))' "$CONFIG" "$NAME")"
else
  ENROLLMENT="$(curl -fsS -X POST "$SERVER_URL/api/enroll" -H 'content-type: application/json' --data "$(node -e 'console.log(JSON.stringify({firstName:process.argv[1]}))' "$NAME")")"
  TOKEN="$(node -e 'console.log(JSON.parse(process.argv[1]).token)' "$ENROLLMENT")"; PROFILE="$(node -e 'console.log(JSON.parse(process.argv[1]).profileUrl)' "$ENROLLMENT")"
fi
printf '[2/5] Downloading tracker...\n'; curl -fsS "$SERVER_URL/burnboard-agent.mjs" -o "$INSTALL_DIR/agent.mjs"
node -e 'require("fs").writeFileSync(process.argv[1],JSON.stringify({serverUrl:process.argv[2],token:process.argv[3],profileUrl:process.argv[4]},null,2)+"\n")' "$CONFIG" "$SERVER_URL" "$TOKEN" "$PROFILE"
node - "$HOME/.claude/settings.json" <<'NODE'
const fs=require('fs'),file=process.argv[2],current=fs.existsSync(file)?JSON.parse(fs.readFileSync(file,'utf8')):{};current.env=current.env||{};for(const key of ['CLAUDE_CODE_ENABLE_TELEMETRY','OTEL_METRICS_EXPORTER','OTEL_LOGS_EXPORTER','OTEL_EXPORTER_OTLP_METRICS_PROTOCOL','OTEL_EXPORTER_OTLP_METRICS_ENDPOINT','OTEL_EXPORTER_OTLP_METRICS_HEADERS','OTEL_METRIC_EXPORT_INTERVAL','OTEL_METRICS_INCLUDE_SESSION_ID'])delete current.env[key];fs.writeFileSync(file,JSON.stringify(current,null,2)+"\n");
NODE
printf '[3/5] Scheduling automatic sync...\n'; (crontab -l 2>/dev/null|grep -Ev 'burnboard/agent.mjs|burnboard-cli@latest update'||true;echo "*/5 * * * * $(command -v node) $INSTALL_DIR/agent.mjs >/dev/null 2>&1";echo "0 4 * * * $(command -v npx) --yes --prefer-online burnboard-cli@latest update >/dev/null 2>&1";echo "@reboot sleep 180 && $(command -v npx) --yes --prefer-online burnboard-cli@latest update >/dev/null 2>&1")|crontab -
if [[ "$(uname -s)" == "Darwin" ]] && ! pgrep -x Antigravity >/dev/null 2>&1 && [[ -d "/Applications/Antigravity.app" ]]; then open -a Antigravity; sleep 8; fi
printf '[4/5] Counting and uploading local history'; OUT="$INSTALL_DIR/setup-sync.out"; ERR="$INSTALL_DIR/setup-sync.err"; node "$INSTALL_DIR/agent.mjs" >"$OUT" 2>"$ERR" & PID=$!
while kill -0 "$PID" 2>/dev/null; do printf '.'; sleep 1; done
if ! wait "$PID"; then printf '\n'; cat "$ERR" >&2; rm -f "$OUT" "$ERR"; exit 1; fi
printf '\n[5/5] Complete\n'; cat "$OUT"; rm -f "$OUT" "$ERR"
echo "Burnboard is connected: $SERVER_URL$PROFILE"; echo "Codex, Claude Code, Cursor, and both supported Antigravity installations sync every 5 minutes. Burnboard checks for updates daily."
