#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

PIDDIR=/run/spamd
PIDFILE=${PIDDIR}/spamd.pid

extra_started_commands="reload"

depend() {
	before mta
	use logger
	use mysql
}

start() {
	ebegin "Starting spamd"

	# Ensure that the PID file's directory exists.
	checkpath --directory "${PIDDIR}"

	# Reloading spamd causes its PID to change, so we track it by
	# name instead.
	start-stop-daemon --start --quiet \
		--name spamd \
		--nicelevel ${SPAMD_NICELEVEL} \
		--pidfile ${PIDFILE} \
		--exec /usr/sbin/spamd -- \
			--daemonize \
			--pidfile=${PIDFILE} \
			${SPAMD_OPTS}

	retval=$?

	eend ${retval} "Failed to start spamd"
}

stop() {
	ebegin "Stopping spamd"
	# Retry after SPAMD_TIMEOUT seconds because spamd can take a
	# while to kill off all of its children. This was bug 322025.
	start-stop-daemon --stop \
			  --quiet \
			  --retry ${SPAMD_TIMEOUT} \
			  --pidfile ${PIDFILE}
	eend $? "Failed to stop spamd"
}

reload() {
	ebegin "Reloading configuration"
	# Warning: causes the PID of the spamd process to change.
	start-stop-daemon --signal HUP --quiet --pidfile ${PIDFILE}
	eend $?
}
