#!/bin/sh
#
# $NetBSD: proxsmtpd.sh,v 1.1.1.1 2004/12/02 04:08:18 jlam Exp $
#
# PROVIDE: proxsmtpd
# REQUIRE: LOGIN
# BEFORE: mail
# KEYWORD: shutdown
#

if [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
fi

name="proxsmtpd"
rcvar=$name
command="/usr/pkg/sbin/${name}"

start_precmd="proxsmtpd_prestart"
stop_postcmd="proxsmtpd_poststop"

user="nobody"
tempdir="/tmp"
pidfile=/var/run/${name}.pid
conffile="/usr/pkg/etc/${name}.conf"
if [ -f "${conffile}" ]; then
	user=`/usr/bin/awk 'BEGIN {r = "nobody"}; /^User:/ {r = $2}; END {print r}' ${conffile}`
	tempdir=`/usr/bin/awk 'BEGIN {r = "/tmp"}; /^TempDirectory:/ {r = $2}; END {print r}' ${conffile}`

	case ${tempdir} in
	/tmp)	;;
	*)	pidfile="${tempdir}/${name}.pid" ;;
	esac
fi

command_args="-p ${pidfile}"

proxsmtpd_prestart()
{
	case ${tempdir} in
	/tmp)	;;
	*)	if [ ! -d "${tempdir}" ]; then
			/bin/mkdir -p -p ${tempdir}
		fi
		/usr/sbin/chown -R ${user} ${tempdir}
		/bin/chmod -R 0700 ${tempdir}
		;;
	esac
	/usr/bin/touch ${pidfile}
	/usr/sbin/chown ${user} ${pidfile}
}

proxsmtpd_poststop()
{
	/bin/rm -f ${pidfile}
	case ${tempdir} in
	/tmp)	;;
	*)	/bin/rmdir -p ${tempdir} 2>/dev/null || true
	esac
}

if [ -f /etc/rc.subr -a -d /etc/rc.d -a -f /etc/rc.d/DAEMON ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	if [ -f /etc/rc.conf ]; then
		. /etc/rc.conf
	fi
	echo -n " ${name}"
	eval ${start_precmd}
	${command} ${proxsmtpd_flags} ${command_args}
fi
