#!/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

db_get debian-installer/country
CC="$RET"
multiple=
if db_get "tzsetup/country/$CC"; then
	multiple=1
fi
STATE=0
while :; do
	case $STATE in
	    0)
		# per-country templates are used for countries with multiple choices
		if [ "$multiple" ]; then
			db_register "tzsetup/country/$CC" time/zone
			db_input high time/zone || :
		else
			db_register time/zone time/zone # might be registered to something else
			zone=$(grep "^$CC" /usr/share/tzsetup/tzmap | cut -d ' ' -f 2)
			db_set time/zone "$zone"

			db_subst tzsetup/selected ZONE $zone
			db_input medium tzsetup/selected || true
		fi
		;;
	    1)
		select_all=
		if [ "$multiple" ]; then
			db_get time/zone
			if [ "$RET" = other ]; then
				select_all=1
			fi
		else
			db_get tzsetup/selected
			if [ "$RET" = false ]; then
				select_all=1
			fi
		fi
		# Asking tzsetup/select_all is done in a distinct state so
		# that our handling of the user selecting a separator works
		# properly.
		;;
	    2)
		if [ "$select_all" ]; then
			db_register tzsetup/select_all time/zone
			db_input high time/zone || :
		fi
		;;
	    3)
		if [ "$select_all" ]; then
			db_get time/zone
			if [ -z "$RET" ] || [ "${RET# }" != "$RET" ] || \
			   [ "${RET#---}" != "$RET" ]; then
				# User selected a separator; try again.
				STATE=2
				continue
			fi
		fi
		;;
	    *)
		break
		;;
	esac

	if db_go; then
		STATE=$(($STATE + 1))
	else
		STATE=$(($STATE - 1))
	fi
done

if [ "$STATE" = -1 ]; then
	exit 10 # back to menu
fi
