#!/bin/sh
exec 2>&1

[ -f ./conf ] && . ./conf

# Make sure a configuration file exists
: ${CONFIG:=/etc/duiadns.conf}
[ -e "${CONFIG}" ] || exit 1

if command -v snooze >/dev/null 2>&1; then
	# Use snooze if available because time control is better

	# PERIOD is a set of snooze arguments in this context
	: ${PERIOD:=-H /1 -M /5 -s 5m -T 1m}
	# TIMEFILE tracks the last runtime
	: ${TIMEFILE:=/var/cache/duiadns/timefile}

	exec chpst -u _duiadns:_duiadns \
		snooze ${PERIOD} -t "${TIMEFILE}" \
			sh -c "/usr/bin/duiadns '${CONFIG}'; touch '${TIMEFILE}'"
else
	# Fall back to dumb sleep if snooze is not available
	# PERIOD is just the number of seconds to sleep in this context
	sleep "${PERIOD:-300}" || exit 1
	exec chpst -u _duiadns:_duiadns /usr/bin/duiadns "${CONFIG}"
fi
