#!/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/sendmail.cf
# pidfile: /var/run/sendmail.pid

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

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network && [ "$NETWORKING" != no ] || exit

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

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

RETVAL=0
LOCKFILE=/var/lock/subsys/sendmail

start()
{
	echo -n "Starting sendmail: "
	/usr/bin/newaliases > /dev/null 2>&1
	for i in virtusertable access domaintable mailertable ; do
	    if [ -f /etc/mail/$i ] ; then
		makemap hash /etc/mail/$i < /etc/mail/$i
	    fi
	done

	# start daemons
	daemon /usr/sbin/sendmail $([ "$DAEMON" = yes ] && echo -bd) \
				  $([ "$CLIENT" = yes ] && echo -Ac) \
                                  $([ -n "$QUEUE" ] && echo -q$QUEUE)

	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	echo
}	

stop()
{
	echo -n "Shutting down sendmail: "
	# stop daemons
	killproc sendmail
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	[ $RETVAL -eq 0 ] && rm -f /var/run/sendmail/sendmail.pid
	echo
}

restart()
{
	stop
	start
}

reload()
{
	# cause the service configuration to be reread, either with kill -HUP:
	echo -n "Reloading sendmail: "
	killproc sendmail -HUP
	RETVAL=$?
	echo
	# or by simple restarting the daemons in a manner similar to restart above.
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  reload)
	reload
	;;
  restart)
	restart
	;;
  condstop)
	# Stop the servce if it is already running
	if [ -e "$LOCKFILE" ]; then
	 stop
	fi
	;;
  condrestart)
	# Restart the servce if it is already running
	if [ -e "$LOCKFILE" ]; then
 	 restart
	fi
	;;
  status)
	# report the status of the daemons in free-form format
	status sendmail
	RETVAL=$?
	;;
  *)
	echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
	RETVAL=1
esac

exit $RETVAL

