#!/bin/sh

alterator_api_version=1
po_domain="alterator-datetime"

alteratordhcp_file=/etc/alterator/dhcp/general
alteratordhcp_cmd=/usr/bin/alterator-dhcp-reset

. alterator-datetime-functions

### timezone

on_message()
{
	case "$in_action" in
		type)
			write_type_item ntp_pool hostname
			write_type_item date date
			write_type_item time time
			;;
		list)
			case "$in__objects" in
			    avail_country)
				list_country
				;;
			    avail_zone)
				local country="$in_name"
				[ -n "$country" ] || country="$(read_country $(read_zone))"
				list_zone "$country"
				;;
			esac
			;;
		read)
			case "$in__objects" in
			    /)
			    ## date&time
			    write_string_param 'date' "$(date +%F)"
			    write_string_param 'time' "$(date +%T)"
			    write_string_param 'date_string' "$(run_localized date)"

			    ## ntp
			    write_string_param ntp_pool "$(read_pool)"

			    ! test_ntp_accept
			    write_bool_param ntp_accept "$?"

			    ! test_ntp_status
			    write_bool_param ntp_status "$?"

			    write_bool_param utc "$(read_utc)"

			    ## timezone
			    # We need the real zone from config file here,
			    # not the guess according to current language.
			    # So call read_zone with empty in_language variable.
			    local zone="$(in_language= read_zone)"
			    local tr_zone="$(translate_zone "$zone")"
			    local country="$(read_country "$zone")"
			    local tr_country="$(country_name "$country")"

			    write_string_param timezone "$tr_country/${tr_zone#*/}"
			    ;;
			    timezone)
				local country="$in_name"
				local zone="$in_zone"

				local default_zone="$(read_zone)"
				local default_country="$(read_country "$default_zone")"

				if [ -z "$country" ];then
				    zone="$default_zone"
				    country="$default_country"
				elif [ "$country" = "$default_country" ];then
				    zone="$default_zone"
				fi

				write_string_param zone "$zone"
				write_string_param name "$country"
			    ;;
			esac
			;;
		write)
			case "$in__objects" in
			    /)

			    write_utc "$in_utc"
			    write_ntp_status "$in_ntp_status"

			    # ntp vs. date&time
			    if test_bool "$in_ntp_status";then
				write_pool "$in_ntp_pool"
				write_ntp_accept "$in_ntp_accept"
			    else
				if  [ -n "$in_date" -a -n "$in_time" ] &&
				    ! date --set="$in_date $in_time" 2>/dev/null >/dev/null; then
				    write_error "`_ "unable to setup time"`"
				    return
				fi
			    fi

			    ## alterator-dhcp
			    if [ -f "$alteratordhcp_file" -a -x "$alteratordhcp_cmd" ]; then
				if test_bool "$in_ntp_status" && test_bool "$in_ntp_accept";then
				    shell_config_set "$alteratordhcp_file" client_ntp "*"
				else
				    shell_config_del "$alteratordhcp_file" client_ntp
				fi
				"$alteratordhcp_cmd"
				service_control dhcpd condreload
			    fi
			    ;;
			    timezone)
			    if [ -n "$in_zone" ] && ! write_zone "$in_zone"; then
				write_error "`_ "Unable to setup time zone"`"
				return
			    fi
			    ;;
			esac

			/etc/init.d/clock sync >/dev/null 2>/dev/null
			;;
	esac
}

message_loop
