#!/bin/sh

alterator_api_version=1
po_domain="alterator-datetime"

ntp_service_status_file=/var/cache/alterator/ntp_service.status
ntp_service_installed=

. alterator-datetime-functions
. shell-config
. shell-var

__read_zone_once_guess()
{
	local zone="$(read_zone "$in_language")"

	# write timezone in the config if needed
	init_timezone "$zone"

	echo "$zone"
}

__set_ntp_status_var()
{
	local name="$1"; shift
	local in_val="$1"; shift
	local out_val=no

	if test_bool "$in_val"; then
		out_val=yes
	fi

	shell_config_set "$ntp_service_status_file" "$name" "$out_val"
}

__get_ntp_status_var()
{
	shell_config_get "$ntp_service_status_file" "$1"
}

__is_ntp_enabled()
{
	[ -n "$ntp_service_installed" ] && shell_var_is_yes "$(__get_ntp_status_var enabled)"
}

read_ctime()
{
    local domain="${2:-$po_domain}"
    local langlist="$(write_language "$in_language")"
    local firstlang="${langlist%%:*}"
    local flags=

    if __is_ntp_enabled; then
	_ "set automatically"
	return
    fi

    if test_bool "$1"; then
	flags='--utc'
    else
	flags='--localtime'
    fi

    (export LC_ALL="$firstlang"; export LANGUAGE="$langlist"; export TZ="$2"; date -d "$(/sbin/hwclock $flags --show|
	    sed 's/[[:space:]]\+[0-9.-]\+[[:space:]]\+[[:alpha:]]\+$//')")
}

# Try to load rpc module
[ -c /dev/rtc ] || modprobe rtc

if is_ntp_installed; then
	ntp_service_installed=yes
fi

# Enable ntp service as client by default.
# This status file will be used later,
# by postinstall script to enable/disable service
if [ ! -e "$ntp_service_status_file" ]; then
	shell_config_set "$ntp_service_status_file" enabled "${ntp_service_installed:-no}"
	shell_config_set "$ntp_service_status_file" server no
fi

on_message()
{
	case "$in_action" in
		type)
			write_type_item ntp_pool hostname-or-ip
			write_type_item date date
			write_type_item time time
			write_type_item zone timezone
			;;
		list)

			case "$in__objects" in
			    avail_country)
				list_country
				;;
			    avail_zone)
				local country="$in_name"
				[ -n "$country" -a -z "$in_reset" ] || country="$(read_country "$(__read_zone_once_guess)")"
				list_zone "$country" ;;
			esac
			;;
		ctime)
			write_string_param ctime "$(read_ctime "$in_utc" "$in_zone")"
			;;
		read)
			case "$in__objects" in
			    /)
				## timezone
				local country="$in_name"
				local zone="$in_zone"

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

				if [ -z "$country" -o -n "$in_reset" ];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"

				## datetime
				local utc="$(read_utc)"
				write_bool_param utc "$utc"
				write_string_param ctime "$(read_ctime "$(write_bool $utc)" "$zone")" #"
				;;
			    edit)
				#date&time
				write_string_param 'date' "$(date +%F)"
				write_string_param 'time' "$(date +%T)"

				#ntp
				write_bool_param ntp_installed "${ntp_service_installed:-no}"
				if [ -n "$ntp_service_installed" ]; then
					write_string_param ntp_pool "$(read_pool)"
					write_bool_param ntp_accept "$(__get_ntp_status_var server)"
					write_bool_param ntp_status "$(__get_ntp_status_var enabled)"
				else
					write_string_param ntp_pool ""
					write_bool_param ntp_accept no
					write_bool_param ntp_status no
				fi
			    ;;
			esac
			;;
		write)
			case "$in__objects" in
			    /)
				write_utc "$in_utc"
				set_adjtime_utc "$in_utc"

				if [ -n "$in_zone" ] && ! write_zone "$in_zone"; then
					write_error "`_ "Unable to setup time zone"`"
					return
				fi

				/sbin/service clock start >&2
				;;
			    edit)
				if [ -n "$ntp_service_installed" ]; then
					__set_ntp_status_var enabled "$in_ntp_status"
				fi

				#ntp vs date&time
				if test_bool "$in_ntp_status" && [ -n "$ntp_service_installed" ]; then
				    write_pool "$in_ntp_pool"
				    __set_ntp_status_var server "$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

				/sbin/service clock sync >&2
				;;
			esac

			
			;;
	esac
}

message_loop
