#!/bin/sh
#
# $NetBSD: clamsmtpd.sh,v 1.8 2007/06/15 09:47:06 martti Exp $
#
# PROVIDE: clamsmtpd
# REQUIRE: LOGIN clamd
# BEFORE: mail
# KEYWORD: shutdown
#
# You will need to set some variables in /etc/rc.conf to start clamsmtpd:
#
# clamsmtpd=YES
#
# The following variables are optional:
#
#       clamsmtpd_maxwait="600"             # max wait time for clamd
#       clamsmtpd_user="clamav"      # user to run clamsmtpd as
#       clamsmtpd_addr="localhost:10026"    # address to forward mail to;
#                                           # see clamsmtpd(8).
#

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

name="clamsmtpd"
rcvar="${name}"
command="/usr/pkg/sbin/${name}"
pidfile="/var/run/clamsmtpd.pid"

start_precmd="clamsmtpd_prestart"
start_cmd="clamsmtpd_start"

clamav_conffile="/usr/pkg/etc/clamd.conf"
if [ -f "${clamav_conffile}" ]; then
	socket=`/usr/bin/awk 'BEGIN {r = "/tmp/clamd"};
			/^#/ {next}; /^LocalSocket[ 	]/ {r = $2};
			END {print r}' ${clamav_conffile}`
	: ${clamsmtpd_user=`/usr/bin/awk 'BEGIN {r = "clamav"};
			/^#/ {next}; /^User[ 	]/ {r = $2};
			END {print r}' ${clamav_conffile}`}
else
	: ${socket="/tmp/clamd"}
	: ${clamsmtpd_user="clamav"}
fi
: ${clamsmtpd_maxwait="600"}

clamsmtpd_prestart()
{
	if [ ! -S "${socket}" ]; then
		echo -n "Waiting max ${clamsmtpd_maxwait} seconds for clamd to become ready"
		while [ ${clamsmtpd_maxwait} -gt 0 -a ! -S "${socket}" ]; do
			echo -n "."
			sleep 10
			clamsmtpd_maxwait=$((clamsmtpd_maxwait - 10))
		done
		if [ ! -S "${socket}" ]; then
			echo ""
			echo "ERROR: Unable to start clamsmtpd as clamd is not running!"
			exit 1
		fi

		# Wait another 10 seconds so that clamd is really ready
		echo -n "."
		sleep 10
		echo ""
	fi
	/usr/bin/touch ${pidfile}
	/usr/sbin/chown ${clamsmtpd_user} ${pidfile}
}

clamsmtpd_start()
{
	echo "Starting ${name}."
	doit="${command} ${clamsmtpd_flags} -p ${pidfile}"
	/usr/bin/su -m ${clamsmtpd_user} -c "${doit}"
}

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}
	eval ${start_cmd}
fi
