#!/bin/sh

# PROVIDE: postsrsd
# REQUIRE: DAEMON
# BEFORE: mail
# KEYWORD: shutdown

# Define these postsrsd_* variables in one of these files:
#	/etc/rc.conf
#	/etc/rc.conf.local
#	/etc/rc.conf.d/postsrsd
#
# postsrsd_enable (bool):   Set to "NO" by default.
#                           Set it to "YES" to enable postsrsd.
# postsrsd_uid (str):       Set username to run postsrsd.
# postsrsd_secret (str):    Secret file file. See postsrsd(8)
# postsrsd_flags (str):     Flags passed to start command.

. /etc/rc.subr

name="postsrsd"
rcvar=postsrsd_enable

start_precmd="postsrsd_prepcmd"
command="%%PREFIX%%/sbin/postsrsd"
_piddir="/var/run/postsrsd"
pidfile="${_piddir}/${name}.pid"

load_rc_config $name

#
# DO NOT CHANGE THESE DEFAULT VALUES HERE
#
: ${postsrsd_enable="NO"}
: ${postsrsd_uid="mailnull"}
: ${postsrsd_secret="%%PREFIX%%/etc/postsrsd.secret"}

# Options other than above can be set with $postsrsd_flags.
# see postsrsd documentation for detail.

[ -n "${postsrsd_uid}" ] && _uid_prefix="-u"

command_args=" -D ${_uid_prefix} ${postsrsd_uid} -p ${pidfile} ${cmd_args} ${postsrsd_flags}"

postsrsd_prepcmd ()
{
    if [ ! -d ${_piddir} ] ; then
	mkdir -p ${_piddir}
    fi
    if [ -n "${postsrsd_uid}" ] ; then
	chown ${postsrsd_uid} ${_piddir}
    fi
    if [ ! -f "${postsrsd_secret}" ] ; then
	env -i tr -cd "[:alnum:]" < /dev/urandom | head -c 24 > "${postsrsd_secret}"
    fi
}

# to let rc.subr kill them all
unset pidfile

run_rc_command "$1"
