#!/bin/sh

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/

cat_isotabfile()
{
    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
}

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"
}

list_zones() {
	local IFS="	"
	local country_code= code coordinates zone comments
				
	country_code="${in__objects%/*}"
				
	grep "^$country_code[[:space:]]" "$zonetabfile" |
	while read code coordinates zone comments; do
		tr_zone="`_ "$zone" alterator-tzone-tz`"
		printf '("%s" label "%s")\n' "$zone" "${tr_zone#*/}"
	done
}

read_utc()
{
    local UTC= val=
    . "$sysconfig_clockfile"
    write_bool "$UTC"
}

write_utc()
{
	if grep -qs "^[[:space:]]*UTC=" "$sysconfig_clockfile"; then
		sed -i -e "s#^[[:space:]]*UTC=.*#UTC=\"$1\"#g" "$sysconfig_clockfile"
	else
		cat > "$sysconfig_clockfile" <<-EOF
		# UTC=true indicates that the clock is set to UTC; 
		# anything else indicates that it is set to local time.
		UTC="$1"
		EOF
	fi
}

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

contry_code=
on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			printf 'name (label "%s")' "`_ "Country"`"
			printf 'zone (label "%s")' "`_ "Time zone"`"
			printf 'utc  (label "%s")' "`_ "Hardware time set to GMT"`"
			echo ')'
			;;
		list)
			echo '('
			if [ "$in__objects" = "/" ]; then
				cat_isotabfile|
				    sed -r -e 's,([^[:space:]]+)[[:space:]]+(.*),("\1" label "\2"),'
			elif [ "${in__objects##*/}" = "zones" ]; then
				list_zones
			fi
			echo ')'
			;;
	    	read)
			echo '('
			if [ "$in__objects" == "/" ];then
				# read current zone info (country)

				local zone= country_code=
				zone="$(read_zone)"

				country_code="$(cut -f1,3 "$zonetabfile" |grep "[[:space:]]$zone" |cut -f1)"

				printf 'name "%s"\n' "$country_code"
			        printf 'utc %s\n' "$(read_utc)"

			else
				# read zone info for selected contry (default town?)
				local zone=
				
				zone="$(read_zone)"
				printf 'zone "%s"\n'  "$zone"
			fi
			echo ')'
			;;
		write)
			[ "$in_utc" = "#t" ] && write_utc "true" || write_utc "false"

			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
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop
