#!/bin/bash
#
# sendmail      This shell script takes care of starting and stopping
#               sendmail.
#
# chkconfig: 2345 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.pid
# securlevel: 30

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

# Run some action. Log its output.
action() {
  STRING=$1
  echo -n "$STRING "
  shift
  $*
  rc=$?
  echo
  return $rc
}

# Check if $pid (could be plural) are running
checkpid() {
	while [ "$1" ]; do
	   [ -d /proc/$1 ] && return 0
	   shift
	done
	return 1
}

# Log that something succeeded
success() {
  echo -n "$1"
  return 0

#  if [ -z "$IN_INITLOG" ]; then
#     initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
#  else
#     # silly hack to avoid EPIPE killing rc.sysinit
#     trap "" SIGPIPE
#     echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" >&21
#     trap - SIGPIPE
#  fi
#  [ "$BOOTUP" != "verbose" ] && echo_success
#  return 0
}

# Log that something failed
failure() {
  rc=$?
  echo -n "$1"
  return $rc

#  if [ -z "$IN_INITLOG" ]; then
#     initlog $INITLOG_ARGS -n $0 -s "$1" -e 2
#  else
#     trap "" SIGPIPE
#     echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 2" >&21
#     trap - SIGPIPE
#  fi
#  [ "$BOOTUP" != "verbose" ] && echo_failure
#  return $rc
}

# Source networking configuration.
. /etc/sysconfig/network
[ ${NETWORKING} = "no" ] && exit 0

# Setup default values
DAEMON=yes
MTA=yes
MSP=yes
QUEUE=1h

# Source sendmail configureation.
if [ -f /etc/sysconfig/sendmail ] ; then
	. /etc/sysconfig/sendmail
fi

# Check that networking is up.

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

RETVAL=0

start() {
	# Start daemons.

	echo -n "Setup sendmail: "

	if [ -f /etc/mail/submit.cf.nodaemon ] && [ ! -f /etc/mail/submit.cf ]; then
	    mv -f /etc/mail/submit.cf.nodaemon /etc/mail/submit.cf
	fi
	if [ -f /etc/mail/submit.cf ] ; then
	    chmod 644 /etc/mail/submit.cf
	fi
	chown root.smmsp /usr/sbin/sendmail
	chmod 2555 /usr/sbin/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
	echo ""

	args="$([ -n "$QUEUE" ] && echo -q$QUEUE)"

	RETVAL=0
	## Startup MTA agent
	if [ "${MTA}" = "yes" -o "${MTA}" = "YES" ] ; then
		echo -n "Starting sendmail MTA: "
		daemon /usr/sbin/sendmail -L sm-mta \
			$([ "$DAEMON" = "yes" ] && echo -bd) \
			${args}
		RETVAL=$?
		echo ""
	fi
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sendmail
	## Startup MSP agent
	if [ "$RETVAL" = "0" -a "${MSP}" = "yes" -o "${MSP}" = "YES" ] ; then
		action "Starting sendmail MSP: " \
			/usr/sbin/sendmail -L sm-msp-queue -Ac ${args}
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sm-client
	fi
	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n "Shutting down sendmail MTA: "
	killproc sendmail
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sendmail

	if test -f /var/lock/subsys/sm-client -a \
		-f /var/spool/clientmqueue/sm-client.pid
	then
		echo -n "Shutting down sendmail MSP: "
		pid=$(head -1 /var/spool/clientmqueue/sm-client.pid)
		if ! checkpid ${pid} ; then
			failure "sm-client shutdown"
		else 
			kill -TERM ${pid}
			sleep 1
			if ! checkpid ${pid} ; then 
				success "sm-client shutdown"
				RETVAL=0
				rm -f /var/lock/subsys/sm-client
				rm -f /var/spool/clientmqueue/sm-client.pid
			else
				failure "sm-client shutdown"
				RETVAL=1
			fi
		fi
		echo ""
	fi

	if [ -f /etc/mail/submit.cf ]; then
	    mv -f /etc/mail/submit.cf /etc/mail/submit.cf.nodaemon
	fi
	chown root.mail /usr/sbin/sendmail
	chmod 6555 /usr/sbin/sendmail
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/sendmail ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	status sendmail
	RETVAL=$?
	;;
  *)
	echo "Usage: sendmail {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL

