# Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

# Script to normalize line endings in text files
Write-Host "Normalizing line endings in workspace files to LF..." -ForegroundColor Cyan

# Text file extensions to process
$textExtensions = @("*.ps1", "*.txt", "*.json", "*.js", "*.py", "*.csv", "*.md", "*.ini", "*.sh")

# Get all text files
$textFiles = Get-ChildItem -Path $PSScriptRoot -Include $textExtensions -Recurse -File

$count = 0

foreach ($file in $textFiles) {
    Write-Host "Processing: $($file.Name)" -ForegroundColor DarkGray
    
    try {
        # Read file content with current line endings
        $content = Get-Content -Path $file.FullName -Raw -ErrorAction Stop
        
        if ($content) {
            # Replace CRLF with LF
            $normalizedContent = $content.Replace("`r`n", "`n")
            
            # Check if content was changed
            if ($normalizedContent -ne $content) {
                # Write back with LF endings
                $utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
                [System.IO.File]::WriteAllText($file.FullName, $normalizedContent, $utf8NoBomEncoding)
                
                Write-Host "  - Normalized: $($file.Name)" -ForegroundColor Green
                $count++
            } else {
                Write-Host "  - Already has LF endings: $($file.Name)" -ForegroundColor DarkGray
            }
        }
    } catch {
        Write-Host "  - Error processing $($file.Name): $_" -ForegroundColor Red
    }
}

Write-Host "`nNormalization complete! Converted $count files to LF line endings." -ForegroundColor Cyan

# Update your Git configuration for this repository
Write-Host "`nUpdating Git configuration to use LF line endings..." -ForegroundColor Cyan

try {
    # Ensure Git uses LF in the repo
    & git config core.eol lf
    & git config core.autocrlf false

    Write-Host "Git configuration updated." -ForegroundColor Green
    Write-Host "- core.eol: lf" -ForegroundColor DarkGray
    Write-Host "- core.autocrlf: false" -ForegroundColor DarkGray
} catch {
    Write-Host "Failed to update Git configuration: $_" -ForegroundColor Red
}

Write-Host "`nTo verify changes, run your update check again." -ForegroundColor Yellow
