NTPDATE_CONF=/etc/default/ntpdate
NTPDATE_DHCP_CONF=/run/ntpdate.dhcp


ntp_servers_setup_remove() {
	rm -f $NTPDATE_DHCP_CONF
}


ntp_servers_setup_add() {
	if [ -e $NTPDATE_DHCP_CONF ] && [ "$new_ntp_servers" = "$old_ntp_servers" ]; then
		return
	fi

	if [ -z "$new_ntp_servers" ]; then
		ntp_servers_setup_remove
		return
	fi

	tmp=$(mktemp "$NTPDATE_DHCP_CONF.XXXXXX") || return
	chmod --reference=$NTPDATE_CONF $tmp
	chown --reference=$NTPDATE_CONF $tmp

	(
	  echo "# NTP server entries received from DHCP server"
	  echo "NTPSERVERS='$new_ntp_servers'"
	) >>$tmp
	
	mv $tmp $NTPDATE_DHCP_CONF
}


ntp_servers_setup() {
	case $reason in
		BOUND|RENEW|REBIND|REBOOT)
			ntp_servers_setup_add
			;;
		EXPIRE|FAIL|RELEASE|STOP)
			ntp_servers_setup_remove
			;;
	esac
}


ntp_servers_setup
