#! /bin/sh
set -e

. /usr/share/debconf/confmodule
db_version 2.0
db_capb backup

convert_timezone()
{
	case "$1" in
		right/* | posix/*)
			convert_timezone "${1#*/}"
			;;
		"Africa/Asmera")
			echo "Africa/Asmara"
			;;
		"America/Buenos_Aires")
			echo "America/Argentina/Buenos_Aires"
			;;
		"America/Argentina/ComodRivadavia" | "America/Catamarca")
			echo "America/Argentina/Catamarca"
			;;
		"America/Cordoba" | "America/Rosario")
			echo "America/Argentina/Cordoba"
			;;
		"America/Fort_Wayne" | "America/Indianapolis" | "EST" | "SystemV/EST5" | "US/East-Indiana")
			echo "America/Indiana/Indianapolis"
			;;
		"America/Jujuy")
			echo "America/Argentina/Jujuy"
			;;
		"America/Knox_IN")
			echo "America/Indiana/Knox"
			;;
		"America/Louisville")
			echo "America/Kentucky/Louisville"
			;;
		"America/Mendoza")
			echo "America/Argentina/Mendoza"
			;;
		"Antarctica/South_Pole")
			echo "Antarctica/McMurdo"
			;;
		"Asia/Ashkhabad")
			echo "Asia/Ashgabat"
			;;
		"Asia/Calcutta")
			echo "Asia/Kolkata"
			;;
		"Asia/Chungking")
			echo "Asia/Chongqing"
			;;
		"Asia/Dacca")
			echo "Asia/Dhaka"
			;;
		"Asia/Katmandu")
			echo "Asia/Kathmandu"
			;;
		"Asia/Macao")
			echo "Asia/Macau"
			;;
		"Asia/Rangoon")
			echo "Asia/Yangon"
			;;
		"Asia/Riyadh87" | "Asia/Riyadh88" | "Asia/Riyadh89" | "Mideast/Riyadh87" | "Mideast/Riyadh88" | "Mideast/Riyadh89")
			echo "Asia/Riyadh"
			;;
		"Asia/Saigon")
			echo "Asia/Ho_Chi_Minh"
			;;
		"Asia/Thimbu")
			echo "Asia/Thimphu"
			;;
		"Asia/Ujung_Pandang")
			echo "Asia/Makassar"
			;;
		"Asia/Ulan_Bator")
			echo "Asia/Ulaanbaatar"
			;;
		"Atlantic/Faeroe")
			echo "Atlantic/Faroe"
			;;
		"Australia/ACT" | "Australia/NSW")
			echo "Australia/Sydney"
			;;
		"Australia/LHI")
			echo "Australia/Lord_Howe"
			;;
		"Australia/North")
			echo "Australia/Darwin"
			;;
		"Australia/Queensland")
			echo "Australia/Brisbane"
			;;
		"Australia/South")
			echo "Australia/Adelaide"
			;;
		"Australia/Tasmania")
			echo "Australia/Hobart"
			;;
		"Australia/Victoria")
			echo "Australia/Melbourne"
			;;
		"Australia/West")
			echo "Australia/Perth"
			;;
		"Brazil/Acre")
			echo "America/Rio_Branco"
			;;
		"Brazil/DeNoronha")
			echo "America/Noronha"
			;;
		"Brazil/East")
			echo "America/Sao_Paulo"
			;;
		"Brazil/West")
			echo "America/Manaus"
			;;
		"Canada/Atlantic" | "SystemV/AST4ADT")
			echo "America/Halifax"
			;;
		"Canada/Central")
			echo "America/Winnipeg"
			;;
		"Canada/Eastern")
			echo "America/Toronto"
			;;
		"Canada/East-Saskatchewan" | "Canada/Saskatchewan" | "SystemV/CST6")
			echo "America/Regina"
			;;
		"Canada/Mountain")
			echo "America/Edmonton"
			;;
		"Canada/Newfoundland")
			echo "America/St_Johns"
			;;
		"Canada/Pacific")
			echo "America/Vancouver"
			;;
		"Canada/Yukon")
			echo "America/Whitehorse"
			;;
		"CET" | "MET")
			echo "Europe/Paris"
			;;
		"Chile/Continental")
			echo "America/Santiago"
			;;
		"Chile/EasterIsland")
			echo "Pacific/Easter"
			;;
		"CST6CDT" | "SystemV/CST6CDT")
			echo "America/Chicago"
			;;
		"Cuba")
			echo "America/Havana"
			;;
		"EET")
			echo "Europe/Helsinki"
			;;
		"Egypt")
			echo "Africa/Cairo"
			;;
		"Eire")
			echo "Europe/Dublin"
			;;
		"EST5EDT" | "SystemV/EST5EDT")
			echo "America/New_York"
			;;
		"Europe/Kiev")
			echo "Europe/Kyiv"
			;;
		"Europe/Uzhgorod")
			echo "Europe/Kyiv"
			;;
		"Europe/Zaporozhye")
			echo "Europe/Kyiv"
			;;
		"GB" | "GB-Eire")
			echo "Europe/London"
			;;
		"GMT")
			echo "Etc/GMT"
			;;
		"GMT0")
			echo "Etc/GMT0"
			;;
		"GMT-0")
			echo "Etc/GMT-0"
			;;
		"GMT+0")
			echo "Etc/GMT+0"
			;;
		"Greenwich")
			echo "Etc/Greenwich"
			;;
		"Hongkong")
			echo "Asia/Hong_Kong"
			;;
		"HST" | "SystemV/HST10")
			echo "Pacific/Honolulu"
			;;
		"Iceland")
			echo "Atlantic/Reykjavik"
			;;
		"Iran")
			echo "Asia/Tehran"
			;;
		"Israel")
			echo "Asia/Tel_Aviv"
			;;
		"Jamaica")
			echo "America/Jamaica"
			;;
		"Japan")
			echo "Asia/Tokyo"
			;;
		"Kwajalein")
			echo "Pacific/Kwajalein"
			;;
		"Libya")
			echo "Africa/Tripoli"
			;;
		"Mexico/BajaNorte")
			echo "America/Tijuana"
			;;
		"Mexico/BajaSur")
			echo "America/Mazatlan"
			;;
		"Mexico/General")
			echo "America/Mexico_City"
			;;
		"MST" | "SystemV/MST7")
			echo "America/Phoenix"
			;;
		"MST7MDT" | "Navajo" | "SystemV/MST7MDT")
			echo "America/Denver"
			;;
		"NZ")
			echo "Pacific/Auckland"
			;;
		"Pacific/Enderbury")
			echo "Pacific/Kanton"
			;;
		"Pacific/Ponape")
			echo "Pacific/Pohnpei"
			;;
		"Pacific/Truk")
			echo "Pacific/Chuuk"
			;;
		"NZ-CHAT")
			echo "Pacific/Chatham"
			;;
		"Poland")
			echo "Europe/Warsaw"
			;;
		"Portugal" | "WET")
			echo "Europe/Lisbon"
			;;
		"PRC")
			echo "Asia/Shanghai"
			;;
		"PST8PDT" | "SystemV/PST8PDT")
			echo "America/Los_Angeles"
			;;
		"ROC")
			echo "Asia/Taipei"
			;;
		"ROK")
			echo "Asia/Seoul"
			;;
		"Singapore")
			echo "Asia/Singapore"
			;;
		"SystemV/AST4")
			echo "America/Puerto_Rico"
			;;
		"SystemV/PST8")
			echo "Pacific/Pitcairn"
			;;
		"SystemV/YST9")
			echo "Pacific/Gambier"
			;;
		"SystemV/YST9YDT")
			echo "America/Anchorage"
			;;
		"Turkey")
			echo "Europe/Istanbul"
			;;
		"UCT")
			echo "Etc/UCT"
			;;
		"Universal" | "UTC" | "Zulu")
			echo "Etc/UTC"
			;;
		"W-SU")
			echo "Europe/Moscow"
			;;
		*)
			echo "$1"
			;;
	esac
}

# Read timezone from /etc/localtime if it is a link
if [ -L "$DPKG_ROOT/etc/localtime" ] ; then
	TIMEZONE="$(readlink "$DPKG_ROOT/etc/localtime")"
	TIMEZONE="$(cd "$DPKG_ROOT/etc" && realpath -m -s "$TIMEZONE")"
	TIMEZONE="${TIMEZONE#/usr/share/zoneinfo/}"
# Fall back to /etc/timezone if present
elif [ -e "$DPKG_ROOT/etc/timezone" ]; then
	TIMEZONE="$(head -n 1 "$DPKG_ROOT/etc/timezone")"
	TIMEZONE="${TIMEZONE%% *}"
	TIMEZONE="${TIMEZONE##/}"
	TIMEZONE="${TIMEZONE%%/}"
fi

TIMEZONE="$(convert_timezone "$TIMEZONE")"

if [ -f "$DPKG_ROOT/usr/share/zoneinfo/$TIMEZONE" ] ; then
	AREA="${TIMEZONE%%/*}"
	ZONE="${TIMEZONE#*/}"

	# Don't ask the user, except if he/she explicitely asked that
	if [ -z "$DEBCONF_RECONFIGURE" ] ; then
		db_fset tzdata/Areas seen true
		db_fset "tzdata/Zones/$AREA" seen true
	fi
# The timezone has never been configured or is falsely configured
else
	RET=""
	db_fget tzdata/Areas seen || RET=false
	if [ "$RET" = true ] ; then
		db_get tzdata/Areas
		AREA=$RET
	else
		AREA="Etc"
	fi

	RET=""
	db_fget "tzdata/Zones/$AREA" seen || RET=false
	if [ "$RET" = true ] ; then
		db_get "tzdata/Zones/$AREA"
		ZONE=$RET
	else
		ZONE="UTC"
	fi

	db_fset tzdata/Areas seen false
	db_fset "tzdata/Zones/$AREA" seen false
fi

# Initializes debconf default values from the ones found in
# configuration files
db_set tzdata/Areas "$AREA"
db_set "tzdata/Zones/$AREA" "$ZONE"

STATE=1
while [ "$STATE" -ge 0 ]; do
	case "$STATE" in
	0)
		# The user has cancel the timezone change, reset the debconf
		# values to the initial one.
		db_set tzdata/Areas "$AREA"
		db_set "tzdata/Zones/$AREA" "$ZONE"
		break
		;;
	1)
		# Ask the user of the Area
		db_input high tzdata/Areas || true
		;;
	2)
		# Ask the user of the Zone
		db_get tzdata/Areas || RET=Etc
		db_input high tzdata/Zones/$RET || true
		;;
	*)
		break
		;;
	esac
	# shellcheck disable=SC2119
	if db_go; then
		STATE=$((STATE + 1))
	else
		STATE=$((STATE - 1))
	fi
done




exit 0
