#!/bin/bash

echo "🚀 需求分析MCP服务快速启动脚本"
echo "================================"

echo ""
echo "📦 检查项目状态..."
if [ ! -f "dist/index.js" ]; then
    echo "⚠️  编译文件不存在，开始编译..."
    npm run build
    if [ $? -ne 0 ]; then
        echo "❌ 编译失败！"
        exit 1
    fi
    echo "✅ 编译完成"
else
    echo "✅ 编译文件已存在"
fi

echo ""
echo "🎯 选择启动方式:"
echo "1. NPX启动 (推荐)"
echo "2. Node直接启动"
echo "3. 打包并全局安装"
echo "4. 退出"

read -p "请选择 (1-4): " choice

case $choice in
    1)
        echo ""
        echo "🚀 使用NPX启动服务..."
        npx requirements-analysis-simple
        ;;
    2)
        echo ""
        echo "🚀 使用Node直接启动服务..."
        node dist/index.js
        ;;
    3)
        echo ""
        echo "📦 打包项目..."
        npm pack
        if [ $? -ne 0 ]; then
            echo "❌ 打包失败！"
            exit 1
        fi
        
        echo "🌐 全局安装..."
        tgz_file=$(ls requirements-analysis-simple-*.tgz | head -n 1)
        npm install -g "$tgz_file"
        if [ $? -ne 0 ]; then
            echo "❌ 全局安装失败！"
            exit 1
        fi
        
        echo "✅ 安装完成！现在可以在任何地方使用:"
        echo "   npx requirements-analysis-simple"
        echo "   或"
        echo "   npx req-analysis"
        
        echo ""
        echo "🚀 启动服务..."
        npx requirements-analysis-simple
        ;;
    4)
        echo "👋 再见！"
        exit 0
        ;;
    *)
        echo "❌ 无效选择！"
        exit 1
        ;;
esac
