#!/bin/bash
#
# sendmail      This shell script takes care of starting and stopping
#               sendmail.
#
# chkconfig: 2345 80 30
# description: Sendmail is a Mail Transport Agent, which is the program \
#              that moves mail from one machine to another.
# processname: sendmail
# config: /etc/mail/sendmail.cf
# pidfile: /var/run/sendmail.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Source sendmail configureation.
if [ -f /etc/sysconfig/sendmail ] ; then
	. /etc/sysconfig/sendmail
else
	DAEMON=no
	QUEUE=1h
fi

[ -z "$SMQUEUE" ] && SMQUEUE="$QUEUE"
[ -z "$SMQUEUE" ] && SMQUEUE=1h

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

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

RETVAL=0
prog="sendmail"

INPID=/var/run/${prog}.in.pid
OUTPID=/var/run/${prog}.out.pid

start() {
	# Start daemons.

	echo -n $"Starting $prog: "
	/usr/bin/newaliases > /dev/null 2>&1
	for i in virtusertable access domaintable ; do
	    if [ -f /etc/mail/$i ] ; then
		makemap hash /etc/mail/$i < /etc/mail/$i
	    fi
	done
        mailertables=
        if [ -f /etc/mail/mailertable.virtual_domains ]; then
            mailertables="/etc/mail/mailertable.virtual_domains"
        fi
        if [ -f /etc/mail/mailertable ]; then
            mailertables="$mailertables /etc/mail/mailertable"
        fi
        if [ -n "$mailertables" ]; then
            cat $mailertables | makemap hash /etc/mail/mailertable.db
        fi
        genericstables=
        if [ -f /etc/mail/genericstable.siteadmins ]; then
            genericstables="/etc/mail/genericstable.siteadmins"
        fi
        if [ -f /etc/mail/genericstable ]; then
            genericstables="$genericstables /etc/mail/genericstable"
        fi
        if [ -n "$genericstables" ]; then
            cat $genericstables | makemap hash /etc/mail/genericstable.db
        fi
	export LD_PRELOAD=libensimvwhbw.so
	export ENSIMVWH_BWSVCID=1
	export ENSIMVWH_sendmail=1
	/usr/lib/opcenter/sendmail/sendmail_add_maps /etc/mail/sendmail.cf
	daemon --check=`/bin/basename ${INPID%%.pid}` \
               /usr/sbin/sendmail $([ "$DAEMON" = yes ] && echo -bd) \
                                  $([ -n "$QUEUE" ] && echo -q$QUEUE) \
                                  -OPidFile=$INPID
	RETVAL=$?

	if [ $RETVAL -eq 0 -a -f /var/appliance/mailscanner ]; then
	    daemon --check=`/bin/basename ${OUTPID%%.pid}` \
                   /usr/sbin/sendmail $([ -n "QUEUE" ] && echo -q$QUEUE) \
                                      -ODeliveryMode=background \
	                              -OQueueDirectory=/home/virtual/FILESYSTEMTEMPLATE/services/sendmail/mqueue.scanned \
	                              -OPidFile=$OUTPID
            RETVAL=$?
        fi
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sendmail
        if ! test -f /var/run/sm-client.pid ; then
        echo -n $"Starting sm-client: "
        touch /var/run/sm-client.pid
        chown smmsp:smmsp /var/run/sm-client.pid
        daemon --check sm-client /usr/sbin/sendmail -L sm-msp-queue -Ac \
                        -q $SMQUEUE $SENDMAIL_OPTARG
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/sm-client
        fi

	return $RETVAL
}

startfast() {
	# Start daemons.

	echo -n $"Starting $prog: "
	/usr/bin/newaliases > /dev/null 2>&1
	for i in virtusertable access domaintable ; do
	    if [ -f /etc/mail/$i ] ; then
		makemap hash /etc/mail/$i < /etc/mail/$i
	    fi
	done
	export LD_PRELOAD=/lib/libensimvwhbw.so
	export ENSIMVWH_BWSVCID=1
	export ENSIMVWH_sendmail=1
	/usr/lib/opcenter/sendmail/sendmail_add_maps /etc/mail/sendmail.cf
	daemon --check=`/bin/basename ${INPID%%.pid}` \
               /usr/sbin/sendmail $([ "$DAEMON" = yes ] && echo -bd) \
                                  $([ -n "$QUEUE" ] && echo -q$QUEUE) \
                                  -OPidFile=$INPID
	RETVAL=$?
	if [ $RETVAL -eq 0 -a -f /var/appliance/mailscanner ]; then
	    daemon --check=`/bin/basename ${OUTPID%%.pid}` \
                   /usr/sbin/sendmail $([ -n "QUEUE" ] && echo -q$QUEUE) \
                                      -ODeliveryMode=background \
	                              -OQueueDirectory=/home/virtual/FILESYSTEMTEMPLATE/services/sendmail/mqueue.scanned \
	                              -OPidFile=$OUTPID
            RETVAL=$?
        fi
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sendmail

        if ! test -f /var/run/sm-client.pid ; then
        echo -n $"Starting sm-client: "
        touch /var/run/sm-client.pid
        chown smmsp:smmsp /var/run/sm-client.pid
        daemon --check sm-client /usr/sbin/sendmail -L sm-msp-queue -Ac \
                        -q $SMQUEUE $SENDMAIL_OPTARG
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/sm-client
        fi


	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n $"Shutting down $prog: "
	killproc ${INPID%%.pid}
	[ -f /var/appliance/mailscanner ] && killproc ${OUTPID%%.pid}
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sendmail
        if test -f /var/run/sm-client.pid ; then
                echo -n $"Shutting down sm-client: "
                killproc sm-client
                RETVAL=$?
                echo
                [ $RETVAL -eq 0 ] && rm -f /var/run/sm-client.pid
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sm-client
        fi

	return $RETVAL
}

oldstop() {
	# Stop daemons.
	echo -n $"Shutting down $prog: "
	killproc ${INPID%%.pid}
#	[ -f /var/appliance/mailscanner ] && killproc ${OUTPID%%.pid}
#	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sendmail
        if test -f /var/run/sm-client.pid ; then
                echo -n $"Shutting down sm-client: "
                killproc sm-client
                RETVAL=$?
                echo
                [ $RETVAL -eq 0 ] && rm -f /var/run/sm-client.pid
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sm-client
        fi

	return $RETVAL
}

# See how we were called. Note that we provide oldstop and oldstatus
# so that we can cleanly test and shut down sendmails on systems where
# our new MailScanner-friendly sendmail setup hasn't been installed yet.
case "$1" in
  start)
	start
	;;
  startfast)
	startfast
	;;
  stop)
	stop
	;;
  oldstop)
        oldstop
        ;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/sendmail ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	# Changing the way status is called.
	# Instead of the standard way of calling the "functions" 
	# (which does not work for sendmail.in and sendmail.out pid parameters), what is done instead is
	# we are parsing the pid file for getting pid and comparing it with the one that is running.
        if [ ! -f /var/appliance/mailscanner ]; then 
		if [ -f /var/run/sendmail.in.pid ] && pidofin=`cat /var/run/sendmail.in.pid`; then
  		   b=`echo $pidofin | cut -f1 -d' '`
		   pidofsend=`pidof sendmail`
		   for i in $pidofsend
		       do
		       if [ $i == $b ];then
		  	  echo "sendmail.in is running"
			  exit 0
		       fi
                       done
		fi
	        # See if /var/lock/subsys/${base} exists
        	if [ -f /var/lock/subsys/${base} ]; then
                       echo $"${base} dead but subsys locked"
		       exit 0
        	fi
		echo "sendmail is stopped"
        else
                [ -f /var/run/sendmail.in.pid ] && pidofin=`cat /var/run/sendmail.in.pid`
                [ -f /var/run/sendmail.out.pid ] && pidofout=`cat /var/run/sendmail.out.pid`

                pidofsend=`pidof sendmail`
                pidin=`echo $pidofin | cut -f1 -d' '`
		if [ ! -z $pidin ]; then
		   for i in $pidofsend
                       do
                         if [ $i == $pidin ];then
                           flag=1
                         fi
                       done
                fi
                pidout=`echo $pidofout | cut -f1 -d' '` 
		if [ ! -z $pidout ]; then
    	           for i in $pidofsend
                       do
                         if [ $i == $pidout ] && [ $flag -eq '1' ]; then
                           echo "sendmail.in and sendmail.out are running..."
			   flag=0
			   exit 0
                         fi
                       done
		fi
	        # See if /var/lock/subsys/${base} exists
        	if [ -f /var/lock/subsys/${base} ]; then
                       echo $"${base} dead but subsys locked"
                       exit 0
        	fi
		echo "sendmail is stopped"
        fi
	RETVAL=$?
	;;
  oldstatus)
        status sendmail
        RETVAL=$?
        ;;
  restartfast)
	stop
	startfast
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|condrestart|status|startfast|restartfast}"
	exit 1
esac

exit $RETVAL
