#!/bin/sh

### 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:    1
# Short-Description: Start NTP daemon
### END INIT INFO

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

. /lib/lsb/init-functions

# first, all sntp-related functions

prog=/usr/bin/sntp
hwlockfile=/var/lock/sntp
ntpconf=/etc/ntp.conf
ntpstep=/etc/ntp/step-tickers
RETVAL=0

sntpstart() {
        [ -x /usr/bin/sntp ] || exit 5
        [ -f /etc/default/sntp ] || exit 6
        . /etc/default/sntp

        [ -f $ntpstep ] && tickers=$(sed 's/#.*//' $ntpstep) || tickers=

        if ! echo "$tickers" | grep -qi '[a-z0-9]' && [ -f $ntpconf ]; then
            # step-tickers doesn't specify a server,
            # use servers from ntp.conf instead
            tickers=$(awk '$1=="peer"||$1=="server"{print $2}' $ntpconf | \
                egrep -v '127\.127\.[0-9]+\.[0-9]+')
        fi

        if ! echo "$tickers" | grep -qi '[a-z0-9]'; then
            echo $"NTP server not specified in $ntpstep or $ntpconf"
            exit 6
        fi
        [ -z "$RETRIES" ] && RETRIES=2
        retry=1
        while true; do
		log_daemon_msg "Try to sync with NTP server over sntp, retry $retry"
                $prog $OPTIONS $tickers > /tmp/sntp.out
                RETVAL=$?
		if grep 'no UCST response' /tmp/sntp.out; then
		    RETVAL=1
		    rm -f /tmp/sntp.out
		fi
                log_end_msg $RETVAL
                [ $RETVAL -eq 0 ] || [ $retry -ge "$RETRIES" ] && break
                sleep 2
                retry=$(($retry +1))
        done

       # [ $RETVAL -eq 0 ] && success || failure
        echo
        if [ $RETVAL -eq 0 ]; then
            touch $hwlockfile
            [ "$SYNC_HWCLOCK" = "yes" ] && \
                action $"Syncing hardware clock to system time" \
                    /sbin/hwclock --systohc
        fi
        return $RETVAL
}

# end of sntp-related declarations

DAEMON=/usr/bin/ntpd
PIDFILE=/var/run/ntpd.pid

test -x $DAEMON || exit 5

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

if [ -e /var/lib/ntp/ntp.conf.dhcp ]; then
	NTPD_OPTS="$NTPD_OPTS -c /var/lib/ntp/ntp.conf.dhcp"
fi


LOCKFILE=/var/lock/ntpdate

lock_ntpdate() {
	if [ -x /usr/bin/lockfile-create ]; then
		lockfile-create $LOCKFILE
		lockfile-touch $LOCKFILE &
		LOCKTOUCHPID="$!"
	fi
}

unlock_ntpdate() {
	if [ -x /usr/bin/lockfile-create ] ; then
		kill $LOCKTOUCHPID
		lockfile-remove $LOCKFILE
	fi
}

case $1 in
	start)
                sntpstart
		log_daemon_msg "Starting NTP server" "ntpd"
		lock_ntpdate
  		start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -p $PIDFILE $NTPD_OPTS
		status=$?
		unlock_ntpdate
		log_end_msg $status
  		;;
	stop)
		log_daemon_msg "Stopping NTP server" "ntpd"
  		start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
		log_end_msg $?
		rm -f $PIDFILE
  		;;
	restart|force-reload)
		$0 stop && sleep 2 && $0 start
  		;;
	try-restart)
		if $0 status >/dev/null; then
			$0 restart
		else
			exit 0
		fi
		;;
	reload)
		exit 3
		;;
	status)
		status_of_proc $DAEMON "NTP server"
		;;
	*)
		echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
		exit 2
		;;
esac
