#!/bin/sh
#
# postfix	This script takes care of starting and stopping postfix MTA.
#
# chkconfig:	2345 80 30
#
# description: \
#	Postfix is Wietse Venema's attempt to provide an alternative to \
#	the widely-used sendmail program.  Postfix Mail Transport Agent \
#	attempts to be fast, easy to administer, and hopefully secure, \
#	while at the same time being sendmail compatible enough to not \
#	upset your users.
#
# processname:	master
# config: /etc/postfix/
# pidfile: /var/spool/postfix/pid/master.pid

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

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

DAEMON=/usr/sbin/postfix
ROOT=/var/spool/postfix
ALIASES=/etc/postfix/aliases
LOCKFILE=/var/lock/subsys/postfix
RETVAL=0

adjust()
{
	grep -qs ^root: /etc/postfix/aliases ||
		action "Adjusting $ALIASES:" /usr/share/sendmail-common/make_aliases "$ALIASES"
	action "Adjusting environment for postfix:" /etc/chroot.d/postfix.all || return
	RETVAL=$?
	return $RETVAL
}

start()
{
	adjust || return
	action "Starting postfix:" "$DAEMON" start
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	return $RETVAL
}

stop()
{
	action "Shutting down postfix:" "$DAEMON" stop
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	return $RETVAL
}

check()
{
	action "Checking postfix configuration:" "$DAEMON" check
	RETVAL=$?
	return $RETVAL
}

flush()
{
	action "Flushing postfix queue:" "$DAEMON" flush
	RETVAL=$?
	return $RETVAL
}

reload()
{
	adjust || return
	action "Reloading postfix configuration:" "$DAEMON" reload
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	adjust)
		adjust
		;;
	check)
		check
		;;
	flush)
		flush
		;;
	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 master
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|adjust|check|flush|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
