#!/bin/sh
#
#
# chkconfig: 345 15 05
# description: This startup script sets timezone on live system

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions
. shell-config
. shell-error
. alterator-datetime-functions

RETVAL=0

__set_timezone()
{
	local msg="Set timezone $1 from $2"

	[ -n "$1" ] || return 1

	if write_zone "$1"; then
		success "$msg"
		return 0
	fi

	failure "$msg"

	return 1
}

start()
{
	local tz=
	local utc=

	# Try to setup UTC from kernel command line.
	utc="$(get_utc_cmdline)"
	case "$utc" in
		true) write_utc "#t";;
		false) write_utc "#f";;
	esac

	# First try to setup time zone from kernel command line.
	tz="$(get_timezone_cmdline)"
	if [ -n "$tz" ]; then
		__set_timezone "$tz" "kernel command line" && return 0
	fi

	# Second try to use geoip server.
	tz="$(get_timezone_geoip)"
	if [ -n "$tz" ]; then
		__set_timezone "$tz" "geoip server" && return 0
	fi

	# As last resort try to guess based on language
	tz="$(get_timezone_lang)"
	if [ -n "$tz" ]; then
		__set_timezone "$tz" "language" && return 0
	fi

	return 0
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	start|stop|reload|restart|condstop|condrestart|condreload|status)
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
		;;
esac

exit $RETVAL
