#! /bin/sh

set -e

. /usr/share/debconf/confmodule

# Avoid locale errors when using apt-install (logical consequence
# of the fact that locales is not yet installed).
export IT_LANG_OVERRIDE=C

log() {
	logger -t localechooser "$@"
}

error() {
	log "error: $@"
}

# If locale is invalid, add it to locale.gen
add_if_invalid() {
	local locale=$1

	# Use LANG=C to avoid locale errors from perl
	if LANG=C chroot /target /usr/sbin/validlocale "$locale" \
	   >>/target/etc/locale.gen 2>/dev/null; then
		# locale is valid and available
		return 1
	else
		log "Adding locale '$locale'"
	fi
}


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
if [ "$LOCALE" != "C" ] || [ "$EXTRAS" ]; then
	apt-install locales || true
fi

# Set global locale and language, and make sure the glibc locale is
# generated.
DESTFILE="/target/etc/default/locale"
if [ -e $DESTFILE ]; then
	sed -i 's/^# LANG=$/LANG=\"'"$LOCALE"'\"/' $DESTFILE
	# 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
		sed -i 's/^# LANGUAGE=$/LANGUAGE=\"'"$LANGLIST"'\"/' $DESTFILE
	fi
fi
# Fallback in case the file wasn't provided by locales, or the format
# changed.
if [ ! -e "$DESTFILE" ] || ! grep -q '^LANG=' $DESTFILE; then
	mkdir -p "${DESTFILE%/*}"
	echo "LANG=\"$LOCALE\"" >> $DESTFILE
	if echo "$LANGLIST" | grep -q ":"; then
		echo "LANGUAGE=\"$LANGLIST\"" >> $DESTFILE
	fi
fi

# For languages that have no chance to be displayed at the Linux console
# let's set root's environment with a non localized environment
ROOTPROFILE="/target/root/.profile"
# We must map the language to its "level" from languagelist
LANGUAGECODE=`echo $LOCALE|cut -f1 -d_`
# For language with multiple entries such as pt/pt_BR or zh_CN/zh_TW
# we don't really care about the entry we will match as the level will always
# be the same
LEVEL=`cat /usr/share/localechooser/languagelist |\
	cut -f 2-3 -d\; | \
	grep "$LANGUAGECODE" | \
	head -n 1 | \
	cut -f1 -d\;`
if [ "$LEVEL" = "3" ] || [ "$LEVEL" = "4" ]; then
	echo "# Installed by Debian Installer:" >>$ROOTPROFILE
	echo "#  no localization for root because $LOCALE" >>$ROOTPROFILE
	echo "#  cannot be properly displayed at the Linux console" >>$ROOTPROFILE
	echo "LANG=C" >>$ROOTPROFILE
	echo "LANGUAGE=C" >>$ROOTPROFILE
fi

# Generate selected locales that are not already valid
if ([ "$LOCALE" != "C" ] || [ "$EXTRAS" ]) && \
   [ -x /target/usr/sbin/validlocale ]; then
	gen=""
	if add_if_invalid "$LOCALE"; then
		gen=1
	fi
	for loc in $EXTRAS; do
		if [ "$loc" != "$LOCALE" ] && \
		   add_if_invalid "$loc"; then
			gen=1
		fi
	done
	if [ "$gen" ]; then
		if [ -x /target/usr/sbin/locale-gen ]; then
			log "Generating added locales..."
			log-output -t localechooser --pass-stdout \
				chroot /target /usr/sbin/locale-gen \
				--keep-existing > /dev/null
		else
			error "the command 'locale-gen' is not available"
		fi
	fi
else
	error "the command 'validlocale' is not available"
fi

exit 0
