#!/bin/bash
#
# VERSION=3
# CHANGES="do not hang so long if dns-server is not reachable, but continue anyway if ntp is not reachable by ping"

BEROCONF=/usr/fallback/beroconf

function log () {
	echo "[init_synctime] ${1}"
}

function setup_cron {
	if [ ! -d ${CRONDIR} ] ; then
		mkdir -p /usr/conf/cron
	fi

	crontab -l | grep -v "update-time" > /tmp/crontab.synctime
	if [ "${1}" = "on" ]; then
		log "Adding cron-job."
		echo "*/15 * * * * /usr/sbin/update-time" >> /tmp/crontab.synctime
	else
		log "Removing cron-job."
	fi
	crontab /tmp/crontab.synctime
	rm -f /tmp/crontab.synctime
}

function start_time () {
	log "Updating system-time."
	/usr/sbin/update-time > /dev/null
}

case "${1}" in
	start)
		# setup_cron
		setup_cron "on"
		start_time
		;;
	stop)
		setup_cron "off"
		;;
	restart)
		${0} stop
		${0} start
		;;
	*)
		echo "Usage: ${0} {start|stop|restart}" >&2
		exit 1
		;;
esac
