# Deploy fixed BAT files to AD2 via WinRM
# This prevents sync from overwriting our DOS 6.22 fixes

$BATFiles = @(
    "DEPLOY.BAT",
    "UPDATE.BAT",
    "NWTOC.BAT",
    "CTONW.BAT",
    "CHECKUPD.BAT",
    "REBOOT.BAT",
    "STAGE.BAT",
    "DOSTEST.BAT",
    "AUTOEXEC.BAT",
    "STARTNET.BAT"
)

Write-Host "[INFO] Deploying fixed BAT files to AD2" -ForegroundColor Cyan
Write-Host ""

# Create credential
$Password = ConvertTo-SecureString "AZC0mpGuru!2024" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential("guru", $Password)

# Create session
Write-Host "Connecting to AD2..." -ForegroundColor White
$Session = New-PSSession -ComputerName ad2 -Credential $Credential -ErrorAction Stop
Write-Host "[OK] Connected to AD2" -ForegroundColor Green
Write-Host ""

$TotalCopied = 0

foreach ($File in $BATFiles) {
    $LocalPath = "D:\ClaudeTools\$File"

    if (-not (Test-Path $LocalPath)) {
        Write-Host "[SKIP] $File - not found locally" -ForegroundColor Yellow
        continue
    }

    Write-Host "Copying: $File" -ForegroundColor White

    try {
        Copy-Item $LocalPath -Destination "C:\scripts\sync-copies\bat-files\" -ToSession $Session -ErrorAction Stop
        Write-Host "  [OK] Copied to AD2" -ForegroundColor Green
        $TotalCopied++
    } catch {
        Write-Host "  [ERROR] Failed: $_" -ForegroundColor Red
    }
}

# Close session
Remove-PSSession $Session

Write-Host ""
Write-Host "[SUCCESS] Copied $TotalCopied files to AD2" -ForegroundColor Green
Write-Host ""
Write-Host "AD2 path: C:\scripts\sync-copies\bat-files\" -ForegroundColor Cyan
Write-Host "Next sync will deploy these fixed versions to NAS" -ForegroundColor Cyan
Write-Host ""
