# Deploy UPDATE.BAT v2.1 Fix to AD2
# Fixes "Invalid number of parameters" error in DOS 6.22 XCOPY
# Date: 2026-01-20

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deploying UPDATE.BAT v2.1 to AD2" -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)

# Source file
$SourceFile = "D:\ClaudeTools\UPDATE.BAT"

# Destination paths (both COMMON and _COMMON)
$Destinations = @(
    "\\$AD2Host\C$\Shares\test\COMMON\ProdSW\UPDATE.BAT",
    "\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\UPDATE.BAT",
    "\\$AD2Host\C$\Shares\test\UPDATE.BAT"  # Root level for easy access
)

# Verify source file exists
if (-not (Test-Path $SourceFile)) {
    Write-Host "[ERROR] Source file not found: $SourceFile" -ForegroundColor Red
    exit 1
}

Write-Host "[OK] Source file found: $SourceFile" -ForegroundColor Green
Write-Host ""

# 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
$SuccessCount = 0
foreach ($Dest in $Destinations) {
    Write-Host "Deploying to: $Dest" -ForegroundColor Yellow

    try {
        # Create destination directory if it doesn't exist
        $DestDir = Split-Path $Dest -Parent
        if (-not (Test-Path $DestDir)) {
            Write-Host "  Creating directory: $DestDir" -ForegroundColor Yellow
            New-Item -Path $DestDir -ItemType Directory -Force | Out-Null
        }

        # Copy file
        Copy-Item -Path $SourceFile -Destination $Dest -Force -ErrorAction Stop

        # Verify copy
        if (Test-Path $Dest) {
            Write-Host "  [OK] Deployed successfully" -ForegroundColor Green
            $SuccessCount++
        } else {
            Write-Host "  [WARNING] File copied but verification failed" -ForegroundColor Yellow
        }
    } catch {
        Write-Host "  [ERROR] Failed: $_" -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 "Destinations: $($Destinations.Count)" -ForegroundColor White
Write-Host "Successful: $SuccessCount" -ForegroundColor Green
Write-Host "Failed: $($Destinations.Count - $SuccessCount)" -ForegroundColor $(if ($SuccessCount -eq $Destinations.Count) { "Green" } else { "Red" })
Write-Host ""

if ($SuccessCount -eq $Destinations.Count) {
    Write-Host "[OK] UPDATE.BAT v2.1 deployed successfully!" -ForegroundColor Green
    Write-Host ""
    Write-Host "Next Steps:" -ForegroundColor Yellow
    Write-Host "1. AD2 will sync to NAS within 15 minutes" -ForegroundColor White
    Write-Host "2. Test on TS-4R machine:" -ForegroundColor White
    Write-Host "   T:" -ForegroundColor Gray
    Write-Host "   CD \COMMON\ProdSW" -ForegroundColor Gray
    Write-Host "   XCOPY UPDATE.BAT C:\BAT\ /Y" -ForegroundColor Gray
    Write-Host "   C:" -ForegroundColor Gray
    Write-Host "   CD \BAT" -ForegroundColor Gray
    Write-Host "   UPDATE" -ForegroundColor Gray
    Write-Host ""
    Write-Host "3. Verify backup completes without 'Invalid number of parameters' error" -ForegroundColor White
    Write-Host "4. Check T:\TS-4R\BACKUP\ for copied files" -ForegroundColor White
} else {
    Write-Host "[ERROR] Deployment incomplete - manual intervention required" -ForegroundColor Red
}

Write-Host ""
Write-Host "Changelog: UPDATE_BAT_FIX_2026-01-20.md" -ForegroundColor Cyan
