# Deploy XCOPY /D Fix - Round 2
# Fixes NWTOC.BAT and CHECKUPD.BAT XCOPY /D errors
# Date: 2026-01-20

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deploying XCOPY /D Fix - Round 2" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# AD2 Connection Details
$AD2Host = "192.168.0.6"
$Username = "INTRANET\sysadmin"
$Password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential($Username, $Password)

# Files to deploy
$Files = @(
    @{Name="NWTOC.BAT"; Version="v2.1"},
    @{Name="CHECKUPD.BAT"; Version="v1.1"}
)

# Destinations
$Destinations = @(
    "\\$AD2Host\C$\Shares\test\COMMON\ProdSW\",
    "\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\"
)

# Map network drive
Write-Host "Connecting to AD2..." -ForegroundColor Yellow
try {
    New-PSDrive -Name TEMP_AD2 -PSProvider FileSystem -Root "\\$AD2Host\C$" -Credential $Cred -ErrorAction Stop | Out-Null
    Write-Host "[OK] Connected to AD2" -ForegroundColor Green
    Write-Host ""
} catch {
    Write-Host "[ERROR] Failed to connect to AD2: $_" -ForegroundColor Red
    exit 1
}

# Deploy to each destination
$TotalSuccess = 0
foreach ($Dest in $Destinations) {
    $DestName = if ($Dest -like "*_COMMON*") { "_COMMON\ProdSW" } else { "COMMON\ProdSW" }
    Write-Host "Deploying to $DestName..." -ForegroundColor Cyan

    foreach ($File in $Files) {
        $SourceFile = "D:\ClaudeTools\$($File.Name)"
        $DestFile = "$Dest$($File.Name)"

        if (-not (Test-Path $SourceFile)) {
            Write-Host "  [ERROR] Source not found: $($File.Name)" -ForegroundColor Red
            continue
        }

        try {
            Copy-Item -Path $SourceFile -Destination $DestFile -Force -ErrorAction Stop
            Write-Host "  [OK] $($File.Name) $($File.Version)" -ForegroundColor Green
            $TotalSuccess++
        } catch {
            Write-Host "  [ERROR] $($File.Name): $_" -ForegroundColor Red
        }
    }
    Write-Host ""
}

# Cleanup
Remove-PSDrive -Name TEMP_AD2 -ErrorAction SilentlyContinue

# Summary
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deployment Summary" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Files Fixed:" -ForegroundColor White
Write-Host "  NWTOC.BAT v2.1" -ForegroundColor Gray
Write-Host "    - Line 88: Removed /D from XCOPY (ProdSW)" -ForegroundColor Gray
Write-Host "    - Line 178: Removed /D from XCOPY (NET)" -ForegroundColor Gray
Write-Host ""
Write-Host "  CHECKUPD.BAT v1.1" -ForegroundColor Gray
Write-Host "    - Line 201: Removed /D from XCOPY" -ForegroundColor Gray
Write-Host "    - Simplified file comparison logic" -ForegroundColor Gray
Write-Host ""
Write-Host "Deployments: $TotalSuccess/4 successful" -ForegroundColor $(if ($TotalSuccess -eq 4) { "Green" } else { "Yellow" })
Write-Host ""
Write-Host "Combined with previous fixes:" -ForegroundColor Yellow
Write-Host "  UPDATE.BAT v2.1 - XCOPY /D fixed" -ForegroundColor White
Write-Host "  NWTOC.BAT v2.1 - XCOPY /D fixed (2 instances)" -ForegroundColor White
Write-Host "  CHECKUPD.BAT v1.1 - XCOPY /D fixed" -ForegroundColor White
Write-Host ""
Write-Host "All DOS 6.22 XCOPY /D errors now fixed!" -ForegroundColor Green
Write-Host ""
Write-Host "Next:" -ForegroundColor Yellow
Write-Host "  AD2 will sync to NAS within 15 minutes" -ForegroundColor White
Write-Host "  Test on TS-4R after sync completes" -ForegroundColor White
