$files = Get-ChildItem -Path "site\*.html", "site\tools\*.html", "site\blog\articles\*.html" -Recurse

# Map of corrupted byte sequences to their correct Unicode characters
$replacements = @{
    # Â· -> ·
    ([System.Text.Encoding]::UTF8.GetString([byte[]](195, 130, 194, 183))) = "·"
    
    # â€” -> — (em-dash)
    ([System.Text.Encoding]::UTF8.GetString([byte[]](195, 162, 226, 130, 172, 226, 128, 157))) = "—"
    
    # â€“ -> – (en-dash)
    ([System.Text.Encoding]::UTF8.GetString([byte[]](195, 162, 226, 130, 172, 226, 128, 147))) = "–"
    
    # â†’ -> →
    ([System.Text.Encoding]::UTF8.GetString([byte[]](195, 162, 226, 128, 160, 226, 128, 153))) = "→"
    
    # â€™ -> ’ (right single quote)
    ([System.Text.Encoding]::UTF8.GetString([byte[]](195, 162, 226, 130, 172, 226, 132, 162))) = "’"
    
    # â€œ -> “ (left double quote)
    ([System.Text.Encoding]::UTF8.GetString([byte[]](195, 162, 226, 130, 172, 197, 147))) = "“"
    
    # â€ -> ” (right double quote)
    ([System.Text.Encoding]::UTF8.GetString([byte[]](195, 162, 226, 130, 172, 194, 157))) = "”"
}

foreach ($file in $files) {
    if ($file.Extension -ne ".html") { continue }
    
    $content = [IO.File]::ReadAllText($file.FullName, [System.Text.Encoding]::UTF8)
    $modified = $false
    
    foreach ($key in $replacements.Keys) {
        if ($content.Contains($key)) {
            $content = $content.Replace($key, $replacements[$key])
            $modified = $true
        }
    }
    
    if ($modified) {
        [IO.File]::WriteAllText($file.FullName, $content, [System.Text.Encoding]::UTF8)
        Write-Host "Fixed mojibake in $($file.Name)"
    }
}
Write-Host "All remaining mojibake fixed safely."
