#!/bin/sh
# File:       mail-filter
# Version:    1.14
# Purpose:    Handy script to stop and start the filter
# 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')


# Function to check that WORK_DIR and SOCKET have been read in from 
# the configuration file.
#
check_conf () {
   if [ -z "$WORK_DIR" ]
   then
      echo "$BN0: Working directory not specified."
      CONF_ERROR=1
   fi
   if [ -z "$SOCKET" ]
   then
      echo "$BN0: Socket not specified."
      CONF_ERROR=1
   fi
   if [ "$CONF_ERROR" = "1" ]
   then
      echo "Please check ${CONF_FILE}."
      exit 1
   fi
}
 

# Function to check that WORK_DIR exists and contains a 'rejects' directory.
#
check_directories () {
   if [ ! -d $WORK_DIR ]
   then
      echo "$BN0: Working directory not found."
      echo "  (${WORK_DIR})"
      exit 1
   elif [ ! -w $WORK_DIR -o ! -x $WORK_DIR ]
   then
      echo "$BN0: Working directory does not have 'rwx' attributes."
      echo "  (${WORK_DIR})"
      exit 1
   elif [ ! -d ${WORK_DIR}/rejects ]
   then
      echo "$BN0: Rejects directory not found."
      echo "  (${WORK_DIR}/rejects)"
      exit 1
   elif [ ! -w ${WORK_DIR}/rejects -o ! -x ${WORK_DIR}/rejects ]
   then
      echo "$BN0: Rejects directory does not have 'rwx' attributes."
      echo "  (${WORK_DIR}/rejects)"
      exit 1
   fi
}


CONF_FILE="/usr/local/etc/mail-filter/mail-filter.conf"
BIN="/usr/local/bin"
BN0=`basename $0`

# Prefix BIN to the PATH.  Allows path to be omitted from commands,
# and makes subsequent output from 'ps' more useful on most platforms.

OLD_PATH=$PATH
PATH=${BIN}:$PATH
export PATH

FILTER="rays-filter"
MAIL_FILTER_START="mf-start"
MAIL_FILTER_STOP="mf-stop"
MAIL_FILTER_RESET="mf-reset"

# Get the WORK_DIR and SOCKET variables from mail-filter.conf
#
. $CONF_FILE

# Check that the variables have been read, exit if not. 
#
check_conf

export WORK_DIR

case $1 in
'start')
         check_directories
         nohup $MAIL_FILTER_START $FILTER $SOCKET \
            >> $WORK_DIR/mail-filter.err &
         ;;
'stop')
         $MAIL_FILTER_STOP $FILTER $SOCKET
         ;;
'reset')
         $MAIL_FILTER_RESET $FILTER 
         ;;
*)
         echo " "
         echo "Usage:"
         echo "        $0 { start | stop  | reset }"
         ;;
esac
exit 0

