#!/bin/sh
#
# mailgraph	This shell script takes care of starting and stopping
#		mailgraph service.
#
# chkconfig:	345 55 45
# description:	mailgraph watch postfix logfiles and generate www statistics

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

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/mailgraph ]; then
		msg_starting "mailgraph"
		busy
		/usr/bin/mailgraph.pl >/dev/null 2>&1 &
		sleep 1
		ps -C mailgraph >/dev/null 2>&1
		RETVAL=$?
		CPID=$!
		if [ $RETVAL -eq 0 ]; then
			touch /var/lock/subsys/mailgraph
			echo $CPID >/var/run/mailgraph.pid
			deltext; ok;
		else
			deltext; fail;
		fi
	else
		msg_already_running "mailgraph"
		exit 1
	fi

        ;;
  stop)
        if [ -f /var/lock/subsys/mailgraph ]; then
        	msg_stopping "mailgraph"
		#busy
        	killproc mailgraph
		#deltext; ok;
        	rm -f /var/lock/subsys/mailgraph >/dev/null 2>&1
        	rm -f /var/run/mailgraph.pid >/dev/null 2>&1
        else
	        msg_not_running "mailgraph"
                exit 1
        fi
															
        ;;
  status)
  	status mailgraph
	exit $?
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        msg_usage "$0 {start|stop|status|restart}"
        exit 1
esac

exit $RETVAL
