#!/bin/sh
# $FreeBSD$
#
# PROVIDE: ratelimit_policyd
# REQUIRE: DAEMON
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable nginx:
# ratelimit_policyd_enable (bool):  Set to "NO" by default.
#                                   Set it to "YES" to enable ratelimit_policyd
# ratelimit_policyd_user (str):     User to run as. Default: mailnull
# ratelimit_policyd_group (str):    Group to run as. Default: mailnull
# ratelimit_policyd_log (str):      Log filename
#                                   Must match settings in ratelimit-policyd.cfg

. /etc/rc.subr

name=ratelimit_policyd
rcvar=ratelimit_policyd_enable

extra_commands="stats"
start_precmd="ratelimit_policyd_prestart"
stop_cmd="ratelimit_policyd_stop"
stats_cmd="ratelimit_policyd_stats"

load_rc_config $name

: ${ratelimit_policyd_enable="NO"}
: ${ratelimit_policyd_user="mailnull"}
: ${ratelimit_policyd_group="mailnull"}
: ${ratelimit_policyd_log="/var/log/ratelimit-policyd.log"}

required_files="/usr/local/etc/ratelimit-policyd.cfg"
pidfile="/var/run/ratelimit-policyd/ratelimit-policyd.pid"
procname="/usr/local/bin/ratelimit-policyd.pl"
command="/usr/sbin/daemon"
command_args="-f -p ${pidfile} ${procname}"

ratelimit_policyd_prestart()
{
	if [ ! -f "${ratelimit_policyd_log}" ]; then
		install -o ${ratelimit_policyd_user} -m 640 /dev/null "${ratelimit_policyd_log}"
	fi

	piddir=${pidfile%/*}
	if [ ! -d "${piddir}" ]; then
		install -d -o ${ratelimit_policyd_user} -g ${ratelimit_policyd_group} -m 755 "${piddir}"
	fi
}

ratelimit_policyd_stop()
{
	## ugly workaround - there is some problem running this on FreeBSD
	## It does not terminate on SIGTERM until next TCP request is made.
	## send SIGTERM first
	test -f "${pidfile}" && kill -TERM $(head -n 1 "${pidfile}")
	## then do TCP connect by calling printshm which internally call
	## echo "printshm" | nc -N -w 5 $listen_address $port
	${procname} printshm
	test -f "${pidfile}" && wait_for_pids $(head -n 1 "${pidfile}")
}

ratelimit_policyd_stats()
{
	${procname} printshm
}

run_rc_command $1
