#! /bin/sh
#
# We run as soon as networking is live, and before we use NFS!
# chkconfig: S 41 0
#

test -f /usr/sbin/ntpdate || exit 0
test -f /etc/ntp.conf || exit  0

case "$1" in
start)
  # Fix for ntp offset overflow. Offset can be +/- 0x3FFFFFFF
  # So if the date is any year below 1972 (as of 2006), ntpdate
  # will return a negative offset and will set wrong values
  date 010100002007
  echo "Running ntpdate to synchronize clock :"
  cat /etc/ntp.conf | grep server | awk '{print $2}' \
  |while read NTPSERVER; do
	if [ "$NTPSERVER" != "" ]
	then
		echo -n "Trying NTP Server $NTPSERVER :"
		ntpdate -b -s  $NTPSERVER
		if [ $? = 0 ] 
		then
			echo " Success."
			exit 0
		else
			echo " Failed."
		fi
	fi
  done
  ;;
stop|restart|force-reload)
  ;;
*)
  echo "Usage: /etc/init.d/ntpdate {start|stop|restart|force-reload}"
  exit 1
esac

exit 0
