#!/bin/sh
#
# zmailer       This shell script takes care of starting and stopping
#               ZMailer.
#
# chkconfig: 2345 80 30
# description: ZMailer is a Mail Transport Agent, which is the program \
#              that moves mail from one machine to another.
# processname: router scheduler smtpserver
# config: /etc/zmailer/zmailer.conf
# pidfile: /var/spool/postoffice/.pid.scheduler .pid.smtpserver .pid.router

# Source function library.
if [ -f /etc/rc.d/init.d/functions ] ; then
	. /etc/rc.d/init.d/functions
		elif [ -f /etc/init.d/functions ] ; then
			. /etc/init.d/functions
			else
				exit 1
fi

# Source networking configuration.
if  [ -f  /etc/sysconfig/network ] ; then
	. /etc/sysconfig/network
	else
		exit 1
fi

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Source zmailer configuration.
if [ -f /etc/zmailer/zmailer.conf ] ; then
	. /etc/zmailer/zmailer.conf
else 
	exit 1
fi

[ -f $MAILBIN/zmailer ] || exit 1

RETVAL=0

start() {
	# Start daemons.

	if ( status scheduler || status router || status smtpserver ) | grep -v stop > /dev/null ; then
		echo "Zmailer is running yet"
	else
		if [ "${SMTPSERVER}" ]; then
			$MAILBIN/zmailer smtp
		elif [ ${MAILSERVER-NONE} = NONE -a -x $MAILBIN/zmailer ]; then
			$MAILBIN/zmailer bootclean
			$MAILBIN/zmailer
		fi
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/zmailer
	return $RETVAL

	fi
}

stop() {
	# Stop daemons.

	if ( status scheduler || status router || status smtpserver ) | grep stop > /dev/null ; then
		echo "Zmailer is not running"
	else
		if [ ${MAILSERVER-NONE} = NONE -a -x $MAILBIN/zmailer ]; then
			$MAILBIN/zmailer kill
		fi
	RETVAL=$?
        echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zmailer $POSTOFFICE/.pid.*
	return $RETVAL

	fi
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  status)
	status router
	status scheduler
	status smtpserver
	RETVAL=$?
        ;;
  logsync)
        $MAILBIN/zmailer logsync
        ;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|status|logsync}"
	exit 1
	;;
esac

exit $RETVAL
