# Check and fix DOS line endings for batch files
Write-Host "=== Checking DOS Batch File Line Endings ===" -ForegroundColor Cyan
Write-Host ""

# Find all .bat files (excluding git/node_modules)
$batFiles = Get-ChildItem -Recurse -Filter "*.bat" | Where-Object {
    $_.FullName -notlike "*\.git\*" -and
    $_.FullName -notlike "*\node_modules\*"
}

Write-Host "Found $($batFiles.Count) batch files to check:" -ForegroundColor Yellow
Write-Host ""

$needsConversion = @()

foreach ($file in $batFiles) {
    $bytes = [System.IO.File]::ReadAllBytes($file.FullName)
    $hasCRLF = $false
    $hasLF = $false

    for ($i = 0; $i -lt $bytes.Length - 1; $i++) {
        if ($bytes[$i] -eq 13 -and $bytes[$i+1] -eq 10) {
            # Found CRLF (0x0D 0x0A)
            $hasCRLF = $true
            break
        }
        if ($bytes[$i] -eq 10 -and ($i -eq 0 -or $bytes[$i-1] -ne 13)) {
            # Found LF without CR
            $hasLF = $true
            break
        }
    }

    $relativePath = $file.FullName.Replace((Get-Location).Path + "\", "")

    if ($hasCRLF) {
        Write-Host "[OK]   $relativePath" -ForegroundColor Green
        Write-Host "       CRLF (DOS format)" -ForegroundColor Gray
    } elseif ($hasLF) {
        Write-Host "[FAIL] $relativePath" -ForegroundColor Red
        Write-Host "       LF only (Unix format) - NEEDS CONVERSION" -ForegroundColor Red
        $needsConversion += $file
    } else {
        Write-Host "[INFO] $relativePath" -ForegroundColor Yellow
        Write-Host "       No line endings detected (empty or single line)" -ForegroundColor Gray
    }
    Write-Host ""
}

if ($needsConversion.Count -gt 0) {
    Write-Host "=== Files Needing Conversion: $($needsConversion.Count) ===" -ForegroundColor Red
    Write-Host ""

    foreach ($file in $needsConversion) {
        Write-Host "  - $($file.Name)" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "Convert to DOS format? (Y/N)" -ForegroundColor Yellow
    $response = Read-Host

    if ($response -eq 'Y' -or $response -eq 'y') {
        Write-Host ""
        Write-Host "Converting files..." -ForegroundColor Yellow

        foreach ($file in $needsConversion) {
            try {
                $content = Get-Content $file.FullName -Raw
                $dosContent = $content -replace "`r?`n", "`r`n"
                [System.IO.File]::WriteAllText($file.FullName, $dosContent, [System.Text.Encoding]::ASCII)
                Write-Host "  [OK] $($file.Name)" -ForegroundColor Green
            } catch {
                Write-Host "  [ERROR] $($file.Name): $($_.Exception.Message)" -ForegroundColor Red
            }
        }

        Write-Host ""
        Write-Host "=== Conversion Complete ===" -ForegroundColor Green
    } else {
        Write-Host ""
        Write-Host "Conversion skipped." -ForegroundColor Yellow
    }
} else {
    Write-Host "=== All Files OK ===" -ForegroundColor Green
    Write-Host "All batch files have proper DOS (CRLF) line endings." -ForegroundColor Green
}
