#!/bin/bash
# Exa MCP 部署脚本
# Author: Augment Agent
# Date: 2025-07-01

echo "🚀 开始部署 Exa MCP Server"
echo "=" * 50

# 检查 Node.js 环境
echo "🔍 检查 Node.js 环境..."
if command -v node &> /dev/null; then
    NODE_VERSION=$(node --version)
    echo "✅ Node.js 已安装: $NODE_VERSION"
else
    echo "❌ Node.js 未安装，请先安装 Node.js"
    echo "💡 安装命令: brew install node (macOS) 或访问 https://nodejs.org"
    exit 1
fi

# 检查 npm 环境
echo "🔍 检查 npm 环境..."
if command -v npm &> /dev/null; then
    NPM_VERSION=$(npm --version)
    echo "✅ npm 已安装: $NPM_VERSION"
else
    echo "❌ npm 未安装"
    exit 1
fi

# 检查 EXA_API_KEY 环境变量
echo "🔍 检查 Exa API Key..."
if [ -z "$EXA_API_KEY" ]; then
    echo "⚠️ EXA_API_KEY 环境变量未设置"
    echo "📝 请设置您的 Exa API Key:"
    echo "   export EXA_API_KEY='your-api-key-here'"
    echo ""
    echo "🔗 获取 API Key: https://dashboard.exa.ai/api-keys"
    echo ""
    read -p "是否继续部署? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
else
    echo "✅ EXA_API_KEY 已设置: ${EXA_API_KEY:0:10}..."
fi

# 选择部署方式
echo ""
echo "📋 请选择部署方式:"
echo "1. 全局安装 Exa MCP Server (推荐)"
echo "2. 使用 npx 运行 (无需安装)"
echo "3. 配置 Claude Desktop 集成"
echo "4. 测试远程服务连接"
echo ""
read -p "请选择 (1-4): " -n 1 -r
echo

case $REPLY in
    1)
        echo "📦 开始全局安装 Exa MCP Server..."
        npm install -g exa-mcp-server
        if [ $? -eq 0 ]; then
            echo "✅ Exa MCP Server 安装成功"
            echo "🚀 启动命令: exa-mcp-server"
        else
            echo "❌ 安装失败"
            exit 1
        fi
        ;;
    2)
        echo "🔧 使用 npx 运行 Exa MCP Server..."
        echo "🚀 启动命令: npx -y exa-mcp-server"
        echo "💡 测试运行..."
        timeout 10s npx -y exa-mcp-server --help
        if [ $? -eq 0 ] || [ $? -eq 124 ]; then
            echo "✅ npx 方式可用"
        else
            echo "❌ npx 运行失败"
        fi
        ;;
    3)
        echo "⚙️ 配置 Claude Desktop 集成..."
        
        # 检查 Claude Desktop 配置目录
        if [[ "$OSTYPE" == "darwin"* ]]; then
            CONFIG_DIR="$HOME/Library/Application Support/Claude"
        elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
            CONFIG_DIR="$HOME/.config/claude"
        else
            CONFIG_DIR="$HOME/.claude"
        fi
        
        CONFIG_FILE="$CONFIG_DIR/claude_desktop_config.json"
        
        echo "📁 配置目录: $CONFIG_DIR"
        echo "📄 配置文件: $CONFIG_FILE"
        
        # 创建配置目录
        mkdir -p "$CONFIG_DIR"
        
        # 生成配置文件
        cat > "$CONFIG_FILE" << EOF
{
  "mcpServers": {
    "exa": {
      "command": "npx",
      "args": ["-y", "exa-mcp-server"],
      "env": {
        "EXA_API_KEY": "$EXA_API_KEY"
      }
    },
    "exa-xiaohongshu": {
      "command": "npx",
      "args": [
        "-y", 
        "exa-mcp-server", 
        "--tools=web_search_exa,company_research,competitor_finder"
      ],
      "env": {
        "EXA_API_KEY": "$EXA_API_KEY"
      }
    }
  }
}
EOF
        
        echo "✅ Claude Desktop 配置文件已创建"
        echo "🔄 请重启 Claude Desktop 以加载配置"
        ;;
    4)
        echo "🌐 测试远程服务连接..."
        REMOTE_URL="https://mcp.exa.ai/mcp"
        
        echo "🔗 远程服务地址: $REMOTE_URL"
        echo "🔑 API Key: ${EXA_API_KEY:0:10}..."
        
        # 测试连接
        curl -s --connect-timeout 10 "$REMOTE_URL" > /dev/null
        if [ $? -eq 0 ]; then
            echo "✅ 远程服务连接正常"
            echo "🚀 使用方式: $REMOTE_URL?exaApiKey=$EXA_API_KEY"
        else
            echo "❌ 远程服务连接失败"
        fi
        ;;
    *)
        echo "❌ 无效选择"
        exit 1
        ;;
esac

echo ""
echo "🎉 Exa MCP 部署完成!"
echo ""
echo "📋 下一步操作:"
echo "1. 确保 EXA_API_KEY 环境变量已设置"
echo "2. 根据选择的方式启动 Exa MCP Server"
echo "3. 在 Claude 中测试 Exa 搜索功能"
echo ""
echo "🔧 测试命令:"
echo "   exa-mcp-server --help"
echo "   npx -y exa-mcp-server --help"
echo ""
echo "📚 可用工具:"
echo "   • web_search_exa - 网络搜索"
echo "   • company_research - 公司研究"
echo "   • competitor_finder - 竞争对手分析"
echo "   • research_paper_search - 学术搜索"
echo "   • linkedin_search - LinkedIn 搜索"
echo "   • wikipedia_search_exa - Wikipedia 搜索"
echo "   • github_search - GitHub 搜索"
echo "   • crawling - 内容爬取"
echo ""
echo "🎯 小红书投放应用:"
echo "   • 搜索行业趋势: web_search_exa"
echo "   • 分析竞争对手: competitor_finder + company_research"
echo "   • 研究成功案例: web_search_exa + crawling"
echo "   • 获取用户洞察: research_paper_search"
