#!/sbin/openrc-run
#
# OpenRC init script for chronyd NTP time daemon.
#

description="Chrony time daemon"

command="/usr/sbin/chronyd"
pidfile="/run/chrony/chronyd.pid"

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

start_pre() {
	[ -x "$command" ] || return 0
	# shellcheck disable=SC1091
	[ -f /etc/default/chrony ] && . /etc/default/chrony
	if status_of_proc -p "$pidfile" "$command" chronyd >/dev/null 2>&1; then
		ewarn "chronyd is already running"
		return 0
	fi
	return 0
}

start() {
	[ -x "$command" ] || return 0
	# shellcheck disable=SC1091
	[ -f /etc/default/chrony ] && . /etc/default/chrony

	ebegin "Starting chronyd"
	start-stop-daemon --start --quiet --pidfile "$pidfile" --exec "$command" -- ${DAEMON_OPTS:-}
	eend $?
}

stop() {
	[ -x "$command" ] || return 0

	ebegin "Stopping chronyd"
	start-stop-daemon --stop --quiet --quiet --pidfile "$pidfile" --exec "$command" || true
	eend 0
}

status() {
	status_of_proc -p "$pidfile" "$command" chronyd
}
