#!/bin/sh
#
# zmailer      Zmailer Mail Transfer Agent
#
# 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
# processname: scheduler
# processname: smtpserver
#

# 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 0
fi

# Source networking configuration.
if  [ -f  /etc/sysconfig/network ] ; then
	. /etc/sysconfig/network
else
	exit 0
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 0
fi

# Check zmailer script and work directories
[ -f $MAILBIN/zmailer ] || exit 0
[ -d $LOGDIR ] || exit 0
[ -d $MAILBOX ] || exit 0
[ -d $MAILSHARE ] || exit 0
[ -d $MAILVAR ] || exit 0
[ -d $POSTOFFICE ] || exit 0

# Here we go
RETVAL=0

start() {
	# Start daemons.
	
	# Is it frozen? then exit.
	if [ -f $POSTOFFICE/.freeze ]; then
		echo "Sorry, ZMailer is frozen, won't start anything until thawed !"
		echo "* CHECK THAT THE FREEZE CONDITION ISN'T DUE TO E.G. MAINTENANCE *"
		exit 0
	fi

	if ( status scheduler || status router || status smtpserver ) | 
	   grep -v stop 2>/dev/null 1>&2 ; 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 && success || failure
		fi
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/zmailer
	echo
	return $RETVAL
	fi
}

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

	fi
}
restart() {
	stop
	start
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
	restart
	;;
  reload)
	stop
	start
	;;
  status)
	status router
	status scheduler
	status smtpserver
        ;;
  logsync)
	$MAILBIN/zmailer logsync
	;;
  condrestart)
	[ -f /var/lock/subsys/zmailer ] && restart || :
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|status|logsync|condrestart}"
	exit 1
esac

exit $?


syntax highlighted by Code2HTML, v. 0.9.1