#!/bin/bash
# ZJPB v2.1 快速部署脚本
# 在生产服务器上执行此脚本

echo "================================"
echo "ZJPB v2.1 增量部署"
echo "================================"
echo ""

# 检查是否在正确目录
if [ ! -f "app.py" ]; then
    echo "❌ 错误：请在项目根目录执行此脚本"
    exit 1
fi

# 停止应用
echo "1. 停止应用..."
./manage.sh stop
sleep 2

# 备份当前代码
echo "2. 备份当前代码..."
BACKUP_DIR="../zjpb_backup_$(date +%Y%m%d_%H%M%S)"
cp -r ../zjpb "$BACKUP_DIR"
echo "   备份已保存到: $BACKUP_DIR"

# 激活虚拟环境
echo "3. 激活虚拟环境..."
source venv/bin/activate

# 安装依赖（如有更新）
echo "4. 检查依赖..."
pip install -r requirements.txt -q

# 运行数据库迁移
echo "5. 运行数据库迁移..."
python migrate_prompts.py

# 重启应用
echo "6. 重启应用..."
./manage.sh start
sleep 2

# 检查状态
echo "7. 检查应用状态..."
./manage.sh status

echo ""
echo "================================"
echo "✅ 部署完成！"
echo "================================"
echo ""
echo "验证项目："
echo "1. 访问前台首页，检查页脚ICP备案号"
echo "2. 访问详情页，检查图标是否正常"
echo "3. 登录后台，检查Prompt管理菜单"
echo "4. 编辑网站，检查标签是否正常显示"
echo ""
echo "如遇问题，可快速回滚："
echo "   ./manage.sh stop"
echo "   cd .."
echo "   rm -rf zjpb"
echo "   mv $BACKUP_DIR zjpb"
echo "   cd zjpb"
echo "   ./manage.sh start"
echo ""
