#!/bin/sh
set -e

. /usr/share/debconf/confmodule
db_capb backup

# Leave preseeded time zone untouched if seen flag is set
db_fget time/zone seen
if [ "$RET" = true ]; then
	exit 0
fi

UTC=
db_get debconf/priority
case $RET in
    medium|low)	UTC=1 ;;
esac

# Get description for UTC timezone
db_metaget tzsetup/text/UTC description
UTC_CHOICE="$RET"

db_get debian-installer/country
CC="$RET"
# per-country templates are used for countries with multiple choices
# At medium/low priority UTC is dynamically added to the list at medium/low
if db_get "tzsetup/country/$CC"; then
	priority=high
	db_metaget tzsetup/country/$CC default
	zone=$RET

	db_metaget tzsetup/country/$CC choices-c
	db_subst time/zone CHOICES-C "$RET${UTC:+, UTC}"
	db_metaget tzsetup/country/$CC choices
	db_subst time/zone CHOICES "$RET${UTC:+, $UTC_CHOICE}"

	# First get type of description and use that to get actual description
	db_metaget tzsetup/country/$CC description
	db_metaget tzsetup/descriptions/$RET description
	db_subst time/zone DESCRIPTION "$RET"
else
	priority=medium
	zone=$(grep "^$CC" /usr/share/tzsetup/tzmap | cut -d ' ' -f 2)

	db_subst time/zone CHOICES-C "$zone${UTC:+, UTC}"
	db_subst time/zone CHOICES "$zone${UTC:+, $UTC_CHOICE}"
	db_metaget tzsetup/descriptions/zone description
	db_subst time/zone DESCRIPTION "$RET"
fi

db_get time/zone
if [ -z "$RET" ]; then
	db_set time/zone "$zone"
fi

db_input $priority time/zone || :
if ! db_go; then
	exit 10 # back to menu
fi
