#! /bin/sh
set -e

. /usr/share/debconf/confmodule

supported () {
	grep -q "^$1 " /usr/share/i18n/SUPPORTED
}

# Recover from broken debian-installer/locale settings.
db_get debian-installer/locale
LOCALE="$RET"
if ! supported "$LOCALE"; then
	db_set debian-installer/locale ''
fi

db_set localechooser/alreadyrun false

CODE=$?
PATH="/usr/lib/oem-config/compat:/usr/lib/oem-config/language:$PATH" \
	localechooser || CODE="$?"

# We need to duplicate this code because localechooser has the right to
# assume that its later scripts are only run once, but oem-config doesn't
# have that luxury.

db_get debian-installer/locale
LOCALE="$RET"

db_get debian-installer/language
LANGLIST="$RET"

db_get localechooser/supported-locales
EXTRAS="$(echo "$RET" | sed 's/,//g')"

if [ "$CODE" = 0 ]; then
	if [ -e /etc/environment ]; then
		sed -i "s,^LANG=.*,LANG=\"$LOCALE\"," /etc/environment
	fi
	if [ -e /etc/default/locale ]; then
		sed -i "s,^LANG=.*,LANG=\"$LOCALE\"," /etc/default/locale
	fi
	# We set LANGUAGE only if the languagelist is a list of
	# languages with alternatives. Otherwise, setting it is useless
	if echo "$LANGLIST" | grep -q ":"; then
		if grep -q "^LANGUAGE=" /etc/environment; then
			sed -i "s,^LANGUAGE=.*,LANGUAGE=\"$LANGLIST\"," /etc/environment
		else
			echo "LANGUAGE=\"$LANGLIST\"" >> /etc/environment
		fi
		if grep -q "^LANGUAGE=" /etc/default/locale; then
			sed -i "s,^LANGUAGE=.*,LANGUAGE=\"$LANGLIST\"," /etc/default/locale
		else
			echo "LANGUAGE=\"$LANGLIST\"" >> /etc/default/locale
		fi
	else
		if [ -e /etc/environment ]; then
			sed -i "/^LANGUAGE=/d" /etc/environment
		fi
		if [ -e /etc/default/locale ]; then
			sed -i "/^LANGUAGE=/d" /etc/default/locale
		fi
	fi

	if [ -e /etc/default/gdm ]; then
		sed -i "s,^#*LANG=.*,LANG=$LOCALE,g" /etc/default/gdm
	fi
fi

# TODO: kbd/cyr handling?

exit "$CODE"
