#!/bin/sh
#
# Generic start/stop script for MIMEDefang.  Should work on most
# flavors of UNIX.

RETVAL=0
prog='mimedefang'
SPOOLDIR='/var/spool/MIMEDefang'
PID="$SPOOLDIR/$prog.pid"
MXPID="$SPOOLDIR/$prog-multiplexor.pid"

# Is the program executable?  We search in /usr/bin and /usr/local/bin.

if [ -x /usr/bin/$prog ] ; then
    PROGDIR=/usr/bin
elif [ -x /usr/bin/$prog ] ; then
    PROGDIR=/usr/bin
elif [ -x /usr/local/bin/$prog ] ; then
    PROGDIR=/usr/local/bin
else
    exit 0
fi

# The settings which follow are defaults.  You can override them by
# placing assignments in /etc/mail/mimedefang.conf

# The socket used by mimedefang to communicate with sendmail
# SOCKET=$SPOOLDIR/mimedefang.sock

# Maximum number of processes to run (mail received while this many
# processes are running is rejected with a temporary failure, so be
# wary of how many emails you receive at a time).  This applies only
# if you do NOT use the multiplexor
# MAXIMUM=10

# If you want the multiplexor, you need to uncomment this line
MULTIPLEXOR=yes

# Run the multiplexor and filters as this user, not root.  RECOMMENDED
# MX_USER=defang

# If you want to keep spool directories around if the filter fails,
# set the next one to yes
# KEEP_FAILED_DIRECTORIES=no

# The following apply only to the multiplexor and are used only when
# the MULTIPLEXOR variable is 'yes'

# "yes" turns on the multiplexor relay checking function
# MX_RELAY_CHECK=no

# "yes" turns on the multiplexor sender checking function
# MX_SENDER_CHECK=no

# "yes" turns on the multiplexor recipient checking function
# MX_RECIPIENT_CHECK=no

# Set to yes if you want the multiplexor to log events to syslog
MX_LOG=yes

# The multiplexor does not start all slaves at the same time.  Instead,
# it starts one slave every MX_SLAVE_DELAY seconds when the system is idle.
# (If the system is busy, the multiplexor starts slaves as incoming mail
# requires attention.)
# MX_SLAVE_DELAY=3

# The next setting is an absolute limit on slave activation.  The multiplexor
# will NEVER activate a slave within MX_MIN_SLAVE_DELAY seconds of another.
# The default of zero means that the multiplexor will activate slaves as
# quickly as necessary to keep up with incoming mail.
# MX_MIN_SLAVE_DELAY=0

# Set to yes if you want the multiplexor to log stats in
# /var/log/mimedefang/stats
# MX_STATS=no

# Set to yes if you want the stats file flushed after each entry
# MX_FLUSH_STATS=no

# Set to yes if you want the multiplexor to log stats to syslog
# MX_STATS_SYSLOG=no

# The socket used by the multiplexor
# MX_SOCKET=$SPOOLDIR/mimedefang-multiplexor.sock

# Maximum # of requests a process handles
# MX_REQUESTS=100

# Minimum number of processes to keep.  The default of 0 is probably
# too low; we suggest 2 instead.
MX_MINIMUM=2

# Maximum number of processes to run (mail received while this many
# processes are running is rejected with a temporary failure, so be
# wary of how many emails you receive at a time).  This applies only
# if you DO use the multiplexor.  The default value of 2 is probably
# too low; we suggest 10 instead
MX_MAXIMUM=10

# Limit slave processes' resident-set size to this many kilobytes.  Default
# is unlimited.
# MX_MAX_RSS=10000

# Limit total size of slave processes' memory space to this many kilobytes.
# Default is unlimited.
# MX_MAX_AS=30000

# Number of seconds a process should be idle before checking for
# minimum number and killed
# MX_IDLE=300

# Number of seconds a process is allowed to scan an email before it is
# considered dead.  The default is 30 seconds; we suggest 300.
MX_BUSY=300

# SUBFILTER specifies which filter rules file to use
# SUBFILTER=/etc/mail/mimedefang-filter


# Source configuration
if [ -f /etc/mail/$prog.conf ] ; then
    . /etc/mail/$prog.conf
fi

# Make sure reqired vars are set
SOCKET=${SOCKET:=$SPOOLDIR/$prog.sock}
MX_SOCKET=${MX_SOCKET:=$SPOOLDIR/$prog-multiplexor.sock}

start() {
    if test -r $PID ; then
	if kill -0 `cat $PID` > /dev/null 2>&1 ; then
	    echo "mimedefang (`cat $PID`) seems to be running."
	    return 1
	fi
    fi
    if test "$MULTIPLEXOR" = "yes" -a -r $MXPID ; then
	if kill -0 `cat $MXPID` > /dev/null 2>&1 ; then
	    echo "mimedefang-multiplexor (`cat $MXPID`) seems to be running."
	    return 1
	fi
    fi

    if [ "$MULTIPLEXOR" = "yes" ]; then
	printf "%-60s" "Starting $prog-multiplexor: "
	rm -f $MX_SOCKET > /dev/null 2>&1
	$PROGDIR/$prog-multiplexor -p $MXPID \
	    `[ -n "$FILTER" ] && echo "-f $FILTER"` \
	    `[ -n "$SUBFILTER" ] && echo "-F $SUBFILTER"` \
	    `[ -n "$MX_MINIMUM" ] && echo "-m $MX_MINIMUM"` \
	    `[ -n "$MX_MAXIMUM" ] && echo "-x $MX_MAXIMUM"` \
	    `[ -n "$MX_USER" ] && echo "-U $MX_USER"` \
	    `[ -n "$MX_IDLE" ] && echo "-i $MX_IDLE"` \
	    `[ -n "$MX_BUSY" ] && echo "-b $MX_BUSY"` \
	    `[ -n "$MX_REQUESTS" ] && echo "-r $MX_REQUESTS"` \
	    `[ -n "$MX_SLAVE_DELAY" ] && echo "-w $MX_SLAVE_DELAY"` \
	    `[ -n "$MX_MIN_SLAVE_DELAY" ] && echo "-W $MX_MIN_SLAVE_DELAY"` \
	    `[ -n "$MX_MAX_RSS" ] && echo "-R $MX_MAX_RSS"` \
	    `[ -n "$MX_MAX_AS" ] && echo "-M $MX_MAX_AS"` \
	    `[ "$MX_LOG" = "yes" ] && echo "-l"` \
	    `[ "$MX_STATS" = "yes" ] && echo "-t /var/log/mimedefang/stats"` \
	    `[ "$MX_STATS" = "yes" -a "$MX_FLUSH_STATS" = "yes" ] && echo "-u"` \
	    `[ "$MX_STATS_SYSLOG" = "yes" ] && echo "-T"` \
	    -s $MX_SOCKET
	RETVAL=$?
	if [ $RETVAL = 0 ] ; then
	    echo "[  OK  ]"
	else
	    echo "[FAILED]"
	    return 1
	fi
    fi

    # Start mimedefang
    printf "%-60s" "Starting $prog: "
    rm -f $SOCKET > /dev/null 2>&1
    $PROGDIR/$prog -P $PID \
	`[ -n "$MX_USER" ] && echo "-U $MX_USER"` \
	`[ "$MULTIPLEXOR" != "yes" -a -n "$FILTER" ] && echo "-f $FILTER"` \
	`[ "$MULTIPLEXOR" != "yes" -a -n "$SUBFILTER" ] && echo "-F $SUBFILTER"` \
	`[ "$MULTIPLEXOR" != "yes" -a -n "$MAXIMUM" ] && echo "-n $MAXIMUM"` \
	`[ "$MULTIPLEXOR" = "yes" ] && echo "-m $MX_SOCKET"` \
	`[ "$MULTIPLEXOR" = "yes" -a "$MX_RELAY_CHECK" = "yes" ] && echo "-r"` \
	`[ "$MULTIPLEXOR" = "yes" -a "$MX_SENDER_CHECK" = "yes" ] && echo "-s"` \
	`[ "$MULTIPLEXOR" = "yes" -a "$MX_RECIPIENT_CHECK" = "yes" ] && echo "-t"` \
	`[ "$KEEP_FAILED_DIRECTORIES" = "yes" ] && echo "-k"` \
	-p $SOCKET
    RETVAL=$?
    if [ $RETVAL = 0 ] ; then
	echo "[  OK  ]"
    else
	echo "[FAILED]"
	kill `cat $MXPID`
	return 1
    fi
    return 0
}

stop() {
    # Stop daemon
    printf "%-60s" "Shutting down $prog: "
    if test -f "$PID" ; then
	kill `cat $PID`
	RETVAL=$?
    else
	RETVAL=1
    fi
    if [ $RETVAL = 0 ] ; then
	echo "[  OK  ]"
    else
	echo "[FAILED]"
    fi

    rm -f $SOCKET > /dev/null 2>&1
    rm -f $PID > /dev/null 2>&1

    if [ "$MULTIPLEXOR" = "yes" ]; then
        # Stop daemon
	printf "%-60s" "Shutting down $prog-multiplexor: "
	if test -f "$MXPID" ; then
	    kill `cat $MXPID`
	    RETVAL=$?
	else
	    RETVAL=1
	fi
	if [ $RETVAL = 0 ] ; then
	    echo "[  OK  ]"
	else
	    echo "[FAILED]"
	fi

	rm -f $MX_SOCKET > /dev/null 2>&1
	rm -f $MXPID > /dev/null 2>&1
    fi
}

# See how we were called.
case "$1" in
  start)
  start
    ;;

  stop)
  stop
    ;;

  restart)
    stop
    start
    RETVAL=$?
    ;;

  reread|reload)
    if [ "$MULTIPLEXOR" != "yes" ]; then
	exit 0
    fi
    if [ -r $MXPID ] ; then
	kill -INT `cat $MXPID`
	RETVAL=$?
	if [ $RETVAL = 0 ] ; then
	    echo "Told $prog-multiplexor to force reread of filter rules."
	else
	    echo "Could not signal $prog-multiplexor"
	fi
    else
	RETVAL=1
	echo "Could not find process-ID of $prog-multiplexor"
    fi
    ;;

  *)
    echo "Usage: $0 {start|stop|restart|reread|reload}"
    exit 1
esac

exit $RETVAL
