#!/bin/bash
# 准备Linux打包的源代码包（修复版）

echo "=== 准备Linux打包源代码包（修复版） ==="

# 删除旧的包
rm -rf cloud-drive-source-*

# 创建临时目录
PACKAGE_DIR="cloud-drive-source-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$PACKAGE_DIR"

echo "复制源代码和配置文件..."
# 复制必要文件
cp -r app/ "$PACKAGE_DIR/"
cp main.py "$PACKAGE_DIR/"
cp build.spec "$PACKAGE_DIR/"
cp build_linux_fixed.py "$PACKAGE_DIR/build_linux.py"
cp requirements.txt "$PACKAGE_DIR/"
cp requirements-build.txt "$PACKAGE_DIR/"
cp .env.example "$PACKAGE_DIR/"
cp cloud-drive.service "$PACKAGE_DIR/"
cp install.sh "$PACKAGE_DIR/"
cp install_user.sh "$PACKAGE_DIR/"
cp BUILD_GUIDE.md "$PACKAGE_DIR/"

# 创建Linux打包脚本
cat > "$PACKAGE_DIR/build_on_linux.sh" << 'EOF'
#!/bin/bash
# 在Linux环境下的打包脚本（修复版）

echo "=== 在Linux环境下打包云盘后端（修复版） ==="

# 检查是否为root用户
if [ "$EUID" -eq 0 ]; then
    echo "警告: 不建议在root用户下直接打包，建议使用普通用户"
    read -p "是否继续？(y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# 安装系统依赖（Ubuntu/Debian）
if command -v apt-get &> /dev/null; then
    echo "检测到Ubuntu/Debian系统"
    sudo apt-get update || echo "无法更新包列表，跳过..."
    sudo apt-get install -y python3 python3-pip python3-venv build-essential || echo "安装系统依赖失败，请手动安装"
# 安装系统依赖（CentOS/RHEL）
elif command -v yum &> /dev/null; then
    echo "检测到CentOS/RHEL系统"
    sudo yum install -y python3 python3-pip python3-devel gcc gcc-c++ make || echo "安装系统依赖失败，请手动安装"
# 安装系统依赖（其他系统）
else
    echo "警告: 未检测到支持的包管理器，请手动安装Python3和编译工具"
fi

# 检查Python版本
python3 --version
if [ $? -ne 0 ]; then
    echo "错误: Python3 未安装或无法访问"
    exit 1
fi

# 创建虚拟环境
echo "创建Python虚拟环境..."
python3 -m venv venv || {
    echo "错误: 创建虚拟环境失败"
    exit 1
}

# 激活虚拟环境
echo "激活虚拟环境..."
source venv/bin/activate || {
    echo "错误: 激活虚拟环境失败"
    exit 1
}

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

# 安装依赖
echo "安装Python依赖包..."
pip install -r requirements-build.txt || {
    echo "错误: 安装构建依赖失败"
    exit 1
}

pip install -r requirements.txt || {
    echo "错误: 安装应用依赖失败"
    exit 1
}

# 运行打包
echo "开始打包..."
python build_linux.py || {
    echo "错误: 打包失败"
    exit 1
}

# 检查打包结果
if [ -f "deploy/cloud-drive-server" ]; then
    echo "=== 打包成功 ==="
    echo "可执行文件位置: deploy/cloud-drive-server"
    echo "文件大小: $(ls -lh deploy/cloud-drive-server | awk '{print $5}')"
    echo ""
    echo "下一步操作："
    echo "1. 用户级安装（推荐，无需sudo）："
    echo "   cd deploy && ./install_user.sh"
    echo ""
    echo "2. 系统级安装（需要sudo权限）："
    echo "   cd deploy && sudo ./install.sh"
    echo ""
    echo "3. 直接运行："
    echo "   cd deploy && ./cloud-drive-server"
    echo ""
    echo "4. 查看详细说明："
    echo "   cat deploy/README.md"
else
    echo "错误: 打包失败，未找到可执行文件"
    exit 1
fi
EOF

chmod +x "$PACKAGE_DIR/build_on_linux.sh"

# 创建问题排查脚本
cat > "$PACKAGE_DIR/troubleshoot.sh" << 'EOF'
#!/bin/bash
# 问题排查脚本

echo "=== 云盘后端问题排查工具 ==="

echo "1. 检查Python环境："
python3 --version 2>&1 || echo "Python3 未安装"

echo ""
echo "2. 检查pip："
pip3 --version 2>&1 || echo "pip3 未安装"

echo ""
echo "3. 检查系统依赖："
if command -v apt-get &> /dev/null; then
    echo "包管理器: apt-get (Ubuntu/Debian)"
    dpkg -l | grep -E "(python3|gcc|make)" || echo "检查系统依赖包..."
elif command -v yum &> /dev/null; then
    echo "包管理器: yum (CentOS/RHEL)"
    rpm -qa | grep -E "(python3|gcc|make)" || echo "检查系统依赖包..."
else
    echo "未检测到支持的包管理器"
fi

echo ""
echo "4. 检查虚拟环境："
if [ -d "venv" ]; then
    echo "虚拟环境目录存在"
    if [ -f "venv/bin/python" ]; then
        echo "虚拟环境Python可执行文件存在"
        venv/bin/python --version
    else
        echo "虚拟环境Python可执行文件不存在"
    fi
else
    echo "虚拟环境目录不存在"
fi

echo ""
echo "5. 检查源代码文件："
for file in main.py build.spec requirements.txt; do
    if [ -f "$file" ]; then
        echo "✓ $file 存在"
    else
        echo "✗ $file 不存在"
    fi
done

echo ""
echo "6. 检查app目录："
if [ -d "app" ]; then
    echo "✓ app目录存在"
    find app -name "*.py" | head -5 | while read file; do
        echo "  ✓ $file"
    done
else
    echo "✗ app目录不存在"
fi

echo ""
echo "7. 网络连接测试："
if ping -c 1 pypi.org &> /dev/null; then
    echo "✓ 可以访问PyPI"
else
    echo "✗ 无法访问PyPI，可能需要配置代理"
fi

echo ""
echo "=== 排查完成 ==="
echo "如果发现问题，请根据上述输出进行修复"
EOF

chmod +x "$PACKAGE_DIR/troubleshoot.sh"

# 创建压缩包
echo "创建压缩包..."
tar -czf "$PACKAGE_DIR.tar.gz" "$PACKAGE_DIR"

echo "=== 源代码包准备完成 ==="
echo "生成的文件："
echo "  $PACKAGE_DIR/ - 源代码目录"
echo "  $PACKAGE_DIR.tar.gz - 压缩包 ($(ls -lh "$PACKAGE_DIR.tar.gz" | awk '{print $5}'))"
echo ""
echo "修复内容："
echo "  ✓ 添加了用户级安装脚本 install_user.sh"
echo "  ✓ 修复了 build.spec 依赖缺失问题"
echo "  ✓ 改进了打包脚本 build_linux.py"
echo "  ✓ 添加了问题排查脚本 troubleshoot.sh"
echo "  ✓ 增强了安装说明文档"
echo ""
echo "将压缩包上传到Linux服务器后，解压并运行："
echo "  tar -xzf $PACKAGE_DIR.tar.gz"
echo "  cd $PACKAGE_DIR"
echo "  ./build_on_linux.sh"
echo ""
echo "如果遇到问题，可以先运行："
echo "  ./troubleshoot.sh"
EOF

chmod +x backend/prepare_linux_package_fixed.sh