# Remove redundant ECHO lines before PAUSE
# DOS 6.22 PAUSE already displays "Press any key to continue..."

$BATFiles = Get-ChildItem *.BAT | Where-Object {
    $_.Name -notlike "*_FROM_*" -and
    $_.Name -notlike "*_TEST*" -and
    $_.Name -notlike "*_VERIFY*" -and
    $_.Name -notlike "*_CHECK*" -and
    $_.Name -notlike "*_MONITOR*"
}

Write-Host "[INFO] Removing redundant ECHO before PAUSE" -ForegroundColor Cyan
Write-Host ""

$TotalRemoved = 0

foreach ($File in $BATFiles) {
    Write-Host "Processing: $($File.Name)" -ForegroundColor White

    $Content = Get-Content $File.FullName -Raw

    # Remove "ECHO Press any key..." lines immediately before PAUSE
    # Pattern: ECHO Press any key...\r?\n\s*PAUSE
    $OriginalContent = $Content

    $Content = $Content -replace 'ECHO Press any key[^\r\n]*\r?\n(\s*)PAUSE', '$1PAUSE'

    if ($Content -ne $OriginalContent) {
        $RemovedCount = ([regex]::Matches($OriginalContent, 'ECHO Press any key')).Count - ([regex]::Matches($Content, 'ECHO Press any key')).Count
        Set-Content $File.FullName $Content -NoNewline
        Write-Host "  [OK] Removed $RemovedCount redundant ECHO lines" -ForegroundColor Green
        $TotalRemoved += $RemovedCount
    } else {
        Write-Host "  [OK] No redundant ECHO lines found" -ForegroundColor Green
    }
}

Write-Host ""
Write-Host "[SUCCESS] Removed $TotalRemoved redundant ECHO lines" -ForegroundColor Green
Write-Host ""
Write-Host "DOS 6.22 PAUSE displays:" -ForegroundColor Cyan
Write-Host "  'Press any key to continue . . .'" -ForegroundColor White
Write-Host ""
Write-Host "No need for ECHO before PAUSE unless:" -ForegroundColor Cyan
Write-Host "  - Custom message needed (not 'Press any key...')" -ForegroundColor White
Write-Host ""
