#!/sbin/openrc-run

extra_started_commands="data reload stats"
description_data="Write configuration data to /tmp/keepalived.data"
description_reload="Close down all interfaces, reload its configuration, and start up with new configuration"
description_stats="Print statistics info"

: ${cfgfile:="/etc/keepalived/keepalived.conf"}

command="/usr/sbin/keepalived"
# Note: KEEPALIVED_OPTS is for backward compatibility.
command_args="
	--dont-fork
	--use-file=$cfgfile
	${config_id:+"--config-id=$config_id"}
	${command_args:-${KEEPALIVED_OPTS:-}}"
command_background="yes"
pidfile="/run/$RC_SVCNAME.pid"

required_files="$cfgfile"

depend() {
	use logger
	# The interfaces do not actually need to exist to start,
	# it handles them gracefully.
	use net
}

data() {
	ebegin "Writing configuration data to /tmp/keepalived.data"
	start-stop-daemon --pidfile $pidfile --signal USR1
	eend $?
}

reload() {
	ebegin "Reloading keepalived.conf"
	start-stop-daemon --pidfile $pidfile --signal HUP
	eend $?
}

stats() {
	ebegin "Dumping stats"
	start-stop-daemon --pidfile $pidfile --signal USR2
	eend $?

	cat /tmp/keepalived.stats
}
