#!/bin/sh
#
# chkconfig: 2345 58 74
# description: ntpd is the NTPv4 daemon. \
# The Network Time Protocol (NTP) is used to synchronize the time of \
# a computer client or server to another server or reference time source, \
# such as a radio or satellite receiver or modem.

### BEGIN INIT INFO
# Provides:        ntp
# Required-Start:  $network $remote_fs $syslog
# Required-Stop:   $network $remote_fs $syslog
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start NTP daemon
### END INIT INFO

source /etc/utils/shell-utils.sh
DEBUG=1

log "* $1"

PATH=/sbin:/bin:/usr/sbin:/usr/bin

NAME=ntp
DAEMON=/usr/sbin/ntpd
PIDFILE=/var/run/ntpd.pid

test -x $DAEMON || exit 5

if [ -r /etc/default/$NAME ]; then
    . /etc/default/$NAME
fi

append_ntp_config "configured by ntp-server"

case $1 in
    start)
        log "Starting NTP server"
        if [ "`ps ax | grep $DAEMON | grep -v grep`" == "" ]; then
            #FIXME daemon can bee started without ntpdate
            #/etc/init.d/ntp-init.sh
            log "demon start...opts = $NTPD_OPTS"
            /sbin/start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON --
        fi
        log "started."
    ;;
    stop)
        log "Stopping NTP server"
        /sbin/start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        rm -f $PIDFILE
    ;;
    restart|force-reload)
        $0 stop && $0 start
    ;;
    *)
        echo "Usage: $0 {start|stop|restart|force-reload}"
        exit 2
    ;;
esac
