#!/sbin/openrc-run
# Copyright 2015-2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

command="/usr/sbin/rspamd"
command_rspamadm="/usr/bin/rspamadm"
command_args="${RSPAMD_OPTS} -c \"${RSPAMD_CONFIGFILE}\" -u ${RSPAMD_USER} -g ${RSPAMD_GROUP} --pid \"${RSPAMD_PIDFILE}\""
start_stop_daemon_args=${RSPAMD_SSDARGS}
pidfile="${RSPAMD_PIDFILE}"
retry=${RSPAMD_TERMTIMEOUT}

required_files="${RSPAMD_CONFIGFILE}"

description="Rapid spam filtering system"

extra_commands="checkconfig"
extra_started_commands="reload"

description_checkconfig="Validate rspamd's configuration"
description_reload="Sends rspamd a signal to reload its configuration"

depend() {
	before mta
	use dns redis
}

checkconfig() {
	${command_rspamadm} configtest 1>/dev/null || return 1
}

reload() {
	checkconfig || return 1

	ebegin "Reloading ${SVCNAME}"
	start-stop-daemon --signal HUP --pidfile "${pidfile}"
	eend $?
}

start_pre() {
	if [ "${RC_CMD}" != "restart" ]; then
		checkconfig || return 1
	fi
}

stop_pre() {
	if [ "${RC_CMD}" = "restart" ]; then
		checkconfig || return 1
	fi
}
