#!/bin/bash
# Emergency stop for K2.5 - breaks the API key in config
# Use when runaway loop is burning money

echo "🚨 EMERGENCY K2 STOP"

# 1. Clear all k2-browser sessions
rm -rf /home/johan/.clawdbot/agents/k2-browser/sessions/*.jsonl
echo "✓ Sessions cleared"

# 2. Disable the k2-browser agent by removing from config
# (backup first)
cp ~/.clawdbot/clawdbot.json ~/.clawdbot/clawdbot.json.bak.$(date +%s)

# Remove k2-browser from agents list using jq
if command -v jq &> /dev/null; then
    jq '.agents.list = [.agents.list[] | select(.id != "k2-browser")]' \
        ~/.clawdbot/clawdbot.json > ~/.clawdbot/clawdbot.json.tmp \
        && mv ~/.clawdbot/clawdbot.json.tmp ~/.clawdbot/clawdbot.json
    echo "✓ k2-browser agent removed from config"
else
    echo "⚠ jq not installed, manually remove k2-browser from ~/.clawdbot/clawdbot.json"
fi

# 3. Restart gateway
systemctl --user restart clawdbot-gateway
echo "✓ Gateway restarted"

# 4. Optionally: invalidate Fireworks key by renaming env var
# Uncomment to actually break the key:
# sed -i 's/FIREWORKS_API_KEY=/FIREWORKS_API_KEY_DISABLED=/' ~/.clawdbot/clawdbot.json
# echo "✓ Fireworks API key disabled"

echo ""
echo "K2.5 stopped. To re-enable:"
echo "  1. Restore config: cp ~/.clawdbot/clawdbot.json.bak.* ~/.clawdbot/clawdbot.json"
echo "  2. Restart: systemctl --user restart clawdbot-gateway"
