#! /bin/sh

set -e

. /usr/share/debconf/confmodule

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

# Set locale to C if it has not yet been set
# This can happen during e.g. s390 installs where localechooser is not run
[ "$LOCALE" ] || LOCALE="C"

if [ "$LOCALE" != "C" ] ; then
	db_get debian-installer/language
	LANGLIST="$RET"
fi

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

LANGUAGE="${LOCALE%%_*}"

# Enable translations. We use belocs-locales-data for languages which
# don't have a locale in the locales package.
if [ "$LOCALE" != "C" ] || [ "$EXTRAS" ]; then
	# Which locale package to install
	case "$LANGUAGE" in
		ku|sa)
			apt-install belocs-locales-data || true
			apt-install belocs-locales-bin || true
			;;
		*)
			apt-install locales || true
			;;
	esac
fi

# Set global locale and language, and make sure the glibc locale is
# generated.
DESTFILE="/target/etc/environment"
echo "LANG=\"$LOCALE\"" >> $DESTFILE
if [ "$LANGLIST" ]; then
	echo "LANGUAGE=\"$LANGLIST\"" >> $DESTFILE
fi
if [ "$LOCALE" != C ]; then
	log-output -t localechooser chroot /target /usr/sbin/locale-gen "$LOCALE"
fi
if [ "$EXTRAS" ]; then
	log-output -t localechooser chroot /target /usr/sbin/locale-gen $EXTRAS
fi

exit 0
