#!/sbin/openrc-run
#
# OpenRC init script for htpdate.
# Based on original upstream script and Debian SysV packaging.
#

description="HTTP Time Protocol daemon"

command="/usr/sbin/htpdate"
pidfile="/run/htpdate.pid"

if [ -f /etc/default/htpdate ]; then
	. /etc/default/htpdate
fi

depend() {
	need net localmount
	use dns logger
	after bootmisc
}

start() {
	[ -x "$command" ] || return 0
	if [ ! -f /etc/default/htpdate ]; then
		return 0
	fi
	if [ "$HTP_DAEMON" = "no" ]; then
		ewarn "htpdate is disabled. Please see /etc/default/htpdate."
		return 0
	fi

	# Set the time first before daemonizing, because the time offset
	# might be too big for smooth time adjustment (-D forks).
	ebegin "Starting HTTP Time Protocol daemon"
	start-stop-daemon --start --quiet --pidfile "$pidfile" --exec "$command" -- \
		$HTP_OPTIONS $HTP_PROXY -i "$pidfile" $HTP_SERVERS
	eend $?
}

stop() {
	ebegin "Stopping HTTP Time Protocol daemon"
	if [ -f "$pidfile" ]; then
		start-stop-daemon --stop --quiet --pidfile "$pidfile" --retry=TERM/10/KILL/5
		RETVAL=$?
	else
		ewarn "$pidfile not found"
		RETVAL=0
	fi
	rm -f "$pidfile"
	eend $RETVAL
}

reload() {
	ebegin "Reloading htpdate"
	start-stop-daemon --stop --signal HUP --quiet --exec "$command"
	eend $?
}
