#! /sbin/sh
#Tag 0x00000f00

# Start sendmail demon after checking that things are healthy.
# $Revision: 1.38 $


if /sbin/chkconfig verbose ; then	# For a verbose startup and shutdown
    ECHO=echo
    VERBOSE=-v
else				# For a quiet startup and shutdown
    ECHO=:
    VERBOSE=
fi

CONFIG=/etc/config

case "$1" in
'start')
    $ECHO "Mailer daemons: \c"

    # Choose sendmail options.  The file might contain something like -om
    # to turn on "metoo"
    if test -s $CONFIG/sendmail.options; then
	OPTS=`cat $CONFIG/sendmail.options`
    fi


    # Move to post office directory
    if test ! -d /var/mail -a ! -l /var/mail ; then
	mkdir /var/mail
    fi
    cd /var/mail
    if test $? = 0; then
    	if test ! -d ':saved' ; then
		mkdir ':saved'
    	fi
    	chown root . ':saved'
    	find . -local -exec chgrp mail {} \;
    	find . -local -exec chmod g+w {} \;
    fi

    # Move to spool directory.  Make sure the directory exists, and is of the
    # proper permissions
    if test ! -d /var/spool; then
	mkdir /var/spool
    fi
    if test ! -d /var/spool/mqueue; then
	mkdir /var/spool/mqueue
    fi
    cd /var/spool/mqueue
    chmod 775 .
    chown root .
    chgrp mail .

    # Make sure that /usr/lib/sendmail is executable and setuid root
    if test ! -x /usr/lib/sendmail; then
	echo "mail: no /usr/lib/sendmail, can't use mail"
	exit 255
    fi

    # check the alias data base
    if test ! -r /etc/aliases; then
	echo "mail: /etc/aliases is missing, can't use mail"
	exit 255
    fi
    if test ! -r /etc/aliases.dir -o ! -r /etc/aliases.pag; then
	touch /etc/aliases.dir /etc/aliases.pag
	sleep 1
	touch /etc/aliases
    fi

    # Make sure that /bin/mail is correct
    if test ! -x /bin/mail; then
	echo "mail: no /bin/mail, can't use mail"
	exit 255
    fi

    if test ! -c /dev/log; then
	echo "mail: syslogd is not running, /dev/log does not exist"
	exit 255
    fi

    # kill any old deamons
    /sbin/killall 15 sendmail

    # Clean up any old lock files
    rm -rf [tnx]f*

    # rebuild aliases just in case...
    newaliases > /dev/null &

    # ignore SIGHUP from init, so the backgrounded demons will not be killed
    trap "" 1

    # start the daemon.
    /usr/bsd/logger -t sendmail starting
    if test -f /etc/sendmail.fc; then
	rm /etc/sendmail.fc
	( /usr/lib/sendmail -bz $OPTS; \
	  /usr/lib/sendmail -q60 $OPTS ) | logger -t sendmail &
    else
	( /usr/lib/sendmail -q60 $OPTS ) | logger -t sendmail &
    fi
    $ECHO "sendmail."
    ;;

'stop')
    /sbin/killall 15 sendmail
    ;;

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