# Deploy all DOS BAT files to AD2 for distribution to DOS machines

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

# Files to deploy
$BATFiles = @(
    "UPDATE.BAT",      # Machine backup utility
    "NWTOC.BAT",       # Network to Computer (pull updates)
    "CTONW.BAT",       # Computer to Network (push files)
    "CHECKUPD.BAT",    # Check for updates
    "REBOOT.BAT",      # Reboot utility
    "DEPLOY.BAT"       # One-time deployment installer
)

Write-Host "[INFO] Connecting to AD2..."
New-PSDrive -Name TEMP_AD2 -PSProvider FileSystem -Root "\\192.168.0.6\C$" -Credential $Cred -ErrorAction Stop | Out-Null

Write-Host "[INFO] Deploying BAT files to AD2..."
Write-Host ""

$TotalFiles = 0
$SuccessCount = 0

foreach ($File in $BATFiles) {
    if (Test-Path $File) {
        Write-Host "  Processing: $File"

        # Verify CRLF before copying
        $Content = Get-Content $File -Raw
        if ($Content -match "`r`n") {
            Write-Host "    [OK] CRLF verified"

            # Copy to root (for DEPLOY.BAT and UPDATE.BAT access)
            if ($File -in @("DEPLOY.BAT", "UPDATE.BAT")) {
                Copy-Item $File "TEMP_AD2:\Shares\test\" -Force
                Write-Host "    [OK] Copied to root: C:\Shares\test\"
            }

            # Copy to COMMON\ProdSW (for all machines)
            Copy-Item $File "TEMP_AD2:\Shares\test\COMMON\ProdSW\" -Force
            Write-Host "    [OK] Copied to COMMON\ProdSW\"

            # Also copy to _COMMON\ProdSW (backup location)
            Copy-Item $File "TEMP_AD2:\Shares\test\_COMMON\ProdSW\" -Force
            Write-Host "    [OK] Copied to _COMMON\ProdSW\"

            $SuccessCount++
        } else {
            Write-Host "    [ERROR] No CRLF found - skipping"
        }

        $TotalFiles++
        Write-Host ""
    } else {
        Write-Host "  [WARNING] $File not found - skipping"
        Write-Host ""
    }
}

Remove-PSDrive TEMP_AD2

Write-Host "[SUCCESS] Deployment complete"
Write-Host "  Files processed: $TotalFiles"
Write-Host "  Files deployed: $SuccessCount"
Write-Host ""
Write-Host "[INFO] Files will sync to NAS within 15 minutes"
Write-Host "[INFO] DOS machines can then run NWTOC to get updates"
