#!/bin/sh
#
# rdate		This shell script takes care of setting date from ntp server on startup
#
# chkconfig:	2345 16 89
# description:	set time with rdate
# processname:	rdate

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down rdate
		exit 1
	fi
else
	exit 0
fi

SET_TIME=no
ADJTIMEX=yes

# Get service config
if [ -f /etc/sysconfig/rdate ]; then
	. /etc/sysconfig/rdate
fi
if [ "$ADJTIMEX" = "no" ]; then
	ADJOPT=""
else
	ADJOPT="-a"
fi

#rdate-bsd accepts only one argument
RDATE_SERVERS=`echo $RDATE_SERVERS | tr -cs '[:graph:]' '[\n*]'|head -n1`

# See how we were called.
case "$1" in
  start|restart|reload|force-reload)
	# Check if we have to do anything:
	if is_yes "$SET_TIME" && [ -n "$RDATE_SERVERS" ]; then
		run_cmd "$(nls "Setting time from remote server(s): %s" "$RDATE_SERVERS")" rdate $ADJOPT $RDATE_SERVERS
	fi
	;;
  stop)
	# nothing to do
	;;
  status)
	if [ -n "$RDATE_SERVERS" ]; then
		echo -n "Remote time: "
		rdate -p $RDATE_SERVERS
		echo -n "Local time: "
		date
		echo -n "Local machine hardware clock: "
		clock --show
	fi
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac
