#!/bin/sh
#
# /etc/init.d/sendmail
#
# sendmail      This shell script takes care of starting and stopping
#               sendmail.
#
# chkconfig: - 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/sendmail.pid

### BEGIN INIT INFO
# Provides:          sendmail
# Required-Start:    $network $named $time
# Required-Stop:     $network $named $time
# Default-Start:
# Default-Stop:
# Short-Description: Start sendmail at boot time
# Description:       Enable service provided by sendmail.
### END INIT INFO


# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

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

DAEMON=yes
QUEUE=1h
DATA=""

# Source config.
SourceIfNotEmpty /etc/sysconfig/sendmail

RETVAL=0
PIDFILE=/var/run/sendmail/sendmail.pid
LOCKFILE=/var/lock/subsys/sendmail

PIDFILEOLDQ=/var/run/sendmail/sendmailoldqueue.pid
LOCKFILEOLDQ=/var/lock/subsys/sendmailoldqueue

do_sendmail_init()
{
	pushd /etc/mail >/dev/null
	make db
	popd >/dev/null
}

start()
{
	msg_starting sendmail

	if [ "$DAEMON" = yes -a "$CLIENT" = yes ]; then
	    echo_failure "Mail submission program cannot be used as daemon"
	    RETVAL=1
	    echo
	    return $RETVAL
	fi

	[ -d "/var/run/sendmail" ] || {
	    mkdir "/var/run/sendmail"
	    chown root:smmsp "/var/run/sendmail"
	    chmod 770 "/var/run/sendmail"
	}

	do_sendmail_init
	
	[ -n "$QUEUE" ]     && DATA="-q$QUEUE";
	[ "$DAEMON" = yes ] && DATA="$DATA -bd";
	[ "$CLIENT" = yes ] && DATA="$DATA -Ac";
	
	start_daemon --no-announce --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- /usr/sbin/sendmail "$DATA"
	RETVAL=$?

	if [ $[`ls /var/spool/mqueue/qf* 2>/dev/null |wc -l` > 0] == 1 ]; then
	    msg_starting "sendmail old queue processing"
	    start_daemon --no-announce --pidfile "$PIDFILEOLDQ" --lockfile "$LOCKFILEOLDQ" --expect-user root -- /usr/sbin/sendmail -oQ/var/spool/mqueue -q30m -O PidFile=$PIDFILEOLDQ
	fi

	return $RETVAL
}	

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" -- sendmail
	RETVAL=$?

	if [ -f $PIDFILEOLDQ ]; then
	    msg_stopping "sendmail old queue processing"
	    stop_daemon --no-announce --pidfile "$PIDFILEOLDQ" --lockfile "$LOCKFILEOLDQ" -- sendmail
	fi

	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading sendmail
	stop_daemon -HUP sendmail
	RETVAL=$?
	return $RETVAL
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" -- sendmail
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
