# Manually copy fixed BAT files from AD2 to NAS immediately
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)

Write-Host "=== Copying BAT Files to NAS ===" -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    $SCP = "C:\Program Files\OpenSSH\scp.exe"
    $NAS_IP = "192.168.0.9"
    $NAS_USER = "admin"

    Write-Host "[1] Copying root-level BAT files to NAS" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

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

    $successCount = 0
    $errorCount = 0

    foreach ($file in $rootFiles) {
        $localPath = "C:\Shares\test\$file"
        $remotePath = "/volume1/test/$file"

        if (Test-Path $localPath) {
            Write-Host "Copying $file..." -ForegroundColor Cyan

            $result = & $SCP -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts" -o PreferredAuthentications=password -o PubkeyAuthentication=no -o PasswordAuthentication=yes $localPath "${NAS_USER}@${NAS_IP}:$remotePath" 2>&1

            if ($LASTEXITCODE -eq 0) {
                Write-Host "  [OK] $file" -ForegroundColor Green
                $successCount++
            } else {
                Write-Host "  [ERROR] $file - $result" -ForegroundColor Red
                $errorCount++
            }
        } else {
            Write-Host "  [SKIP] $file - not found" -ForegroundColor Yellow
        }
    }

    Write-Host ""
    Write-Host "[2] Creating COMMON/DOS directory on NAS" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    # SSH to NAS and create directory
    $SSH = "C:\Program Files\OpenSSH\ssh.exe"
    $mkdirResult = & $SSH -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts" -o PreferredAuthentications=password -o PubkeyAuthentication=no -o PasswordAuthentication=yes "${NAS_USER}@${NAS_IP}" "mkdir -p /volume1/test/COMMON/DOS" 2>&1

    if ($LASTEXITCODE -eq 0) {
        Write-Host "[OK] Directory created/verified" -ForegroundColor Green
    } else {
        Write-Host "[WARNING] Directory creation: $mkdirResult" -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "[3] Copying DOS system files to NAS" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    $dosFiles = @("AUTOEXEC.BAT", "STARTNET.BAT")

    foreach ($file in $dosFiles) {
        $localPath = "C:\Shares\test\COMMON\DOS\$file"
        $remotePath = "/volume1/test/COMMON/DOS/$file"

        if (Test-Path $localPath) {
            Write-Host "Copying $file..." -ForegroundColor Cyan

            $result = & $SCP -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts" -o PreferredAuthentications=password -o PubkeyAuthentication=no -o PasswordAuthentication=yes $localPath "${NAS_USER}@${NAS_IP}:$remotePath" 2>&1

            if ($LASTEXITCODE -eq 0) {
                Write-Host "  [OK] $file" -ForegroundColor Green
                $successCount++
            } else {
                Write-Host "  [ERROR] $file - $result" -ForegroundColor Red
                $errorCount++
            }
        } else {
            Write-Host "  [SKIP] $file - not found" -ForegroundColor Yellow
        }
    }

    Write-Host ""
    Write-Host "=" * 80 -ForegroundColor Gray
    Write-Host "Summary: $successCount successful, $errorCount errors" -ForegroundColor Cyan
}

Write-Host ""
Write-Host "=== Copy Complete ===" -ForegroundColor Cyan
Write-Host "[INFO] Files are now available on NAS at /volume1/test/" -ForegroundColor Green
Write-Host "[INFO] DOS machines can access via T:\ drive" -ForegroundColor Green
