#!/bin/bash

# omarchy:summary=Generate the system locale for an Omarchy language
# omarchy:args=<code>
# omarchy:hidden=true

set -euo pipefail

# Runs as root through the passwordless rule in etc/sudoers.d/omarchy-locale-generate,
# which names each registry code as the whole argument. Under sudo's
# secure_path a dev link prepends a user-writable checkout bin/ to PATH, and
# every helper this calls by bare name (grep, sed, localedef) is a system
# tool, so pin PATH to trusted system directories while holding root. sudo
# keeps the caller's directory, where localedef looks for a source first.
if (( EUID == 0 )); then
  export PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
  cd /
fi

# Root reads only the packaged registry and the real locale.gen; the
# overrides are for unprivileged tests (same convention as omarchy-language).
registry=/usr/share/omarchy/default/i18n/locales.json
locale_gen=/etc/locale.gen
if (( EUID != 0 )); then
  registry=${OMARCHY_LOCALE_REGISTRY:-$registry}
  locale_gen=${OMARCHY_LOCALE_GEN:-$locale_gen}
fi

code=${1:-}
if (( $# != 1 )) || [[ ! $code =~ ^[a-z]{2,3}(-[A-Za-z]{2})?$ ]]; then
  echo "Usage: omarchy-locale-generate <code>" >&2
  exit 1
fi

# The code only selects a locale from the registry; nothing the caller passes
# reaches locale.gen or localedef itself.
locale=$(jq -r --arg code "$code" '.[$code].locale // empty' "$registry")
if [[ ! $locale =~ ^[a-z]{2,3}_[A-Z]{2}\.UTF-8$ ]]; then
  echo "Unknown language code: $code" >&2
  exit 1
fi

# locale.gen spells some entries without the codeset (hi_IN UTF-8).
base=${locale%.UTF-8}
if grep -qE "^#$base(\.UTF-8)? UTF-8" "$locale_gen" 2>/dev/null; then
  sed -i -E "s/^#($base(\.UTF-8)? UTF-8)/\1/" "$locale_gen"
elif ! grep -qE "^$base(\.UTF-8)? UTF-8" "$locale_gen" 2>/dev/null; then
  echo "$locale UTF-8" >>"$locale_gen"
fi

# locale-gen would delete the archive and rebuild every locale, ignoring each
# failure; add only this one, and ask the archive whether it landed, since
# localedef exits 1 both for warnings and for a locale it could not add.
# Like locale-gen, a source under /usr/local overrides the packaged one.
input=$base
for name in "$locale" "$base"; do
  if [[ -e /usr/local/share/i18n/locales/$name ]]; then
    input=/usr/local/share/i18n/locales/$name
    break
  fi
done
localedef -i "$input" -c -f UTF-8 -A /usr/share/locale/locale.alias "$locale" >/dev/null || true
if ! localedef --list-archive | grep -Fx "$base.utf8" >/dev/null; then
  echo "Could not generate $locale" >&2
  exit 1
fi
