# Ensure each .ps1 has exactly ONE UTF-8 BOM so PowerShell 5.1 reads Chinese correctly.
# Idempotent: strips any leading BOMs first, then prepends a single one.
# (PS 5.1 treats BOM-less files as ANSI/GBK; this script is intentionally ASCII-only.)
param([string[]]$Files)
if (-not $Files) {
  $Files = Get-ChildItem (Join-Path $PSScriptRoot '*.ps1') | ForEach-Object { $_.FullName }
}
foreach ($f in $Files) {
  $bytes = [System.IO.File]::ReadAllBytes($f)
  $i = 0
  while (($i + 2) -lt $bytes.Length -and $bytes[$i] -eq 0xEF -and $bytes[$i + 1] -eq 0xBB -and $bytes[$i + 2] -eq 0xBF) { $i += 3 }
  if ($i -gt 0) { $bytes = $bytes[$i..($bytes.Length - 1)] }
  $out = New-Object byte[] ($bytes.Length + 3)
  $out[0] = 0xEF; $out[1] = 0xBB; $out[2] = 0xBF
  [Array]::Copy($bytes, 0, $out, 3, $bytes.Length)
  [System.IO.File]::WriteAllBytes($f, $out)
  Write-Output ("BOM fixed: " + $f)
}
