#!/bin/sh -e

. /usr/share/debconf/confmodule

localecode="debian-installer/locale"
fallbacklocalecode="debian-installer/fallbacklocale"
# We need to remember which locale has been set in languagechooser
# as countrychooser will also be able to change debian-installer/locale
languagechooserlocalecode="languagechooser/locale"
languagecode="debian-installer/language"
countrycode="debian-installer/country"
consoledisplay="debian-installer/consoledisplay"
ARCH=`udpkg --print-architecture`

log() {
    logger -t languagechooser "info: $@"
}

# debconf/language is an alias for debian-installer/language
db_register "$languagecode" "debconf/language"

# Only display the translated texts (ie the English "translation")
# when in UTF-8 mode.
if ( echo $LANG $LC_CTYPE | grep -q UTF-8 ); then
  db_set debconf/language en
else
  db_set debconf/language C
fi

db_input critical "languagechooser/language-name" || [ $? -eq 30 ]
if db_go; then
    db_get "languagechooser/language-name"
    if test "$RET" ; then
	LANGNAME="$RET"
	. languagemap
	db_set "$languagecode" "$LANGUAGELIST"
	db_set "$localecode"   "$LOCALE"
	db_set "$fallbacklocalecode"   "$FALLBACKLOCALE"
	# For "remembering" which locale is set by languagechooser
	db_set "$languagechooserlocalecode"   "$LOCALE"
	db_set "$countrycode"  "$COUNTRY"
	db_set "$consoledisplay"  "$CONSOLE"
    else
	# Error, not sure how to handle it
	:
    fi
else
    # Error, not sure how to handle it
    :
fi

db_get "$languagecode"
db_set "debconf/language" "$LANGUAGELIST"

log "Asking for language specific packages to be Installed."

case "$LANGUAGE" in
    ar|el|he|ja|ko|tr|zh)
	anna-install bterm-unifont
	;;
esac

# Enable translations (if this is present, base-config generated the
# required locale on boot)
apt-install locales || true

if [ "$LANGUAGE" != "en" ]; then
    # Package iso-codes is missing in Woody.  Do not fail if it is
    # missing to make d-i capable of installing Woody and Skolelinux.
    apt-install iso-codes || true
    apt-install localization-config || true
fi

case "$LANGUAGE" in
    ja|ko|ko_KR|el|zh|zh_CN|zh_TW|bg)
        # Japanese, Korean, Greek, Chinese, Bulgarian
        apt-install jfbterm || true
        apt-install unifont || true
        ;;
#    ru)
        # Russian
#        apt-install console-cyrillic || true
#        apt-install console-terminus || true
#        ;;
    tr)
        # Turkish
        apt-install console-terminus || true
        ;;
    ar|he|fa)
	# RTL languages (Arabic, Hebrew, Farsi)
        apt-install jfbterm || true
        apt-install unifont || true
	apt-install libfribidi0 || true
	;;
    *)
        ;;
esac

log "$localecode   = '$LOCALE'"
log "$fallbacklocalecode   = '$FALLBACKLOCALE'"
log "$languagechooserlocalecode   = '$LOCALE'"
log "$languagecode = '$LANGUAGELIST'"
log "$countrycode  = '$COUNTRY'"
log "$consoledisplay  = '$CONSOLE'"

exit 0
