#!/bin/sh
#
# mailstripper  Startup script for MailStripper Pro for non-RedHat systems.
#
# chkconfig: 2345 81 29
# description: MailStripper Pro is an anti-spam daemon which is independent\
# of your MTA, and can be on a separate machine to it.

# Uncomment the next line if you have trouble with some high-bit character sets
# unset LANG

# Uncomment the next two lines only if requested to do so by Eridani.
#MSTRIPDEBUG=/var/log/mailstripper.debug
#export MSTRIPDEBUG

PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

# Check what we're on.
MACHINE=`uname`

[ -f /usr/sbin/mailstripper ] || exit 0

if [ ! -f /etc/mailstripper/config ]; then
  echo "MailStripper: No configuration file found."
  echo "Please run mstripconf or xmstripconf and save a configuration file."
  exit 1
fi

case "$1" in
  start)
		if [ $MACHINE = "SunOS" ]; then
		  echo 'Starting MailStripper: \c'
		else
		  echo -n 'Starting MailStripper: '
		fi
		rm -f /var/spool/mailstripper/proc.*
		if [ "X$MSTRIPDEBUG" = "X" ]; then
		  rm -f /etc/mailstripper/debug
		  mailstripper bground
		else
		  touch /etc/mailstripper/debug
		  if [ $MACHINE = "SunOS" ]; then
		    echo '(Debug) \c'
		  else
		    echo -n '(Debug) '
		  fi
		  mailstripper bground $MSTRIPDEBUG
		fi
		echo "Started."
		;;
	
  stop)
		NUMPIDS=`ps -ef | grep mailstripper | grep -v grep | grep -v stop | grep -v restart | grep -v tail | sed 's/^[ ]*//' | tr -s ' ' | cut -f 2 -d ' '`
		X=`echo "${NUMPIDS}" | wc -w`
		if [ $X -gt 0 ]; then
		  for X in $NUMPIDS; do 
		    kill $X > /dev/null 2>&1
		  done
		  echo "Shutting down MailStripper: Stopped."
		fi
		;;
	
  status)
		if [ `ps -ax | grep mailstripper | grep -v grep | grep -v status | wc -l` -ge 1 ]; then
		  echo "MailStripper is running."
		else
		  echo "MailStripper is not running."
		fi
		;;

  restart)
		$0 stop
		sleep 1
		$0 start
		;;
	
  *)
		echo "Usage: mailstripper {start|stop|status|restart}"
		exit 1

esac

exit 0
