#!/sbin/openrc-run

: ${name:="$RC_SVCNAME"}
: ${cfgfile:="/etc/clsync/${RC_SVCNAME%%.*}.conf"}

extra_started_commands="reload resync"
description_reload="Reread filter rules"
description_resync="Run full resync"

command="/usr/bin/clsync"
command_background="yes"
pidfile="/run/$RC_SVCNAME.pid"

if [ "$cfgfile" != '/NULL/' ]; then
	required_files="$cfgfile"
fi

start_pre() {
	local config_block output

	if grep -q '^background\s*=\s*1' "$cfgfile" 2>/dev/null; then
		ewarn "$cfgfile shouldn't set background = 1, it will make init script unreliable!"
	fi

	if [ -z "$config_block" ] && [ "${RC_SVCNAME#*.}" != "$RC_SVCNAME" ]; then
		config_block=${RC_SVCNAME#*.}
	fi
	if [ -z "$output_log$error_log" ]; then
		output='syslog'
	fi

	command_args="
		--config-file $cfgfile
		${config_block:+"--config-block $config_block"}
		${output:+"--output $output"}
		$command_args"
}

reload() {
	ebegin "Reloading $name filter rules"
	start-stop-daemon --signal HUP --pidfile $pidfile
	eend $?
}

resync() {
	ebegin "Executing $name full resync"
	start-stop-daemon --signal 12 --pidfile $pidfile
	eend $?
}
