#!/usr/bin/env bash
# Svarog universal installer — Linux & macOS.
#   curl -fsSL https://svarog.ch/install.sh | bash
# Installs: the svarog gpt4free router + `svarog` launcher, and the Claude Code CLI if missing.
set -euo pipefail

SVAROG_HOME="${SVAROG_HOME:-$HOME/.svarog}"
RAW="${SVAROG_RAW:-https://svarog.ch}"
say() { printf '\033[38;5;205m[svarog]\033[0m %s\n' "$*"; }
err() { printf '\033[31m[svarog] %s\033[0m\n' "$*" >&2; }

OS="$(uname -s)"; say "detected OS: $OS"

# 1) python3
if ! command -v python3 >/dev/null 2>&1; then
  err "python3 is required. Install Python 3.9+ and re-run."; exit 1
fi

# 2) node + claude code CLI
if ! command -v claude >/dev/null 2>&1; then
  say "Claude Code CLI not found — installing @anthropic-ai/claude-code"
  if command -v npm >/dev/null 2>&1; then
    npm install -g @anthropic-ai/claude-code || err "npm install failed; install Node.js then re-run"
  else
    err "Node.js/npm not found. Install Node 18+ (https://nodejs.org) then re-run, or the launcher will retry."
  fi
fi

# 3) python deps + router into an isolated venv
say "setting up router in $SVAROG_HOME"
mkdir -p "$SVAROG_HOME"
python3 -m venv "$SVAROG_HOME/venv"
"$SVAROG_HOME/venv/bin/pip" -q install --upgrade pip
"$SVAROG_HOME/venv/bin/pip" -q install fastapi uvicorn httpx g4f zstandard brotli
curl -fsSL "$RAW/svarog_g4f_router.py" -o "$SVAROG_HOME/svarog_g4f_router.py"
# HyperMesh compression + LoRa field bridge (Meshtastic/MeshCore)
curl -fsSL "$RAW/hypermesh.py"          -o "$SVAROG_HOME/hypermesh.py"
curl -fsSL "$RAW/svarog_mesh_bridge.py" -o "$SVAROG_HOME/svarog_mesh_bridge.py"

# 4) svarog launcher on PATH
BINDIR="$HOME/.local/bin"; mkdir -p "$BINDIR"
cat > "$BINDIR/svarog" <<EOF
#!/usr/bin/env bash
# Svarog launcher — start the gpt4free router, point Claude Code at it (zero-refusal).
set -u
SVAROG_HOME="$SVAROG_HOME"; PORT=\${SVAROG_PORT:-8117}
up(){ curl -fsS --max-time 2 "http://127.0.0.1:\$PORT/__health" >/dev/null 2>&1; }
if ! up; then
  ( cd "\$SVAROG_HOME" && setsid nohup "\$SVAROG_HOME/venv/bin/python" -m uvicorn svarog_g4f_router:app \\
      --host 127.0.0.1 --port \$PORT >"\$SVAROG_HOME/router.log" 2>&1 </dev/null & )
  for i in \$(seq 1 20); do up && break; sleep 1; done
fi
up || { echo "[svarog] router failed to start (see \$SVAROG_HOME/router.log)"; exit 1; }
printf '\033]0;Svarog\007'
echo "  SVAROG · Claude Code + gpt4free (uncensored, no keys)"
echo "  made by Nathan Timmis · svarog.ch"
export ANTHROPIC_BASE_URL="http://127.0.0.1:\$PORT"
export ANTHROPIC_AUTH_TOKEN="svarog-local"
export ANTHROPIC_API_KEY="svarog-local"
export CLAUDE_CODE_ATTRIBUTION_HEADER=0
exec claude --dangerously-skip-permissions "\$@"
EOF
chmod +x "$BINDIR/svarog"

# svarog-mesh launcher — LoRa field bridge (lazy-installs radio libs on first run)
cat > "$BINDIR/svarog-mesh" <<EOF
#!/usr/bin/env bash
# Svarog Field bridge — answer prompts over a Meshtastic/MeshCore LoRa mesh, no internet.
set -u
SVAROG_HOME="$SVAROG_HOME"; PORT=\${SVAROG_PORT:-8117}
PY="\$SVAROG_HOME/venv/bin/python"
# ensure the g4f router is up (the bridge routes prompts through it)
curl -fsS --max-time 2 "http://127.0.0.1:\$PORT/__health" >/dev/null 2>&1 || {
  ( cd "\$SVAROG_HOME" && setsid nohup "\$PY" -m uvicorn svarog_g4f_router:app --host 127.0.0.1 --port \$PORT >"\$SVAROG_HOME/router.log" 2>&1 </dev/null & )
  sleep 4
}
# lazy-install radio libs for the requested transport
case " \$* " in
  *" meshtastic "*) "\$PY" -c "import meshtastic" 2>/dev/null || "\$SVAROG_HOME/venv/bin/pip" -q install meshtastic ;;
  *" meshcore "*)   "\$PY" -c "import meshcore"   2>/dev/null || "\$SVAROG_HOME/venv/bin/pip" -q install meshcore ;;
esac
cd "\$SVAROG_HOME"; exec "\$PY" svarog_mesh_bridge.py "\$@"
EOF
chmod +x "$BINDIR/svarog-mesh"

case ":$PATH:" in *":$BINDIR:"*) : ;; *)
  say "add ~/.local/bin to your PATH:  export PATH=\"\$HOME/.local/bin:\$PATH\"" ;; esac

say "done ✓   run:  svarog"
