#!/sbin/sh 
#
# sendmail $Revision: 1.3.212.9 $ $Date: 96/04/16 20:13:39 $
#
#           Copyright (C) 1993, 1994 Hewlett-Packard Company
#
# NOTE:    This script is not configurable!  Any changes made to this
#          script will be overwritten when you upgrade to the next
#          release of HP-UX.
#

PATH=/sbin:/usr/sbin:/usr/bin
export PATH

rval=0
set_return() {
	x=$?
	if [ $x -ne 0 ]; then
		rval=$x
	fi
}

case $1 in
start_msg)
	echo "Starting mail daemon"
	;;

stop_msg)
	echo "Stopping mail daemon"
	;;

'start')
    if [ -f /etc/rc.config ] ; then
		. /etc/rc.config
    else
		echo "ERROR: /etc/rc.config defaults file MISSING"
    fi

    #
    # SENDMAIL_SERVER is of higher precedence then SENDMAIL_SERVER_NAME
    # (SENDMAIL_SERVER_NAME implies a sendmail client). If both variables
    # are set, the latter will be ignored.
    #
    # If neither variable is set, then the default is to leave
    # /usr/sbin/sendmail as executable, and allow mail agents to use
    # the default configuration and aliases.
    #
    if [ $SENDMAIL_SERVER -eq 1 ] ; then
	pidfile="/etc/mail/sendmail.pid"
	if [ -f $pidfile ]; then
		test "$pidfile" && exec 0< $pidfile
		read -r PID
		sendserv=`ps -ef | grep $PID | grep -v grep | wc -l`
		if [ $sendserv -ne 0 ]; then
			echo "sendmail has already been started"
			set_return
			exit $val
		fi
	fi
	#
	# This host will run a sendmail daemon and will be either
	# a sendmail server for other hosts or it will be a standalone
	# sendmail host.
	# 
	# Make a note with syslogd that the sendmail daemon is being started
	#
	/usr/bin/logger -t /sbin/init.d/sendmail\[$$\] -p mail.notice "#### rebooted ####"
	#
	# Start the sendmail daemon for both a standalone or server sendmail
	# situation.
	#
	rm -f /var/spool/mqueue/xf*
	newaliases
	HOSTNAME=`hostname`
	if [ `grep $HOSTNAME /etc/mail/sendmail.cw | wc -l` -eq 0 ] ; then
		echo "WARNING: /etc/mail/sendmail.cw not configured, configuring..."
		echo "Adding localhost to sendmail.cw"
                echo "localhost" >> /etc/mail/sendmail.cw
		echo "Adding " $HOSTNAME " to sendmail.cw"
                echo $HOSTNAME >> /etc/mail/sendmail.cw	
		resfile="/etc/resolv.conf"
		if [ -f $resfile ]; then
			domname=`grep -i domain $resfile | awk '{if ($2 != "") printf "%s ", $2}'`
			echo "Adding " $HOSTNAME"."$domname " to sendmail.cw"
			echo $HOSTNAME.$domname >> /etc/mail/sendmail.cw
			echo "Adding " $domname " to sendmail.cw"
			echo $domname >> /etc/mail/sendmail.cw
		fi
	else
		/usr/sbin/sendmail -bd -q30m && echo "sendmail"
	fi
	set_return
    elif [ "X$SENDMAIL_SERVER_NAME" != "X" ] ; then
	    #
	    # This host is a sendmail client and will not run a sendmail daemon.
	    # The sendmail configuration file will be checked to see if
            # the DH and DM macros are properly defined to SENDMAIL_SERVER_NAME
            # and if they are not, the .cf file will be rewritten to do so.
	    # If they are already defined, leave them alone.
	    #
            mv /etc/mail/sendmail.cf /etc/mail/#sendmail.cf
            sed -e s/^DH$/DH$SENDMAIL_SERVER_NAME.\$m/ \
		-e s/^DM$/DM$SENDMAIL_SERVER_NAME.\$m/ \
		/etc/mail/#sendmail.cf \
		> /etc/mail/sendmail.cf
	    rm /etc/mail/#sendmail.cf
	    set_return
    else
	rval=2
    fi
    ;;

'stop')
    if [ -f /etc/rc.config ] ; then
		. /etc/rc.config
    else
		echo "ERROR: /etc/rc.config defaults file MISSING"
    fi

    /usr/sbin/killsm
    if [ $? -eq 0 ]; then
	echo "sendmail stopped"
    else
	#
	# A sendmail daemon is not expected on a sendmail client, so
	# print an error only if this machine looks like it is a mail
	# server or standalone machine. 
	#
	if [ $SENDMAIL_SERVER -eq 1 ]; then
	    rval=1
	    echo "ERROR: Unable to stop sendmail"
	fi
    fi
    ;;

*)
    echo "usage: $0 {start|stop}"
    rval=1
    ;;
esac

exit $rval
