#!/usr/bin/env bash
# Install Hermes Agent on Teleo VPS (CAX31, ARM64, Ubuntu)
# Run as: teleo user
# Prereqs: Python 3.11+, Node.js 22+, git
set -euo pipefail

HERMES_HOME="$HOME/.hermes"
OPENROUTER_KEY_FILE="/opt/teleo-eval/secrets/openrouter-key"

echo "=== Hermes Agent Install for Teleo VPS ==="

# 1. Check prereqs
echo "[1/6] Checking prerequisites..."
python3 --version || { echo "ERROR: Python 3.11+ required"; exit 1; }
node --version || { echo "ERROR: Node.js 22+ required"; exit 1; }
git --version || { echo "ERROR: git required"; exit 1; }

# 2. Install Hermes
echo "[2/6] Installing Hermes Agent..."
if command -v hermes &>/dev/null; then
    echo "Hermes already installed, upgrading..."
    pip3 install --upgrade hermes-agent
else
    curl -fsSL https://raw.githubusercontent.com/NousResearch/hermes-agent/main/scripts/install.sh | bash
    # Source the updated PATH
    export PATH="$HOME/.local/bin:$PATH"
fi

# 3. Create config directory
echo "[3/6] Setting up config..."
mkdir -p "$HERMES_HOME"

# 4. Write .env with OpenRouter key (read from existing pipeline secret)
if [ -f "$OPENROUTER_KEY_FILE" ]; then
    OPENROUTER_KEY=$(cat "$OPENROUTER_KEY_FILE")
    cat > "$HERMES_HOME/.env" << EOF
OPENROUTER_API_KEY=${OPENROUTER_KEY}
EOF
    chmod 600 "$HERMES_HOME/.env"
    echo "  OpenRouter key loaded from pipeline secrets"
else
    echo "  WARNING: No OpenRouter key found at $OPENROUTER_KEY_FILE"
    echo "  You'll need to manually add OPENROUTER_API_KEY to $HERMES_HOME/.env"
fi

# 5. Write config.yaml
echo "[4/6] Writing config.yaml..."
cat > "$HERMES_HOME/config.yaml" << 'EOF'
# Hermes Agent config — Teleo VPS
model:
  provider: openrouter
  default: anthropic/claude-sonnet-4-6
  smart_routing: true
  smart_routing_model: google/gemini-2.5-flash

terminal:
  backend: native

memory:
  enabled: true
  search: sqlite_fts5
  nudge_interval: 0

skills:
  creation_nudge_interval: 0

tools:
  web_search: true
  browser: true
  file_ops: true
  terminal: true
  vision: false
  image_gen: false
  tts: false

gateway:
  telegram:
    enabled: false  # Enable after setting BOT_TOKEN below
    # bot_token: "YOUR_TELEGRAM_BOT_TOKEN"
EOF

# 6. Write SOUL.md
echo "[5/6] Writing SOUL.md..."
cat > "$HERMES_HOME/SOUL.md" << 'EOF'
You are Leo, the LivingIP assistant running on the Teleo VPS.

Address each Telegram participant by the exact visible sender handle. When the
sender is @m3taversal, call them m3taversal exactly. Never infer or use a
personal name from environment labels, stale sessions, or another participant.

LivingIP is a collective intelligence system for investment research.

You help with:
- Email triage and drafting (when Gmail is connected)
- Calendar management
- Web research and summarization
- Quick tasks and reminders
- Requests from authorized operators

Style: Direct, concise, no fluff. Skip explanations of basic concepts. When
uncertain, say so rather than guessing.

You are NOT part of the LivingIP pipeline. You're a separate personal
assistant. Don't try to interact with Forgejo, pipeline.db, or teleo-codex
unless an authorized operator specifically asks.
EOF

echo "[6/6] Done!"
echo ""
echo "=== Next Steps ==="
echo "1. Test:  hermes 'hello, what model are you using?'"
echo "2. Gmail: hermes setup google-workspace  (needs OAuth credentials)"
echo "3. Telegram: Create bot via @BotFather, add token to config.yaml,"
echo "   then: hermes gateway start"
echo "4. Cron:  hermes cron add '0 8 * * *' 'Check my calendar and summarize today'"
echo ""
echo "Config: $HERMES_HOME/config.yaml"
echo "Memory: $HERMES_HOME/MEMORY.md"
echo "Skills: $HERMES_HOME/skills/"
EOF
