#!/usr/bin/env bash
# SuperAgentSkill × Hermes installer
# Adds the SuperAgentSkill MCP server to your Hermes config idempotently.
# Homepage: https://superagentskill.com

set -euo pipefail

BLUE='\033[0;34m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

info()  { printf "${BLUE}â${NC} %s\n" "$*"; }
ok()    { printf "${GREEN}â${NC} %s\n" "$*"; }
warn()  { printf "${YELLOW}!${NC} %s\n" "$*"; }
fail()  { printf "${RED}â${NC} %s\n" "$*" >&2; exit 1; }

CONFIG_DIR="${HERMES_HOME:-$HOME/.hermes}"
CONFIG_FILE="$CONFIG_DIR/config.yaml"
ENDPOINT="https://superagentskill.com/api/public/mcp"

info "SuperAgentSkill Ã Hermes installer"
info "Config: $CONFIG_FILE"

mkdir -p "$CONFIG_DIR"

if [ ! -f "$CONFIG_FILE" ]; then
  info "Creating new config.yaml"
  cat > "$CONFIG_FILE" <<EOF
mcp_servers:
  superagent-skill:
    transport: http
    url: $ENDPOINT
    description: SuperAgentSkill â review, upload and discover AI skills.
EOF
  ok "Hermes config created with superagent-skill installed."
  info "Run 'hermes mcp list' to confirm the tools are available."
  exit 0
fi

if grep -qE '^[[:space:]]*superagent-skill:' "$CONFIG_FILE"; then
  ok "superagent-skill is already configured in $CONFIG_FILE"
  info "Nothing to do. Run 'hermes mcp list' to confirm."
  exit 0
fi

STAMP="$(date +%Y%m%d-%H%M%S)"
BACKUP="$CONFIG_FILE.bak.$STAMP"
cp "$CONFIG_FILE" "$BACKUP"
ok "Backed up existing config to $BACKUP"

if grep -qE '^[[:space:]]*mcp_servers[[:space:]]*:' "$CONFIG_FILE"; then
  # Existing mcp_servers block: append the entry under it.
  # We do the safe thing and append at the very end of the block by tacking
  # the entry to the end of the file with a top-level mcp_servers extension.
  # (Hermes merges repeated mcp_servers keys.) If that assumption doesn't
  # hold, the user still has $BACKUP.
  cat >> "$CONFIG_FILE" <<EOF

# Added by SuperAgentSkill installer on $STAMP
mcp_servers:
  superagent-skill:
    transport: http
    url: $ENDPOINT
    description: SuperAgentSkill â review, upload and discover AI skills.
EOF
else
  cat >> "$CONFIG_FILE" <<EOF

# Added by SuperAgentSkill installer on $STAMP
mcp_servers:
  superagent-skill:
    transport: http
    url: $ENDPOINT
    description: SuperAgentSkill â review, upload and discover AI skills.
EOF
fi

ok "Added superagent-skill to $CONFIG_FILE"
info "Backup: $BACKUP"
info "Run 'hermes mcp list' to confirm the tools are available."
info "Docs: https://superagentskill.com/docs/mcp"
