#!/bin/sh
#=================================================================
# Begin ntpd
#
# Description : Deamon handle NTP protocol and keep clock in sync
#
#=================================================================

### BEGIN INIT INFO
# Provides:            $ntpd
# Required-Start:      $network
# Should-Start:        $ripd
# Required-Stop:       
# Should-Stop:         
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts ntpd.
# Description:         Starts ntpd daemon.
# X-LFS-Provided-By:   BLFS
### END INIT INFO

. /lib/lsb/init-functions

PROG="ntpd"
LEAPFILE="/etc/ntp.leapseconds"
BIN_FILE="/usr/sbin/$PROG"
PID_FILE=/var/run/$PROG.pid


case "$1" in
   start)
      #to update leap file if needed
      if [ ! -f $LEAPFILE ] ; then
	(
	cd /tmp
        log_info_msg "Loading IETF 'seconds leap' file"
	wget 								\
		--quiet							\
		--timeout=5						\
	       	http://www.ietf.org/timezones/data/leap-seconds.list
        evaluate_retval
	if [ -f leap-seconds.list ] ; then
	  mv leap-seconds.list $LEAPFILE
          fi
	)
        fi
      #to sync the clock quickly
      log_info_msg "Starting $PROG..."
      start_daemon ${BIN_FILE} -g -l /var/log/$PROG.log
      evaluate_retval
      ;;

   stop)
      log_info_msg "Stopping $PROG..."
      killproc -p $PID_FILE ${BIN_FILE}
      evaluate_retval
      ;;

   restart)
      $0 stop
      sleep 1
      $0 start
      ;;

   status)
      statusproc ${BIN_FILE}
      ;;

   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac

# End /etc/init.d/ntpd
