#!/bin/sh
# File:       mf-stop
# Version:    1.14
# Purpose:    Stops the mail filter 
#             (Called from the 'mail-filter' script)
# Written by: R.Butler <butlerra@sbu.ac.uk>
# Date:       16-Jun-2000
# Revised:    08-Mar-2001
#
# Copyright (C) 2001 South Bank University, London
# (Please see the full copyright notice in 'copyright.txt')

# Note: This script works on the basis that if there are any msg.* files 
#       in the 'rejects' directory, then message processing is in progress.
#       It therefore waits up to MAX_WAIT seconds for all such files to
#       disappear before killing the filter process.  If there are still
#       any msg.* files in existence after MAX_WAIT seconds, it kills the
#       filter process anyway.

WAIT=0
MAX_WAIT=15

FILTER=$1
SOCKET=$2
BN_FILTER=`basename $FILTER`
WORKING=1

# Check that the WORK_DIR variable has been exported
if [ -z "$WORK_DIR" ]
then
   echo " "
   echo "$0: Variable WORK_DIR is not defined."
   echo "To stop the mail filter, please use the command:"
   echo "   mail-filter stop"
   echo " "
   exit 3
fi
REJECTS="$WORK_DIR/rejects/msg.*"
LOGFILE="$WORK_DIR/mail-filter.log"

# Stop the parent 'mf-start' process first, to prevent it 
# from re-starting the filter.

# First look for a 'mf-start' process with filter name in
# command-line.

FILTER_START_PID=`ps -ef | grep 'mf-start' | grep $FILTER \
                 | grep -v 'grep' \
                 | awk '{print $2}'`

# Failing that (e.g. on Linux, process is swapped out to disk so command-
# line parameters are not shown) look for any 'mf-start' process.

if [ -z "$FILTER_START_PID" ]
then
   FILTER_START_PID=`ps -ef | grep 'mf-start' \
                    | grep -v 'grep' \
                    | awk '{print $2}'`
fi

# If found a 'mf-start' process, kill it.

if [ -n "$FILTER_START_PID" ]
then
   kill -KILL $FILTER_START_PID
else
   echo "$0: Warning: cannot find process: mf-start."
fi

# Now stop the filter itself, when no messages are in progress or
# after MAX_WAIT seconds, whichever is quickest.

FILTER_PID=`ps -ef | grep $FILTER \
           | grep -v 'mf-start' \
           | grep -v $0 | grep -v 'grep' \
           | awk '{print $2}'`
if [ -n "$FILTER_PID" ]
then
   while [ $WORKING -eq 1 ]
   do
      if [ -f $REJECTS ]
      then
         if [ $WAIT -lt $MAX_WAIT ]
         then
            WAIT=`expr $WAIT + 1` 
            sleep 1
         else
            DATE=`date +"%d-%b-%Y %T"`
            echo "$DATE : Stopping $BN_FILTER; WAIT = $WAIT, messages dropped" \
               >> $LOGFILE
            WORKING=0
         fi
      else
         DATE=`date +"%d-%b-%Y %T"`
         echo "$DATE : Stopping $BN_FILTER; WAIT = $WAIT" >> $LOGFILE
         WORKING=0
      fi
   done
   kill -KILL $FILTER_PID
fi

# Delete the socket if it is a 'local' file

SOCKET_TYPE=`echo $SOCKET | awk '{split($1,part,":"); print part[1]}'`
if [ "$SOCKET_TYPE" = "local" ]
then
   FILE=`echo $SOCKET | awk '{split($1,part,":"); print part[2]}'`
   rm -f $FILE
fi

exit 

