# Script to convert line endings from Unix (LF) to Windows (CRLF) format
param(
    [Parameter(Mandatory=$true)]
    [string]$FolderPath,
    
    [Parameter(Mandatory=$false)]
    [string[]]$FileExtensions = @("*.txt", "*.cs", "*.json", "*.xml", "*.config", "*.ps1")
)

# Verify the folder exists
if (-not (Test-Path -Path $FolderPath -PathType Container)) {
    Write-Error "The specified folder does not exist: $FolderPath"
    exit 1
}

Write-Host "Converting line endings in folder: $FolderPath"

foreach ($extension in $FileExtensions) {
    $files = Get-ChildItem -Path $FolderPath -Filter $extension -Recurse -File
    
    foreach ($file in $files) {
        try {
            Write-Host "Processing: $($file.FullName)"
            
            # Read file as bytes to properly handle line endings
            $bytes = [System.IO.File]::ReadAllBytes($file.FullName)
            $content = [System.Text.Encoding]::UTF8.GetString($bytes)
            
            # Replace only LF (not preceded by CR) with CRLF
            $newContent = $content -replace "(?<!\r)\n", "\r\n"
            
            # Only write if content has changed
            if ($content -ne $newContent) {
            
            if ($content) {
                [System.IO.File]::WriteAllText($file.FullName, $newContent)
                Write-Host "Converted: $($file.FullName)" -ForegroundColor Green
            } else {
                Write-Host "No changes needed: $($file.FullName)" -ForegroundColor Yellow
            }
        }
        catch {
            Write-Error "Error processing file $($file.FullName): $_"
        }
    }
}

Write-Host "`nConversion complete!" -ForegroundColor Green