#!/usr/bin/env bash
set -euo pipefail

# Windows PowerShell 5.1 can parse UTF-8-without-BOM scripts through the
# system code page. Keep shipped installer scripts ASCII-only to avoid mojibake
# turning punctuation bytes into quote tokens.

if [[ "$#" -eq 0 ]]; then
  echo "Usage: $0 <powershell-installer.ps1> [...]"
  exit 2
fi

failed=0
for file in "$@"; do
  output_file="$(mktemp)"
  if [[ ! -f "$file" ]]; then
    echo "Missing PowerShell installer: $file" >&2
    failed=1
    rm -f "$output_file"
    continue
  fi
  if LC_ALL=C grep -n '[^ -~	]' "$file" >"$output_file" 2>/dev/null; then
    echo "Non-ASCII bytes found in PowerShell installer: $file" >&2
    cat "$output_file" >&2
    failed=1
  fi
  rm -f "$output_file"
done

exit "$failed"
