#!/bin/sh
. /etc/sysconfig/network

if [ "$1" = "stop" -o "$1" = "restart" ]
then
	if [ -e /var/run/ntpd.pid ]
	then
		killall ntpd
	fi

	if [ -x /sbin/hwclock ]
	then
		echo "Syncing hardware clock to system time"
		/sbin/hwclock -w -u
	fi
fi

if [ "$1" = "start" -o "$1" = "restart" ]
then
	echo "Setting time from ntp server: $NTP_SERVER"
	# Use fast NTPdate client
	ntpdate	-t 5 -p 1 pool.ntp.org 2> /dev/null
	if [ $? -eq 0 ]
	then
		echo "NTP succeed, sync HW clock."
		/sbin/hwclock -w -u
	else
		echo "NTP request failed, read HW clock"
		/sbin/hwclock -s -u
	fi

	/usr/sbin/ntpd -p /var/run/ntpd.pid -c /etc/ntp.conf
fi
