#!/bin/bash
# 量化交易系统环境设置脚本
# 使用方法: source setup_env.sh

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo "=========================================="
echo "  量化交易系统 - 环境初始化"
echo "=========================================="
echo ""

# 检查虚拟环境是否存在
if [ ! -d "vnpy_env" ]; then
    echo "⚠️  虚拟环境不存在，正在创建..."
    python3 -m venv vnpy_env
    echo "✅ 虚拟环境创建成功"
fi

# 激活虚拟环境
echo "🔧 激活虚拟环境..."
source vnpy_env/bin/activate

# 升级 pip
echo "🔧 升级 pip..."
pip install --upgrade pip -q

# 检查依赖是否安装
if [ ! -f "vnpy_env/.dependencies_installed" ]; then
    echo "📦 安装项目依赖..."
    pip install -r requirements.txt
    touch vnpy_env/.dependencies_installed
    echo "✅ 依赖安装完成"
else
    echo "✅ 依赖已安装"
fi

# 检查必要的目录结构
echo "📂 检查目录结构..."
mkdir -p vnpy_project/{logs,data,strategies,backup}
echo "✅ 目录结构检查完成"

# 设置环境变量
export QUANT_ENV=development
export QUANT_DB_TYPE=sqlite
export PYTHONPATH="$SCRIPT_DIR:$PYTHONPATH"

echo ""
echo "=========================================="
echo "  ✅ 环境初始化完成！"
echo "=========================================="
echo ""
echo "📌 环境变量已设置："
echo "   - QUANT_ENV: $QUANT_ENV"
echo "   - QUANT_DB_TYPE: $QUANT_DB_TYPE"
echo "   - PYTHONPATH: $PYTHONPATH"
echo ""
echo "📌 常用命令："
echo "   - 运行系统: python main.py"
echo "   - 测试数据库: python test_database.py"
echo "   - 退出虚拟环境: deactivate"
echo ""
