#!/bin/sh

set -e

LOCALEGEN=/etc/locale.gen
LOCALES=/usr/share/i18n/locales
LOCALES_OVERRIDES=/usr/local/share/i18n/locales

if [ -n "$POSIXLY_CORRECT" ]; then
  unset POSIXLY_CORRECT
fi


[ -f $LOCALEGEN -a -s $LOCALEGEN ] || exit 0;

# Remove all old locale dir and locale-archive before generating new
# locale data.
rm -rf /usr/lib/locale/locale-archive || true

umask 022

is_entry_ok() {
  if [ -z "$locale" ] || [ -z "$charset" ]; then
    echo "error: Bad entry '$locale $charset'"
    return 1
  fi
}

echo "Generating locales..."
while read -r locale charset; do
  if [ -z "$locale" ] || [ "${locale#\#}" != "$locale" ]; then continue; fi
  is_entry_ok || continue

  # strip charmap and at-suffix for locale base
  locale_base="${locale%%.*}"
  locale_base="${locale_base%%@*}"

  # optional at-suffix
  locale_at="${locale#*@}"
  [ "$locale_at" = "$locale" ] && locale_at= || locale_at="@$locale_at"

  printf "  %s.%s%s..." "$locale_base" "$charset" "$locale_at"

  # fully matching override locale
  if [ -e "$LOCALES_OVERRIDES/$locale" ]; then
    input="$LOCALES_OVERRIDES/$locale"
  # fully matching vendor locale
  elif [ -e "$LOCALES/$locale" ]; then
    input="$locale"
  # fallback to partial matches
  else
    # locale base with optional at-suffix
    input="$locale_base$locale_at"
    # override partial locale
    if [ -e "$LOCALES_OVERRIDES/$input" ]; then
      input="$LOCALES_OVERRIDES/$input"
    fi
  fi

  localedef -i "$input" -c -f "$charset" -A /usr/share/locale/locale.alias "$locale" || :
  echo ' done'
done < $LOCALEGEN
echo "Generation complete."
