#!/bin/sh

alterator_api_version=1
po_domain='alterator-tzone'

. alterator-sh-functions
. shell-config

zoneinfodir=/usr/share/zoneinfo

zonetabfile=/usr/share/zoneinfo/zone.tab
isotabfile=/usr/share/zoneinfo/iso3166.tab

local_zonetabfile=/etc/sysconfig/alterator/timezone/zone.tab
local_isotabfile=/etc/sysconfig/alterator/timezone/iso3166.tab

sysconfig_clockfile=/etc/sysconfig/clock

datadir=/usr/share/alterator-tzone/

list_country()
{
    local tr_isotabfile=
    local f= 
    local l=
    local lang=

    for l in `echo "$in_language" |tr ';' ' '`; do
	f="$datadir/iso3166.tab.$l"
	if [ -s "$f" ]; then
	    tr_isotabfile="$f"
	    lang="$l"
	    break
	fi
    done

    [ -n "$lang" ] || lang="en_US"

    local SEP='	'
    local IFS="$SEP"
    grep -v '^[[:space:]]*#' "$isotabfile"|
	if [ -s "$tr_isotabfile" ];then
	    join -j1 -o 2.1,1.2,2.2 -t"$SEP" -a2  "$tr_isotabfile" -  |
		while read code trans eng;do
		    printf '%s\t%s\n' "$code" "${trans:-$eng}"
		done
	else
	    cat
	fi|
	LANG="$lang.utf8" sort -k2,2|write_enum
}

read_zone() {
	local ZONE= country= country_code=

	local language="$INSTALLER_LANGUAGE"
	[ -n "$language" ] || language="${in_language%%;*}"

	. /etc/sysconfig/clock

	if [ -z "$ZONE" ]; then
		local region="$(printf %s "$language" |
				sed -r -e 's,[a-z]+_([^\.]+)(\..*)?,\1,' |
				tr '[:lower:]' '[:upper:]')"

		[ -z "$region" ] ||
			ZONE="$(grep "^$region[[:space:]]" "$datadir/defaultzones" |cut -f2)"

		[ -n "$ZONE" ] ||
			ZONE="Europe/London"
	fi
	
	printf %s\\n "$ZONE"
}

read_country()
{
    cut -f1,3 "$zonetabfile" |grep "[[:space:]]$1" |cut -f1
}

list_zones() {
	local IFS="	"
	local code coordinates zone comments

	grep "^$1[[:space:]]" "$zonetabfile" |
	while read code coordinates zone comments; do
		tr_zone="`_ "$zone" alterator-tzone-tz`"
		write_enum_item "$zone" "${tr_zone#*/}"
	done
}

[ ! -s "$local_zonetabfile" ] || zonetabfile="$local_zonetabfile"
[ ! -s "$local_isotabfile" ] || isotabfile="$local_isotabfile"

on_message()
{
	case "$in_action" in
		list)
			local country="$in_name"

			[ -n "$country" -a -z "$in_reset" ] || country="$(read_country $(read_zone))"

			case "$in__objects" in
			    avail_country) list_country ;;
			    avail_zone) list_zones "$country" ;;
			esac
			;;
		read)
			local country="$in_name"
			local zone="$in_zone"

			if [ -z "$country" -o -n "$in_reset" ];then
			    zone="$(read_zone)"
			    country="$(read_country "$zone")"
			fi

			write_bool_param utc "$(shell_config_get "$sysconfig_clockfile" UTC)"
			write_string_param zone "$zone"
			write_string_param name "$country"
			;;
		write)
			[ -n "$in_commit" ] || return

			if test_bool "$in_utc";then
			    shell_config_set "$sysconfig_clockfile" UTC "true"
			else
			    shell_config_set "$sysconfig_clockfile" UTC "false"
			fi

			if [ -n "$in_zone" ]; then
				if [ ! -f "$zoneinfodir/$in_zone" ]; then
					write_error "Timezone file not found"
					return
				fi
				shell_config_set "$sysconfig_clockfile" "ZONE" "$in_zone"
				cp -f -- "$zoneinfodir/$in_zone" /etc/localtime
			fi

			if [ "$in_installer" = "#t" ];then
			    service clock start >/dev/null 2>/dev/null
			else
			    service clock sync >/dev/null 2>/dev/null
			fi
			;;
	esac
}

message_loop
