#! /bin/sh
set -e

. /usr/share/debconf/confmodule
db_capb backup

/usr/lib/ubiquity/tzsetup/tzsetup

if [ -z "$UBIQUITY_OEM_USER_CONFIG" ]; then
	# Set the timezone in the live system. (Cloned-and-hacked from
	# tzsetup/post-base-installer.d/05tzsetup.)
	db_get time/zone
	zone="$RET"

	if [ ! -e /usr/share/zoneinfo/$zone ]; then
		logger -t tzsetup "Warning: ignoring invalid time zone '$zone'"
		exit 0
	fi

	echo "$zone" > /etc/timezone
	rm -f /etc/localtime
	cp -f /usr/share/zoneinfo/$zone /etc/localtime
fi

if [ "$TZSETUP_NO_LOCALECHOOSER" ]; then
	exit 0
fi

# Reconfigure locale according to the selected country. We rely on
# ubiquity/components/ubi-timezone.py having set debian-installer/country
# for us.
db_get debian-installer/country
country="$RET"
db_set mirror/country "$country" || true
db_get localechooser/languagelist
requested_lang="$RET"
case $requested_lang in
    pt|pt_*|zh_*)
	# Portuguese and Chinese are special cases; selecting a different
	# location may imply a different dialect of the language.  We will
	# handle these separately (see scripts/localechooser-apply).
	;;
    *)
	db_get debian-installer/locale
	newlocale="$(echo "$RET" | sed "s/_[A-Z][A-Z]*/_$country/")"
	if grep -q "^${newlocale%%[.@]*}[.@ ]" /usr/share/i18n/SUPPORTED; then
		db_set debian-installer/locale "$newlocale"
		db_fset debian-installer/locale seen true
		# Let localechooser know that we're using the same language as
		# before, so that it doesn't set a default country again.
		db_fset localechooser/languagelist seen false
		rm -f /var/lib/localechooser/preseeded
		PATH="/usr/lib/ubiquity/localechooser:$PATH" \
			OVERRIDE_SHOW_ALL_LANGUAGES=1 \
			/usr/lib/ubiquity/localechooser/localechooser
	fi
	;;
esac
