#!/bin/bash

##########################################################################
# Copyright @2002, Roaring Penguin Software Inc.  All rights reserved.
#
# Project     : MIMEDefang
# Component   : redhat/mimedefang-init
# Author      : Michael McLagan <Michael.McLagan@linux.org>
# Creation    : 30-Apr-2002 13:42
# Description : This is the init script for the RedHat RPM.  It lives 
#               It lives in /etc/rc.d/init.d as mimedefang and is called
#               by init during system startup.
#
#               Uses redhat/mimedefang-sysconfig (/etc/sysconfig/mimedefang)
#               to set various variables used as parameters to start the 
#               mimedefang and mimedefang-multiplexor daemons.
#
#               Based on init scripts provided by RedHat and others.
#
#               mimedefang should be started before sendmail and stopped
#               after sendmail.  The values in the chkconfig: line below
#               are based on those in the default (RedHat issued) sendmail
#               script as /etc/rc.d/init.d/sendmail (80 30)
# 
# Current Revision:
#
# $Source: /home/cvs/MIMEDefang/redhat/mimedefang-init,v $
# $Revision: 1.20 $
# $Author: dfs $
# $Date: 2002/12/03 17:44:47 $
#
# Revision History:
# 
# $Log: mimedefang-init,v $
# Revision 1.20  2002/12/03 17:44:47  dfs
# Do "ulimit -s" if lots of slaves.
#
# Revision 1.19  2002/09/18 15:47:49  dfs
# Updated init scripts for "-T" option.
#
# Revision 1.18  2002/06/13 14:58:28  dfs
# Updated changelog.
#
# Revision 1.17  2002/06/11 13:36:10  dfs
# Update scripts for recipient checks.
#
# Revision 1.16  2002/05/29 18:12:15  dfs
# Put pid files and sockets in /var/spool/MIMEDefang instead of /var/run
#
# Revision 1.15  2002/05/17 12:49:42  dfs
# Fixed typo.
#
# Revision 1.14  2002/05/17 12:48:34  dfs
# Added reload as a synonym for reread.
#
# Revision 1.13  2002/05/15 13:39:02  dfs
# Added README.NONROOT
#
# Revision 1.12  2002/05/15 13:08:43  dfs
# Made init script use -U for mimedefang too.
#
# Revision 1.11  2002/05/15 12:55:02  dfs
# Infrastructure for running multiplexor as non-root.
#
# Revision 1.10  2002/05/10 12:36:53  dfs
# Changed MX_SLAVE_RATE to MX_SLAVE_DELAY; added MX_MIN_SLAVE_DELAY.
#
# Revision 1.9  2002/05/08 16:55:17  dfs
# Updated redhat init scripts for subfilter.
#
# Revision 1.8  2002/05/06 18:36:08  dfs
# Added MX_SLAVE_RATE to init scripts.
#
# Revision 1.7  2002/05/03 14:58:55  dfs
# Fixed init script to look in /usr/bin and /usr/local/bin
#
# Revision 1.6  2002/05/03 14:24:24  dfs
# Merge packaging patches.
# Fixed typo.
# Made default value for -n 10.
#
##########################################################################

# These comments are used by chkconfig and supporting programs
#
# chkconfig: 2345 79 31
# description: mimedefang is a sendmail milter designed to perform virus \
#              scans on incoming mail messages.
# processname: mimedefang
# config: /etc/mail/mimedefang-filter
# pidfile: /var/spool/MIMEDefang/mimedefang.pid

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

# Source networking configuration.
. /etc/sysconfig/network

RETVAL=0
prog="mimedefang"

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Is the program executable?  We search in /usr/bin and /usr/local/bin
# so that both RPM and normal installation methods work fine.

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


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

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

start() {

    # Lets not run twice
    if [ -f /var/lock/subsys/$prog ]; then
	RETVAL=2
	return $RETVAL
    fi

    if [ "$MULTIPLEXOR" = "yes" ]; then
	echo -n "Starting $prog-multiplexor: "
	[ -e $MX_SOCKET ] && rm -f $MX_SOCKET
	daemon $PROGDIR/$prog-multiplexor -p /var/spool/MIMEDefang/$prog-multiplexor.pid \
	    $([ -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
	echo
    fi

    # Start daemon
    echo -n "Starting $prog: "
    [ -e $SOCKET ] && rm -f $SOCKET

    # NOTE: if you plan on running LOTS of slaves, you should limit
    # the stack size on Linux, or thread-creation will fail.
    if test "$MULTIPLEXOR" = "yes" -a "$MX_MAXIMUM" -gt 100 ; then
	ulimit -s 2048
    fi

    daemon $PROGDIR/$prog -P /var/spool/MIMEDefang/$prog.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") \
	$([ "$LOG_FILTER_TIME" = "yes" ] && echo "-T") \
	$([ "$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=$?
    echo

    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
    return $RETVAL
}

stop() {

    # If we're not running, there's nothing to do
    if [ ! -f /var/lock/subsys/$prog ]; then
	RETVAL=2
	return $RETVAL
    fi

    # Stop daemon
    echo -n "Shutting down $prog: "
    killproc $prog
    RETVAL=$?
    echo

    [ -e $SOCKET ] && rm -f $SOCKET
    [ -f /var/spool/MIMEDefang/$prog.pid ] && rm -f /var/spool/MIMEDefang/$prog.pid

    if [ "$MULTIPLEXOR" = "yes" ]; then
        # Stop daemon
	echo -n "Shutting down $prog-multiplexor: "
	killproc $prog-multiplexor
	echo

	[ -e $MX_SOCKET ] && rm -f $MX_SOCKET
	[ -f /var/spool/MIMEDefang/$prog-multiplexor.pid ] && rm -f /var/spool/MIMEDefang/$prog-multiplexor.pid
    fi

    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    stop
    start
    RETVAL=$?
    ;;
    condrestart)
    if [ -f /var/lock/subsys/$prog ]; then
	stop
	start
	RETVAL=$?
    fi
    ;;
    status)
    status $prog
    RETVAL=$?
    ;;
    reread|reload)
    if [ "$MULTIPLEXOR" != "yes" ]; then
	exit 0
    fi
    if [ -r /var/spool/MIMEDefang/$prog-multiplexor.pid ] ; then
	kill -INT `cat /var/spool/MIMEDefang/$prog-multiplexor.pid`
	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|condrestart|reread|reload|status}"
    exit 1
esac

exit $RETVAL
