#! /bin/sh
set -e

# Files
LG="/etc/locale.gen"
EE="/etc/locale.conf"

# Sanitize environnement
LC_ALL=C
LANG=C

# Load debconf
. /usr/share/debconf/confmodule
db_version 2.0
db_capb backup multiselect

# Conversion of locales that have been removed 
convert_locale()
{
    echo "$1" | sed -e "s/no_NO/nb_NO/g" -e 's/ks_IN/ks_IN@devanagari/g' -e 's/iw_IL/he_IL/g'
}

# List of locales provided by the current version
PROVIDED_LOCALES="aa_DJ.UTF-8 UTF-8
aa_ER UTF-8
aa_ET UTF-8
af_ZA.UTF-8 UTF-8
agr_PE UTF-8
ak_GH UTF-8
am_ET UTF-8
an_ES.UTF-8 UTF-8
anp_IN UTF-8
ar_AE.UTF-8 UTF-8
ar_BH.UTF-8 UTF-8
ar_DZ.UTF-8 UTF-8
ar_EG.UTF-8 UTF-8
ar_IN UTF-8
ar_IQ.UTF-8 UTF-8
ar_JO.UTF-8 UTF-8
ar_KW.UTF-8 UTF-8
ar_LB.UTF-8 UTF-8
ar_LY.UTF-8 UTF-8
ar_MA.UTF-8 UTF-8
ar_OM.UTF-8 UTF-8
ar_QA.UTF-8 UTF-8
ar_SA.UTF-8 UTF-8
ar_SD.UTF-8 UTF-8
ar_SS UTF-8
ar_SY.UTF-8 UTF-8
ar_TN.UTF-8 UTF-8
ar_YE.UTF-8 UTF-8
ayc_PE UTF-8
az_AZ UTF-8
az_IR UTF-8
as_IN UTF-8
ast_ES.UTF-8 UTF-8
be_BY.UTF-8 UTF-8
be_BY@latin UTF-8
bem_ZM UTF-8
ber_DZ UTF-8
ber_MA UTF-8
bg_BG.UTF-8 UTF-8
bhb_IN.UTF-8 UTF-8
bho_IN UTF-8
bho_NP UTF-8
bi_VU UTF-8
bn_BD UTF-8
bn_IN UTF-8
bo_CN UTF-8
bo_IN UTF-8
br_FR.UTF-8 UTF-8
brx_IN UTF-8
bs_BA.UTF-8 UTF-8
byn_ER UTF-8
ca_AD.UTF-8 UTF-8
ca_ES.UTF-8 UTF-8
ca_ES@valenc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_RS@latin UTF-8
ss_ZA UTF-8
ssy_ER UTF-8
st_ZA.UTF-8 UTF-8
su_ID UTF-8
sv_FI.UTF-8 UTF-8
sv_SE.UTF-8 UTF-8
sw_KE UTF-8
sw_TZ UTF-8
syr UTF-8
szl_PL UTF-8
ta_IN UTF-8
ta_LK UTF-8
tcy_IN.UTF-8 UTF-8
te_IN UTF-8
tg_TJ.UTF-8 UTF-8
th_TH.UTF-8 UTF-8
the_NP UTF-8
ti_ER UTF-8
ti_ET UTF-8
tig_ER UTF-8
tk_TM UTF-8
tl_PH.UTF-8 UTF-8
tn_ZA UTF-8
to_TO UTF-8
tok UTF-8
tpi_PG UTF-8
tr_CY.UTF-8 UTF-8
tr_TR.UTF-8 UTF-8
ts_ZA UTF-8
tt_RU UTF-8
tt_RU@iqtelif UTF-8
ug_CN UTF-8
ug_CN@latin UTF-8
uk_UA.UTF-8 UTF-8
unm_US UTF-8
ur_IN UTF-8
ur_PK UTF-8
uz_UZ.UTF-8 UTF-8
uz_UZ@cyrillic UTF-8
ve_ZA UTF-8
vi_VN UTF-8
wa_BE.UTF-8 UTF-8
wae_CH UTF-8
wal_ET UTF-8
wo_SN UTF-8
xh_ZA.UTF-8 UTF-8
yi_US.UTF-8 UTF-8
yo_NG UTF-8
yue_HK UTF-8
yuw_PG UTF-8
zgh_MA UTF-8
zh_CN.UTF-8 UTF-8
zh_HK.UTF-8 UTF-8
zh_SG.UTF-8 UTF-8
zh_TW.UTF-8 UTF-8
zu_ZA.UTF-8 UTF-8
"

# List of locales provided by the user
if [ -f /usr/local/share/i18n/SUPPORTED ] ; then
    USER_LOCALES="$(sed -e '/^[a-zA-Z]/!d' -e 's/ *$//g' /usr/local/share/i18n/SUPPORTED)"
fi

# List of locales in /etc/locale.gen
if [ -e $LG ]; then
  GEN_LOCALES="$(sed -e '/^[a-zA-Z]/!d' -e 's/ *$//g' $LG)"
  GEN_LOCALES="$(convert_locale "$GEN_LOCALES")"
fi

# List of supported locales (PROVIDED_LOCALES + USER_LOCALES + GEN_LOCALES)
SUPPORTED_LOCALES="$(printf '%s\n' "$PROVIDED_LOCALES" "$USER_LOCALES" "$GEN_LOCALES" | grep -v "^$" | sort -u | tr '\n' ',' | sed -e 's/, */, /g' -e 's/, *$//g')"
db_subst locales/locales_to_be_generated locales "$SUPPORTED_LOCALES"

# Get the list of selected locales from /etc/locale.gen
if [ -e /etc/locale.gen ]; then
    if [ -L $LG ] && [ "$(readlink $LG)" = "/usr/share/i18n/SUPPORTED" ]; then
        SELECTED_LOCALES="All locales"
    else
        SELECTED_LOCALES="$(echo "$GEN_LOCALES" | sort -u | tr '\n' ',' | sed -e 's/, */, /g' -e 's/, *$//g')"
    fi
    db_set locales/locales_to_be_generated "$SELECTED_LOCALES"
fi

DEFAULT_ENVIRONMENT="$(cat /etc/default/locale /etc/locale.conf 2>/dev/null | awk '/^LANG=/ {gsub("\"", ""); sub("LANG=", ""); lang=$0;} END {print lang}')"
DEFAULT_ENVIRONMENT="$(convert_locale "$DEFAULT_ENVIRONMENT")"
if [ -n "$SUPPORTED_LOCALES" ] && [ -n "$DEFAULT_ENVIRONMENT" ]; then
  if echo "$SUPPORTED_LOCALES" | grep -q -e "\b$DEFAULT_ENVIRONMENT\b" ; then
    db_set locales/default_environment_locale "$DEFAULT_ENVIRONMENT"
  fi
fi

STATE=1
while [ "$STATE" -ge 0 ]; do
    case "$STATE" in
    0)
        exit 1
        ;;
    1)
        db_input medium locales/locales_to_be_generated || true
        ;;
    2)
        db_get locales/locales_to_be_generated || RET=
        if expr ", $RET," : ".*, None,.*" >/dev/null 2>&1; then
            # "None" was a choice in older packages
            db_set locales/locales_to_be_generated ""
            RET=
        elif expr ", $RET," : ".*, All locales,.*" >/dev/null 2>&1; then
            # When "All locales" is selected, other choices have to be dropped
            db_set locales/locales_to_be_generated "All locales"
            RET=$SUPPORTED_LOCALES
        fi
        DEFAULT_LOCALES="$(echo $RET | sed -e 's/ [^ ]*,/,/g' -e 's/ [^ ]*$//')"
        for langpack in /var/lib/locales/supported.d/*; do
            if [ -f $langpack ]; then
                while read locale charset; do
                    DEFAULT_LOCALES="$DEFAULT_LOCALES, $locale"
                done < $langpack
            fi
        done
        if [ -n "$DEFAULT_LOCALES" ]; then
            db_subst locales/default_environment_locale locales $DEFAULT_LOCALES
            db_input medium locales/default_environment_locale || true
        fi
        ;;
    *)
        break
        ;;
    esac
    if db_go; then
        STATE=$(($STATE + 1))
    else
        STATE=$(($STATE - 1))
    fi
done
