#!/bin/sh
#
# amavisd	Startup script for the amavisd-new daemon.

# chkconfig: - 77 31
# description: amavisd is an interface between MTA and content checkers
# processname: amavisd
# config:       /etc/amavis/amavisd.conf
# pidfile: /var/run/amavis/amavisd.pid
### BEGIN INIT INFO
# Provides:          amavisd
# Required-Start:    $syslog $network $local_fs $remote_fs
# Required-Stop:     $syslog $network $local_fs $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts amavisd-new mailfilter
# Description:       Launches the amavisd-new mailfilter
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

#DAEMON=/usr/sbin/amavisd
CONFIGFILE=/etc/amavis/amavisd.conf
CONFIGDIR=/etc/amavis/conf.d
PIDFILE=/var/run/amavis/amavisd.pid
LOCKFILE=/var/lock/subsys/amavisd
RETVAL=0

start()
{
	get_conf
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user mail -- \
	amavisd $CONF -P "$PIDFILE" start
	RETVAL=$?
	return $RETVAL
}
stop()
{
	stop_daemon --pidfile "$PIDFILE" --expect-user mail --name amavisd -- amavisd -P "$PIDFILE" stop
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	return $RETVAL
}
restart()
{
	stop
	start
}
reload()
{
	get_conf
        action "Reloading amavisd:" amavisd $CONF -P "$PIDFILE" reload
	RETVAL=$?
	return $RETVAL
}
debug()
{
	get_conf
        echo "Trying to run amavisd-new in debug mode..." 
	exec amavisd $CONF -P "$PIDFILE" debug
}
get_conf()
{
	CONF=""
	if [ -e "$CONFIGFILE" ] ; then 
	    echo "Please update the amavisd-new configuration." 2>&1
	    echo "Remove the $CONFIGFILE to enable new scheme configs." 2>&1
	    echo "Use old config file $CONFIGFILE"
	    CONF="-c $CONFIGFILE"
	else
	    if [ -d "$CONFIGDIR" ] ; then
		for i in `ls -1 $CONFIGDIR |grep conf$`
		do
		    CONF="$CONF -c $CONFIGDIR/$i"
		done
	    fi
	fi
}

# See how we were called.
case "$1" in
        start)
		start
		;;
	stop)
		stop
		;;
	restart)
	        restart
		;;
	reload)
	        reload
		;;
	condstop)
	        if [ -e "$LOCKFILE" ]; then
		stop
		fi
		;;
	condrestart)
	        if [ -e "$LOCKFILE" ]; then
		restart
		fi
		;;
        condreload)
	        if [ -e "$LOCKFILE" ]; then
		reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user mail --name amavisd -- amavisd
		RETVAL=$? 
		;;
	debug)
		debug
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status|debug}"
		RETVAL=1
esac

exit $RETVAL
													       
