#!/bin/sh

[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1


ntp_server_conf() {
  cat <<__EOF__
# This is the most basic ntp configuration file
# The driftfile must remain in a place specific to this
# machine - it records the machine specific clock error
driftfile /var/lib/ntp/drift
# This should be a server that is close (in IP terms)
# to the machine.  Add other servers as required.
# Unless you un-comment the line below ntpd will sync
# only against the local system clock.
#
# server time.server.example.com
#
# Using local hardware clock as fallback
# Disable this when using ntpd -q -g -x as ntpdate or it will sync to itself
#server 127.127.1.0
#fudge 127.127.1.0 stratum 14

$(for ntp_serv in ${ntpsrv};do echo "server $ntp_serv iburst";done)
$(for srv in 0 1 2 3;do echo "server ${srv}.za.pool.ntp.org iburst";done)

# Defining a default security setting
restrict -4 default limited kod notrap nomodify noquery
restrict -6 default limited kod notrap nomodify noquery

restrict 127.0.0.1    # allow local host
restrict ::1          # allow local host

broadcast ff02::101
broadcast ff05::101
broadcast 224.0.1.1

disable auth
multicastclient ff02::101
multicastclient ff05::101
multicastclient 224.0.1.1

__EOF__
}

case "$1" in
	renew|bound)
		if [ "$interface" == "eth0" ] || [ "$interface" == "wlan0" ];then
			if [ "${ntpsrv}" ];then
				ntp_server_conf > /tmp/ntp.conf
				if ! /bin/pidof ntpd >/dev/null ;then
					/usr/sbin/ntpd -c /tmp/ntp.conf -qxg >/dev/null 2>&1
				fi;
			fi;
		fi;
		;;
esac
