#! /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
# We must map the language to its "level" from languagelist
# We take the first entry that matches up to the locale's first `_` since
# languages with multiple entries (such as pt/pt_BR or zh_CN/zh_TW)
# will have the same level for all of their entries.
LEVEL=$(sed -ne "/^${LOCALE%%_*}[_;]/"'{ s/^[^;]*;\([0-9]\);.*/\1/p; q}' /usr/share/localechooser/languagelist)
if [ "$LEVEL" -ge 3 ]; then
	cat >> "/target/root/.profile"  <<-EOF
		# Installed by Debian Installer:"
		#  no localization for root because $LOCALE"
		#  cannot be properly displayed at the Linux console"
		LANG=C
		LANGUAGE=C
	EOF
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
