# 结伴客后端同步脚本 - Windows PowerShell版本
# 同步文件到CentOS生产服务器
# 服务器地址: www.jiebanke.com
# 服务器目录: /data/nodejsjiebanke/

Write-Host "🚀 开始同步文件到生产服务器..." -ForegroundColor Green
Write-Host "📋 服务器: www.jiebanke.com"
Write-Host "📁 目标目录: /data/nodejsjiebanke/"
Write-Host "📁 本地目录: e:\vue\jiebanke\backend\"
Write-Host ""

# 检查rsync是否可用
$rsyncAvailable = Get-Command rsync -ErrorAction SilentlyContinue
if (-not $rsyncAvailable) {
    Write-Host "❌ rsync未安装，请先安装Git for Windows或Cygwin" -ForegroundColor Red
    Write-Host "💡 或者使用WSL中的rsync"
    exit 1
}

# 同步文件到服务器
try {
    Write-Host "📤 正在同步文件..." -ForegroundColor Yellow
    
    $rsyncArgs = @(
        "-avz",
        "--delete",
        "--exclude=node_modules/",
        "--exclude=logs/",
        "--exclude=uploads/",
        "--exclude=.git/",
        "--exclude=.env",
        "--exclude=*.log",
        "--exclude=*.tmp",
        "--exclude=.DS_Store",
        "--exclude=*.swp",
        "--exclude=*.swo",
        "-e", "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
        "e:/vue/jiebanke/backend/",
        "root@www.jiebanke.com:/data/nodejsjiebanke/"
    )
    
    & rsync @rsyncArgs
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 文件同步完成!" -ForegroundColor Green
    } else {
        Write-Host "❌ 文件同步失败，退出码: $LASTEXITCODE" -ForegroundColor Red
        exit $LASTEXITCODE
    }
    
    # 设置文件权限
    Write-Host "🔧 设置文件权限..." -ForegroundColor Yellow
    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@www.jiebanke.com "chmod 755 /data/nodejsjiebanke/*.sh && chmod 644 /data/nodejsjiebanke/package.json"
    
    Write-Host ""
    Write-Host "✅ 同步完成!" -ForegroundColor Green
    Write-Host "📋 同步时间: $(Get-Date)"
    Write-Host "💡 接下来请在服务器上运行启动脚本:"
    Write-Host "   ssh root@www.jiebanke.com"
    Write-Host "   cd /data/nodejsjiebanke/ && ./start-server.sh"
    
} catch {
    Write-Host "❌ 同步过程中发生错误: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}