#!/usr/bin/env bash
# Atlan Docs MCP — universal installer
# Usage: curl -fsSL "https://docs.atlan.com/install-docs-mcp" | bash

set -euo pipefail

: "${HOME:?HOME is not set — cannot determine config paths}"

MCP_URL="https://docs.atlan.com/mcp"
SERVER_NAME="atlan-docs"

echo ""
echo "Atlan Docs MCP installer"
echo "━━━━━━━━━━━━━━━━━━━━━━━━"

# ── Merge or create a JSON MCP config file ───────────────────────────────────
write_json_config() {
  local file="$1"
  local key="$2"
  local dir
  dir="$(dirname "$file")"

  if [ -L "$file" ]; then
    echo "✗ $file is a symlink — refusing to write through it."
    echo "  Remove or replace it, then re-run this installer."
    exit 1
  fi

  mkdir -p "$dir"

  if command -v python3 &>/dev/null; then
    python3 - "$file" "$key" "$SERVER_NAME" "$MCP_URL" <<'PYEOF'
import sys, json, os
path, key, name, url = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
config = {}
if os.path.exists(path):
    with open(path) as f:
        try: config = json.load(f)
        except Exception: pass
config.setdefault(key, {})[name] = {"type": "http", "url": url}
with open(path, 'w') as f:
    json.dump(config, f, indent=2)
    f.write('\n')
PYEOF
  else
    if [ ! -f "$file" ]; then
      printf '{\n  "%s": {\n    "%s": { "type": "http", "url": "%s" }\n  }\n}\n' \
        "$key" "$SERVER_NAME" "$MCP_URL" > "$file"
    else
      echo "⚠  Could not merge (python3 not found). Add manually to $file:"
      echo "     \"$SERVER_NAME\": { \"type\": \"http\", \"url\": \"$MCP_URL\" }"
    fi
  fi
}

# ── Detect installed AI clients ───────────────────────────────────────────────
has_app() {
  # macOS app bundle check
  [ -d "/Applications/$1" ] || [ -d "$HOME/Applications/$1" ]
}

# ── 1. Claude Code CLI ────────────────────────────────────────────────────────
if command -v claude &>/dev/null; then
  echo "→ Detected Claude Code"
  claude mcp add --transport http "$SERVER_NAME" "$MCP_URL"
  echo ""
  echo "✓ Done. Start Claude Code and try: What is a Persona in Atlan?"
  exit 0
fi

# ── 2. Cursor ────────────────────────────────────────────────────────────────
if command -v cursor &>/dev/null || has_app "Cursor.app" || [ -d "$HOME/.cursor" ]; then
  echo "→ Detected Cursor"
  write_json_config "$HOME/.cursor/mcp.json" "mcpServers"
  echo ""
  echo "✓ Added atlan-docs to ~/.cursor/mcp.json"
  echo "  Restart Cursor to activate."
  exit 0
fi

# ── 3. Windsurf ──────────────────────────────────────────────────────────────
if command -v windsurf &>/dev/null || has_app "Windsurf.app" || [ -d "$HOME/.codeium/windsurf" ]; then
  echo "→ Detected Windsurf"
  write_json_config "$HOME/.codeium/windsurf/mcp_config.json" "mcpServers"
  echo ""
  echo "✓ Added atlan-docs to ~/.codeium/windsurf/mcp_config.json"
  echo "  Restart Windsurf to activate."
  exit 0
fi

# ── 4. VS Code ───────────────────────────────────────────────────────────────
if command -v code &>/dev/null || has_app "Visual Studio Code.app"; then
  echo "→ Detected VS Code"
  write_json_config "$HOME/.vscode/mcp.json" "servers"
  echo ""
  echo "✓ Added atlan-docs to ~/.vscode/mcp.json"
  echo "  Reload VS Code to activate."
  exit 0
fi

# ── 5. Claude Desktop ────────────────────────────────────────────────────────
CLAUDE_DESKTOP_CONFIG=""
if [ -d "$HOME/Library/Application Support/Claude" ]; then
  CLAUDE_DESKTOP_CONFIG="$HOME/Library/Application Support/Claude/claude_desktop_config.json"
elif [ -n "${APPDATA:-}" ] && [ -d "$APPDATA/Claude" ]; then
  CLAUDE_DESKTOP_CONFIG="$APPDATA/Claude/claude_desktop_config.json"
fi

if [ -n "$CLAUDE_DESKTOP_CONFIG" ]; then
  echo "→ Detected Claude Desktop"
  write_json_config "$CLAUDE_DESKTOP_CONFIG" "mcpServers"
  echo ""
  echo "✓ Added atlan-docs to Claude Desktop config."
  echo "  Restart Claude Desktop to activate."
  exit 0
fi

# ── Fallback ─────────────────────────────────────────────────────────────────
echo ""
echo "Could not auto-detect your AI tool."
echo ""
echo "For Claude Code:"
echo "  claude mcp add --transport http $SERVER_NAME $MCP_URL"
echo ""
echo "For other clients, visit:"
echo "  https://docs.atlan.com/agents/how-tos/set-up-docs-over-mcp"
