#!/bin/sh
#
# ntp           This shell script takes care of starting and stopping
#               ntp (NTP daemon).
#
# chkconfig:	2345 55 10
# description:	ntp is the NTP daemon.

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

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

# Source ntp configuration
. /etc/sysconfig/ntp

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

[ -x /usr/sbin/ntpd -a -f /etc/ntp/ntp.conf ] || exit 0

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
        if [ ! -f /var/lock/subsys/ntp ]; then
		if is_yes "$SYNC_TIME"; then
	        	# Adjust time to make life easy for ntp
       			run_cmd "Syncing time for ntp" /usr/sbin/ntpdate $NTPDATE_OPTIONS $NTPDATE_SERVERS
		fi
            	msg_starting ntp
            	daemon ntpd -c /etc/ntp/ntp.conf
        	RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ntp
        else
	      	msg_already_running ntp
		exit 1
	fi
        ;;
  stop)
  	if [ -f /var/lock/subsys/ntp ]; then
        	msg_stopping ntp
		killproc ntpd
        	rm -f /var/lock/subsys/ntp
        else
		msg_not_running ntp
		exit 1
	fi
	;;
  status)
	status ntpd
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit 0
