#!/bin/sh

. /etc/init.d/functions
. /etc/init.d/globals

chronyd=$(which chronyd)
chronyc=$(which chronyc)

fragment_conf=/etc/chrony.d/neutrino-ntpserver.conf
if [ ! -e $fragment_conf ]; then
	mkdir -p $(dirname $fragment_conf)
	# add ntpserver from neutrino.conf
	ntpserver=$(get_setting network_ntpserver)
	ntpserver=${ntpserver:-0.de.pool.ntp.org}
	echo -e "# managed by neutrino; do not edit\npool $ntpserver iburst prefer" > $fragment_conf
fi

start() {
	printf "[$BASENAME] Starting ... "
	$chronyd -U
	if [ $? -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
}

stop() {
	printf "[$BASENAME] Shutdown ... "
	$chronyc shutdown
}

restart() {
	stop
	start
}

case "$1" in
	start)
		start
	;;
	stop)
		stop
	;;
	restart)
		restart
	;;
	*)
		echo "[$BASENAME] Usage: $0 {start|stop|restart}"
	;;
esac
