#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

START=60

start() {
	[ ! -f /var/run/chronyd.pid ] && (
		/usr/sbin/chronyd -f /etc/chrony/chrony.conf
		
		local NTP_SERVERS
		local NTP_SERVER
		local CLIENT_NETWORKS
		local NETNAME
		config_load ntpd
		config_get NTP_SERVERS cfg1 TimeServers
		config_get CLIENT_NETWORKS cfg1 ClientAccessFrom
		CHRONY_KEY=$(awk '/^1 / { print $2; }' /etc/chrony/chrony.keys)
		include /lib/network
		scan_interfaces
		
		# Define servers and do a rapid series of time measurements
		# to get up to date
		(
		 echo password $CHRONY_KEY
	  	 for NTP_SERVER in $NTP_SERVERS; do
	  	     echo add server $NTP_SERVER
	  	 done
	  	 echo burst 5/5
	  	) | chronyc > /dev/null 2>&1 
	  	
	  	# ... wait until a reference ID is found ...
	  	while chronyc tracking | grep 'Reference ID.*0.0.0.0' >/dev/null ; do sleep 1; done 
	  	
	  	 
	  	# ... and finally, adjust the time in a single step
	  	# and allow clients to access the server
		(
		 echo password $CHRONY_KEY
	  	 echo makestep
	  	 for NETNAME in $CLIENT_NETWORKS; do
		     local INTERFACE
		     local IP
		     local NETMASK
		     local BROADCAST
		     local NETWORK
		     local PREFIX
		
		     config_get INTERFACE "$NETNAME" ifname
		     if [ -n "$INTERFACE" ] ; then
		         eval $(ifconfig $INTERFACE | grep inet | sed -e 's/.*addr:/IP=/' -e 's/ .*Mask:/; NETMASK=/')
		         eval $(ipcalc $IP $NETMASK)
	  	         echo allow $NETWORK/$PREFIX
	  	     fi
	  	 done
		) | chronyc > /dev/null 2>&1
		
	)
}

stop() {
	[ -r /var/run/chronyd.pid ] && PID=$(cat /var/run/chronyd.pid)
	[ -n "$PID" ] && kill $PID 
}

restart() {
	stop
	while [ -r /var/run/chronyd.pid ] ; do sleep 1; done
	start
}
