#!/bin/sh
# $FreeBSD: ports/mail/dkimproxy/files/dkimproxy_in.in,v 1.3 2006/09/18 14:15:22 rafan Exp $
#

# PROVIDE: dkimproxy_in
# REQUIRE: LOGIN
# BEFORE: mail
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable dkimproxy.in:
#
# dkimproxy_in_enable="YES"
#

. %%RC_SUBR%%

name=dkimproxy_in
rcvar=`set_rcvar`

# set defaults

: ${dkimproxy_in_enable="NO"}
: ${dkimproxy_in_flags="127.0.0.1:10025 127.0.0.1:10026"}
: ${dkimproxy_in_pidfile="/var/run/dkimproxy_in.pid"}
: ${dkimproxy_in_user="dkimproxy"}

start_cmd="dkimproxy_in_start"
stop_cmd="dkimproxy_in_stop"

dkimproxy_in_start()
{
	if [ -f ${dkimproxy_in_pidfile} ]; then
		echo "${name} already running?"
		exit 1
	fi
	if pw user show "${dkimproxy_in_user}" > /dev/null 2>&1; then else
		echo "dkimproxy_in_user: ${dkimproxy_in_user} not found"
		exit 1
	fi
	echo "Starting ${name}"
	logger -t ${name} "Starting ${name}"
	touch ${dkimproxy_in_pidfile}
	chown ${dkimproxy_in_user} ${dkimproxy_in_pidfile}
	tmpfile=`mktemp /tmp/${name}.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
	su -m ${dkimproxy_in_user} -c "daemon -c -p ${dkimproxy_in_pidfile} \
		%%PREFIX%%/bin/dkimproxy.in ${dkimproxy_in_flags}" \
		> /dev/null 2> ${tmpfile}
	# wait until dkimproxy start.
	while true
	do
		filesize=`ls -l ${tmpfile}|awk '{print $5}'`
		if [ ${filesize} -gt 0 ]; then
			break
		fi
	done

	logger -t ${name} "`cat ${tmpfile}`"
	err=`grep Error ${tmpfile}`
	if [ "${err}" ]; then
		echo "Failed to start ${name}."
		echo "${err}"
		rm -f ${dkimproxy_in_pidfile}
	else
		# To prevent DoS attack by dkimproxy_in_user.
		chown root:wheel ${dkimproxy_in_pidfile}
	fi
	rm -f ${tmpfile}
}

dkimproxy_in_stop()
{
	if [ ! -f ${dkimproxy_in_pidfile} ]; then
		echo "${name} not running?"
		exit 1
	fi
	echo "Stopping ${name}"
	logger -t ${name} "Stopping ${name}"
	kill `cat ${dkimproxy_in_pidfile}`
	rm -f ${dkimproxy_in_pidfile}
}

load_rc_config $name
run_rc_command "$1"


syntax highlighted by Code2HTML, v. 0.9.1