#!/bin/bash
# 准备Linux打包的源代码包

echo "=== 准备Linux打包源代码包 ==="

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

# 复制必要文件
echo "复制源代码..."
cp -r app/ "$PACKAGE_DIR/"
cp main.py "$PACKAGE_DIR/"
cp build.spec "$PACKAGE_DIR/"
cp build_linux.py "$PACKAGE_DIR/"
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 uninstall.sh "$PACKAGE_DIR/"
cp BUILD_GUIDE.md "$PACKAGE_DIR/"

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

echo "=== 在Linux环境下打包云盘后端 ==="

# 安装系统依赖
sudo apt-get update
sudo apt-get install -y python3 python3-pip python3-venv build-essential

# 创建虚拟环境
python3 -m venv venv
source venv/bin/activate

# 安装依赖
pip install --upgrade pip
pip install -r requirements-build.txt
pip install -r requirements.txt

# 运行打包
python build_linux.py

echo "=== 打包完成 ==="
echo "可执行文件位置: deploy/cloud-drive-server"
echo "现在可以运行 ./install.sh 进行安装"
EOF

chmod +x "$PACKAGE_DIR/build_on_linux.sh"

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

echo "=== 源代码包准备完成 ==="
echo "生成的文件："
echo "  $PACKAGE_DIR/ - 源代码目录"
echo "  $PACKAGE_DIR.tar.gz - 压缩包"
echo ""
echo "将压缩包上传到Linux服务器后，解压并运行："
echo "  tar -xzf $PACKAGE_DIR.tar.gz"
echo "  cd $PACKAGE_DIR"
echo "  ./build_on_linux.sh"