#!/bin/sh
#
#    Copyright (c) 1996-2000 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Initialization script to start/stop sendmail
#
# 

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

SENDMAIL=${USRSBINDIR}/sendmail
DOMAINNAME=${BINDIR}/domainname
DOMFILE=${CONFDIR}/domain
MAILCONFDIR=${CONFDIR}/mail
MAILCONF=${MAILCONFDIR}/sendmail.cf
MAILSUBMITCONF=${MAILCONFDIR}/submit.cf
MAILSUBMITCONFSAVED=${MAILCONFDIR}/.submit.cf.orig

export PATH

case "$1" in

'start')
    skipsendmail=0
    ${KILLALL} sendmail
    #set the domain from DOMFILE
    if [ -f $DOMFILE ]; then
	${DOMAINNAME} `/bin/cat $DOMFILE` 
	if [ $? -ne 0 ]; then
	   echo "Could not set domain name from $DOMFILE"
	fi
    fi

    #put the current domain name in the sendmail configuration file
    DOMAIN=`${DOMAINNAME}`

    # Note that we will NOT invoke the sendmail daemon here because
    # e-mail only needs to go out, not in. Consequently, because
    # we do not want to receive any e-mails on the system so for sending
    # sendmail daemon is not required. Only sendmail binary will be
    # used send out emails in synchronous mode.

    if [ "$DOMAIN" = "" ]; then
	$ECHO "Domain name is null. E-mail alerts will not be available."
	skipsendmail=1
    fi

    if [ $skipsendmail -eq 0 ]; then
	/bin/sed -e "s/^Dj\$w.*/Dj\$w\.${DOMAIN}/g" < ${MAILCONF} > ${MAILCONF}.tmp
	/bin/mv ${MAILCONF}.tmp ${MAILCONF}
	/bin/chmod 444 ${MAILCONF}

	if [ ! -f /tmp/spool/mqueue ]; then
	    /bin/mkdir -p /tmp/spool/mqueue
	    /bin/chmod 755 /tmp/spool/mqueue
	fi
	if [ -f "${MAILSUBMITCONF}" ]; then
		if [ ! -f "${MAILSUBMITCONFSAVED}" ]; then
			/bin/cp ${MAILSUBMITCONF} ${MAILSUBMITCONFSAVED}
		fi
		/bin/cp ${MAILCONF} ${MAILSUBMITCONF}
		sed 's/^O QueueDirectory.*$/O QueueDirectory=\/tmp\/spool\/clientmqueue\
O RunAsUser=smmsp\
O TrustedUser=smmsp\
		/g' ${MAILSUBMITCONF} > ${MAILSUBMITCONF}.tmp
		/bin/cp ${MAILSUBMITCONF}.tmp ${MAILSUBMITCONF}
		/bin/rm -f ${MAILSUBMITCONF}.tmp
		/bin/sed -e "s/^#Dj$w.*/Dj\$w\.${DOMAIN}/g" < ${MAILSUBMITCONF} > ${MAILSUBMITCONF}.tmp
		/bin/sed -e "s/^Dj$w.*/Dj\$w\.${DOMAIN}/g" < ${MAILSUBMITCONF}.tmp > ${MAILSUBMITCONF}
		/bin/rm -rf /tmp/spool
		if [ ! -d "/var/spool/mqueue" ]; then
			/bin/mkdir -p /var/spool/mqueue
		fi
		/bin/chmod 755 /var/spool/mqueue
		if [ ! -d "/var/spool/clientmqueue" ]; then
			/bin/mkdir -p /var/spool/clientmqueue
		fi
		if [ -d "/var/spool/clientmqueue" ]; then
			/bin/chmod 755 /var/spool/clientmqueue
			chown smmsp:smmsp /var/spool/clientmqueue
		fi
		if [ ! -f /tmp/spool ]; then
		    ln -s /var/spool /tmp/spool
		fi
	fi
	fi
    ;;

'stop')
    ${KILLALL} sendmail
    ;;

*)
    echo "usage: $0 {start |stop}"
    ;;

esac

