#!/bin/sh

# PROVIDE: stunnel
# REQUIRE: NETWORKING SERVERS
# BEFORE: DAEMON
# KEYWORD: shutdown

#
# Add some of the following variables to /etc/rc.conf to configure stunnel:
# stunnel_enable (bool):	Set to "NO" by default.
#				Set it to "YES" to enable stunnel.
# stunnel_config (str):		Default "/usr/local/etc/stunnel/stunnel.conf"
#				Set it to the full path to the config file
#				that stunnel will use during the automated
#				start-up.
# stunnel_pidfile (str):	Default "/var/run/stunnel/stunnel.pid"
#				Set it to the value of 'pid' in
#				the stunnel.conf file.
# stunnel_uid (str):		Default "stunnel"
# stunnel_gid (str):		Default "stunnel"
#

. /etc/rc.subr

name="stunnel"
rcvar=stunnel_enable

load_rc_config $name

: ${stunnel_enable="NO"}
: ${stunnel_config="/usr/local/etc/stunnel/${name}.conf"}
: ${stunnel_pidfile="/var/run/stunnel/stunnel.pid"}
: ${stunnel_uid="stunnel"}
: ${stunnel_gid="stunnel"}

command="/usr/local/bin/stunnel"
command_args=${stunnel_config}
pidfile=${stunnel_pidfile}

required_files="${stunnel_config}"

start_precmd=stunnel_start_precmd

stunnel_start_precmd () {
	local piddir
	piddir=`/usr/bin/dirname "${pidfile}"`
	if [ ! -d "${piddir}" ] ; then
		/usr/bin/install -d -o "${stunnel_uid}" -g "${stunnel_gid}" "${piddir}"
	fi
}

run_rc_command "$1"
