#!/bin/sh

. /usr/share/alterator/build/backend3.sh

prefix=
ZONEINFODIR=/usr/share/zoneinfo
ZONETABFILE=$ZONEINFODIR/zone.tab
ISOTABFILE=$ZONEINFODIR/iso3166.tab
NTPSERVERS="/usr/share/install3/data/ntpservers"
ZONEDEFAULTS="/usr/share/install3/data/timezones"

error() {
	printf '(error "%s")' "$*"
}

is_yes() {
	case "$1" in
		yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1) return 0 ;;
		*) return 1 ;;
	esac
}

add_or_subst() {
	if grep -qs "^[[:space:]]*$1=" /etc/sysconfig/clock; then
		sed -i -e "s#^[[:space:]]*$1=.*#$1=\"$2\"#g" /etc/sysconfig/clock
	else
		printf 'ZONE="%s"\n' "$2" >> /etc/sysconfig/clock
	fi
}

contry_code=
on_message()
{
	case "$in_action" in
		list)
			echo "("
			if [ "${in__objects##*/}" = "countries" ]; then
				grep -v '^#' "$ISOTABFILE" |
					sort -k2,2 |
					sed -r -e 's,([^[:space:]]+)[[:space:]]+(.*),("" country "\2" code "\1"),'
			elif [ "${in__objects##*/}" = "zones" ]; then
				grep -v "^#" "$ZONETABFILE" |
					cut -f1,3 |
					sed -r -e 's,^([^[:space:]]+)[[:space:]]+([^/]+)/(.*),("" city "\3" code "\1" zone "\2/\3"),'
			fi
			echo ")"
			;;
		read)
			if [ "${in__objects##*/}" = "zones" ]; then
				local ZONE=
				. /etc/sysconfig/clock

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

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

					[ -n "$ZONE" ] ||
						ZONE="Europe/Vatican"
				fi
				local country_code= country= utc= ntp=

				is_yes "$UTC" && utc="#t" || utc="#f"

				[ -f /etc/rc.d/init.d/ntpd ] && ntp="#t" || ntp="#f"

				country_code="$(grep -v '^#' "$ZONETABFILE" |
							cut -f1,3 |
							grep -m1 "[[:space:]]$ZONE" |
							cut -f1 |
							head -1)"

				[ -z "$country_code" ] ||
					country="$(sed -n -e "s,^$country_code[[:space:]]\(.*\),\1,p" "$ISOTABFILE")"

				printf '(("" country "%s" city "%s" utc %s ntp %s))\n' "$country" "${ZONE#*/}" "$utc" "$ntp"

			elif [ "${in__objects##*/}" = "ntps" ]; then
				local suffix=
				[ -n "$INSTALLER_LANGUAGE" -a "$INSTALLER_LANGUAGE" != "POSIX" -a "$INSTALLER_LANGUAGE" != "C" ] &&
					suffix="$(printf %s "$INSTALLER_LANGUAGE" |
							sed -r -e 's,[a-z]+_([^\.]+)(\..*)?,\1,' |
							tr '[:upper:]' '[:lower:]')" ||
					suffix="all"

				local ntp_file="$NTPSERVERS.$suffix"
				[ -s "$ntp_file" ] ||
					ntp_file="$NTPSERVERS.all"

				echo "("
				sed -e 's,^\(.*[^[:space:]]\)[[:space:]]\+\([^[:space:]]\+\)$,("" area "\1" server "\2"),' \
					"$ntp_file"
				echo ")"
			fi
			;;
		write)
			if [ -n "$in_zone" ]; then
				if [ ! -f "$ZONEINFODIR/$in_zone" ]; then
					error "Timezone file not found"
					return
				fi
				add_or_subst "ZONE" "$in_zone"
				cp -f -- "$ZONEINFODIR/$in_zone" /etc/localtime
			fi

			if [ -n "$in_utc" ]; then
				[ "$in_utc" = "#t" ] &&
					utc="true" ||
					utc="false"
				add_or_subst "UTC" "$utc"
			fi

			if [ "$in_usentp" = "#f" -a -n "$in_date" -a -n "$in_time" ]; then
				local UTC= args= err=
				. /etc/sysconfig/clock
				is_yes "$UTC" && args="--utc"

				if ! err="$(date $args --set="$in_date $in_time" +'%F %T' 2>&1 >/dev/null)"; then
					error "$err"
					return
				fi
			fi

#			if [ "$in_usentp" = "#t" ]; then
#				chkconfig ntpd on
#			else
#				chkconfig ntpd off
#			fi
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop
