#!/bin/bash
# EG Linux 构建环境检查脚本

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

ISSUES=()
WARNINGS=()

echo "========================================"
echo -e "${BLUE}  EG 构建环境检查${NC}"
echo "========================================"

# 检查 Python
echo ""
echo "[1/6] 检查 Python..."
if command -v python3 &> /dev/null; then
    VERSION=$(python3 --version 2>&1)
    echo -e "  ${GREEN}✓${NC} $VERSION"
else
    echo -e "  ${RED}✗${NC} Python3 未找到"
    ISSUES+=("需要安装 Python 3.8+")
fi

# 检查 pip
echo ""
echo "[2/6] 检查 pip..."
if command -v pip3 &> /dev/null; then
    VERSION=$(pip3 --version 2>&1 | awk '{print $2}')
    echo -e "  ${GREEN}✓${NC} pip $VERSION"
else
    echo -e "  ${RED}✗${NC} pip3 未找到"
    ISSUES+=("需要安装 pip3")
fi

# 检查 GCC
echo ""
echo "[3/6] 检查 GCC..."
if command -v gcc &> /dev/null; then
    VERSION=$(gcc --version 2>&1 | head -1)
    echo -e "  ${GREEN}✓${NC} $VERSION"
else
    echo -e "  ${RED}✗${NC} GCC 未找到"
    ISSUES+=("需要安装 GCC: sudo apt-get install build-essential")
fi

# 检查 G++
echo ""
echo "[4/6] 检查 G++..."
if command -v g++ &> /dev/null; then
    VERSION=$(g++ --version 2>&1 | head -1)
    echo -e "  ${GREEN}✓${NC} $VERSION"
else
    echo -e "  ${RED}✗${NC} G++ 未找到"
    ISSUES+=("需要安装 G++: sudo apt-get install build-essential")
fi

# 检查 Python 模块
echo ""
echo "[5/6] 检查 Python 模块..."
MODULES=("nuitka" "pyinstaller")
for mod in "${MODULES[@]}"; do
    if pip3 show "$mod" &> /dev/null; then
        VERSION=$(pip3 show "$mod" 2>/dev/null | grep Version | awk '{print $2}')
        echo -e "  ${GREEN}✓${NC} $mod $VERSION"
    else
        echo -e "  ${YELLOW}⚠${NC} $mod 未安装 (将自动安装)"
    fi
done

# 检查项目依赖
echo ""
echo "[6/6] 检查项目依赖..."
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REQ_FILE="$SCRIPT_DIR/../requirements/requirements.txt"

if [ -f "$REQ_FILE" ]; then
    echo -e "  ${GREEN}✓${NC} 找到 requirements.txt"
    
    # 检查关键包
    KEY_PKGS=("Panda3D" "PyQt5" "openvr" "imgui-bundle")
    for pkg in "${KEY_PKGS[@]}"; do
        # 转换为小写检查
        pkg_lower=$(echo "$pkg" | tr '[:upper:]' '[:lower:]')
        if pip3 show "$pkg_lower" &> /dev/null; then
            echo -e "  ${GREEN}✓${NC} $pkg"
        else
            echo -e "  ${YELLOW}⚠${NC} $pkg 未安装"
        fi
    done
else
    echo -e "  ${YELLOW}⚠${NC} 未找到 requirements.txt"
    WARNINGS+=("未找到依赖文件")
fi

# 检查额外工具
echo ""
echo "[7/6] 检查额外工具..."
if command -v wget &> /dev/null; then
    echo -e "  ${GREEN}✓${NC} wget"
else
    echo -e "  ${YELLOW}⚠${NC} wget 未安装 (用于下载 appimagetool)"
    WARNINGS+=("建议安装 wget")
fi

# 检查 dpkg (用于构建 deb)
if command -v dpkg-deb &> /dev/null; then
    echo -e "  ${GREEN}✓${NC} dpkg-deb (可构建 DEB 包)"
else
    echo -e "  ${YELLOW}⚠${NC} dpkg-deb 未安装 (无法构建 DEB 包)"
fi

# 总结
echo ""
echo "========================================"
if [ ${#ISSUES[@]} -eq 0 ] && [ ${#WARNINGS[@]} -eq 0 ]; then
    echo -e "${GREEN}  ✓ 环境检查通过!${NC}"
    echo -e "${GREEN}  可以开始构建${NC}"
    exit 0
else
    if [ ${#ISSUES[@]} -gt 0 ]; then
        echo -e "${RED}  发现问题 (${#ISSUES[@]}):${NC}"
        for issue in "${ISSUES[@]}"; do
            echo -e "    ${RED}- $issue${NC}"
        done
    fi
    if [ ${#WARNINGS[@]} -gt 0 ]; then
        echo -e "${YELLOW}  警告 (${#WARNINGS[@]}):${NC}"
        for warn in "${WARNINGS[@]}"; do
            echo -e "    ${YELLOW}- $warn${NC}"
        done
    fi
    exit 1
fi
