#!/bin/bash
#
# pysrs	This shell script takes care of starting and stopping pysrs.
#
# chkconfig: 2345 80 30
# description: Milter is a process that filters messages sent through sendmail.
# processname: pysrs
# config: /etc/mail/pysrs.cfg
# pidfile: /var/run/milter/pysrs.pid

python="python"

pidof() {
	set - ""
	if set - `ps -e -o pid,cmd | grep "${python} pysrs.py"` &&
	  [ "$2" != "grep" ]; then
	  echo $1
	  return 0
	fi
	return 1
}

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

test -x /usr/lib/pymilter/pysrs.py && test -x /usr/sbin/daemonize || exit 0

RETVAL=0
prog="pysrs"
datadir="/var/log/milter"
logdir="/var/log/milter"
piddir="/var/run/milter"
libdir="/usr/lib/pymilter"

start() {
	# Start daemon.
	if test -s ${datadir}/${prog}.py; then
	  workdir="${datadir}" # use data dir if it exists for debugging
	elif test -s ${logdir}/${prog}.py; then
	  workdir="${logdir}" # use log dir if it exists for debugging
	else
	  workdir="${libdir}"
	fi
	echo -n "Starting $prog: "
	daemon --check pysrs --pidfile "${piddir}/${prog}.pid" --user mail \
	daemonize -a -e "${logdir}/${prog}.log" -o "${logdir}/${prog}.log" \
		-c "${workdir}" -p "${piddir}/${prog}.pid" \
		/usr/bin/${python} ${prog}.py 
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pysrs
	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n "Shutting down $prog: "
	killproc -p "${piddir}/${prog}.pid" pysrs
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/pysrs
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/pysrs ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	status pysrs
	RETVAL=$?
	;;
  *)
	echo "Usage: $0 {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL
