#!/bin/bash

SERVER="root@118.145.218.2"
REMOTE_DIR="/mnt/data/asset-management/backend"
LOCAL_DIR="C:/Users/Administrator/asset_management_backend"

echo "=========================================="
echo "资产管理系统后端部署脚本"
echo "=========================================="
echo ""

# 步骤1：上传代码到服务器
echo "[步骤1] 上传代码到服务器..."
echo "正在连接到 $SERVER ..."
echo "请输入服务器密码 (Yuyx4944@@@)"

# 创建远程目录
ssh $SERVER "mkdir -p $REMOTE_DIR"

# 上传代码（排除不必要的文件）
echo "正在上传文件..."
rsync -avz --progress \
  --exclude '.git' \
  --exclude '__pycache__' \
  --exclude '*.pyc' \
  --exclude '.pytest_cache' \
  --exclude 'test_*.py' \
  --exclude 'test_reports' \
  --exclude 'logs/*.log' \
  --exclude 'uploads/*' \
  --exclude '.env' \
  --exclude '*.md' \
  --exclude 'start.bat' \
  --exclude 'verify_*.py' \
  $LOCAL_DIR/ $SERVER:$REMOTE_DIR/

if [ $? -eq 0 ]; then
    echo "✓ 代码上传成功！"
else
    echo "✗ 代码上传失败！"
    exit 1
fi

echo ""
echo "[步骤2] 在服务器上部署应用..."
echo "请继续在服务器上执行以下命令："
echo ""
echo "ssh $SERVER"
echo "cd $REMOTE_DIR"
echo "docker build -t asset-backend ."
echo "source /mnt/data/asset-management/db_config.sh"
echo "docker run -d \\"
echo "  --name asset-backend \\"
echo "  --link asset-db:asset-db \\"
echo "  --link asset-redis:asset-redis \\"
echo "  -p 8001:8001 \\"
echo "  -v /mnt/data/asset-management/backend/uploads:/app/uploads \\"
echo "  -v /mnt/data/asset-management/backend/logs:/app/logs \\"
echo "  -e DATABASE_URL=\"postgresql+asyncpg://asset_user:\${DB_PASSWORD}@asset-db:5432/asset_management\" \\"
echo "  -e REDIS_URL=\"redis://:\${REDIS_PASSWORD}@asset-redis:6379/0\" \\"
echo "  --restart unless-stopped \\"
echo "  asset-backend"
echo ""

