# Push Fixed BAT Files Directly to NAS
# Bypasses AD2 sync - direct deployment to D2TESTNAS
# Date: 2026-01-20

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Direct NAS Deployment (Bypass AD2 Sync)" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Files to deploy (all latest versions)
$Files = @(
    "AUTOEXEC.BAT",
    "UPDATE.BAT",      # v2.2
    "DEPLOY.BAT",
    "NWTOC.BAT",       # v2.2
    "CTONW.BAT",       # v2.1
    "CHECKUPD.BAT",    # v1.2
    "STAGE.BAT",
    "REBOOT.BAT",
    "DOSTEST.BAT"      # v1.1
)

# NAS connection details
$NASHost = "192.168.0.9"
$NASUser = "root"
$DestPath = "/data/test/COMMON/ProdSW"

Write-Host "Copying files to D2TESTNAS..." -ForegroundColor Yellow
Write-Host "Target: $NASHost`:$DestPath" -ForegroundColor Gray
Write-Host ""

$SuccessCount = 0
$FailCount = 0

foreach ($File in $Files) {
    $SourceFile = "D:\ClaudeTools\$File"

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

    Write-Host "  Copying $File..." -NoNewline

    try {
        # Use pscp.exe to copy via SSH (passwordless with key)
        # Convert Windows path to WSL/Linux format for pscp
        $UnixSource = $SourceFile -replace '\\', '/'
        $UnixSource = $UnixSource -replace 'D:', '/mnt/d'

        # Use pscp from PuTTY tools
        & pscp.exe -batch -i C:\Users\MikeSwanson\.ssh\id_ed25519 "$SourceFile" "${NASUser}@${NASHost}:${DestPath}/${File}" 2>&1 | Out-Null

        if ($LASTEXITCODE -eq 0) {
            Write-Host " [OK]" -ForegroundColor Green
            $SuccessCount++
        } else {
            Write-Host " [FAILED]" -ForegroundColor Red
            $FailCount++
        }
    } catch {
        Write-Host " [ERROR] $_" -ForegroundColor Red
        $FailCount++
    }
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deployment Summary" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Successful: $SuccessCount" -ForegroundColor Green
Write-Host "Failed: $FailCount" -ForegroundColor $(if ($FailCount -eq 0) { "Green" } else { "Red" })
Write-Host ""

if ($SuccessCount -gt 0) {
    Write-Host "Files are now available on NAS at:" -ForegroundColor Yellow
    Write-Host "  T:\COMMON\ProdSW\" -ForegroundColor White
    Write-Host ""
    Write-Host "Test on TS-4R immediately:" -ForegroundColor Yellow
    Write-Host "  C:\BAT\NWTOC     - Download updates" -ForegroundColor White
    Write-Host "  C:\BAT\UPDATE    - Test backup" -ForegroundColor White
    Write-Host "  C:\BAT\CHECKUPD  - Check for updates" -ForegroundColor White
    Write-Host ""
    Write-Host "Latest versions deployed:" -ForegroundColor Cyan
    Write-Host "  UPDATE.BAT v2.2 - XCOPY fix + Drive test fix" -ForegroundColor Gray
    Write-Host "  NWTOC.BAT v2.2 - XCOPY fix + Drive test fix" -ForegroundColor Gray
    Write-Host "  CTONW.BAT v2.1 - Drive test fix" -ForegroundColor Gray
    Write-Host "  CHECKUPD.BAT v1.2 - XCOPY fix + Drive test fix" -ForegroundColor Gray
    Write-Host "  DOSTEST.BAT v1.1 - Drive test fix" -ForegroundColor Gray
} else {
    Write-Host "[ERROR] No files copied successfully!" -ForegroundColor Red
    Write-Host "Check SSH key and NAS connectivity" -ForegroundColor Yellow
}
