# Deploy Drive Test Fix - All BAT Files
# Removes unreliable DIR >nul pattern, uses direct IF NOT EXIST test
# Date: 2026-01-20

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deploying Drive Test Fix" -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 with new versions
$Files = @(
    @{Name="UPDATE.BAT"; Version="v2.2"; Changes="Simplified drive test"},
    @{Name="NWTOC.BAT"; Version="v2.2"; Changes="Simplified drive test"},
    @{Name="CTONW.BAT"; Version="v2.1"; Changes="Simplified drive test"},
    @{Name="CHECKUPD.BAT"; Version="v1.2"; Changes="Simplified drive test"},
    @{Name="DOSTEST.BAT"; Version="v1.1"; Changes="Fixed T: and X: drive tests"}
)

# 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
$TotalFiles = $Files.Count * $Destinations.Count

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 "Issue Fixed:" -ForegroundColor Yellow
Write-Host "  DIR T:\ >nul pattern was unreliable in DOS 6.22" -ForegroundColor White
Write-Host ""
Write-Host "Solution:" -ForegroundColor Yellow
Write-Host "  Replaced with: IF NOT EXIST T:\*.* GOTO ERROR" -ForegroundColor White
Write-Host "  Direct file existence test - most reliable for DOS 6.22" -ForegroundColor White
Write-Host ""
Write-Host "Files Updated:" -ForegroundColor Cyan
foreach ($File in $Files) {
    Write-Host "  $($File.Name) $($File.Version) - $($File.Changes)" -ForegroundColor Gray
}
Write-Host ""
Write-Host "Deployments: $TotalSuccess/$TotalFiles successful" -ForegroundColor $(if ($TotalSuccess -eq $TotalFiles) { "Green" } else { "Yellow" })
Write-Host ""
Write-Host "Pattern Changes:" -ForegroundColor Yellow
Write-Host "  BEFORE (unreliable):" -ForegroundColor White
Write-Host "    DIR T:\ >nul" -ForegroundColor Gray
Write-Host "    IF ERRORLEVEL 1 GOTO NO_T_DRIVE" -ForegroundColor Gray
Write-Host "    C:" -ForegroundColor Gray
Write-Host "    IF NOT EXIST T:\*.* GOTO NO_T_DRIVE" -ForegroundColor Gray
Write-Host ""
Write-Host "  AFTER (reliable):" -ForegroundColor White
Write-Host "    REM DOS 6.22: Direct file test is most reliable" -ForegroundColor Gray
Write-Host "    IF NOT EXIST T:\*.* GOTO NO_T_DRIVE" -ForegroundColor Gray
Write-Host ""
Write-Host "This should resolve drive detection issues!" -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
