#!/bin/sh
#
# postfix       This shell script takes care of starting and stopping
#               postfix.
#
# chkconfig:	345 80 30
#
# description:	Postfix is a Mail Transport Agent, which is the program
#		that moves mail from one machine to another.


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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/postfix ] && . /etc/sysconfig/postfix

# Check that networking is up.
if is_no "${NETWORKING}"; then
        msg_network_down Postfix
        exit 1
fi

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/postfix ]; then
		msg_starting Postfix
		busy
		MSG="`/usr/sbin/postfix start 2>&1`"
		if [ "$?" = "0" ] ; then
			deltext; ok
			touch /var/lock/subsys/postfix
		else
			deltext; fail
			echo "$MSG"
		fi
	else
		msg_already_running Postfix
	fi
	;;
  stop)
	# Stop daemons.
        if [ -f /var/lock/subsys/postfix ]; then
                msg_stopping Postfix
		busy
		MSG="`/usr/sbin/postfix stop 2>&1`"
		if [ "$?" = "0" ]; then
			deltext; ok
		else
			deltext; fail
			echo "$MSG"
		fi
		rm -f /var/lock/subsys/postfix
        else
                msg_not_running Postfix
                exit 1
	fi
	;;
  restart)
	$0 stop
	$0 start
	;;
  reload)
	/usr/sbin/postfix reload
	;;
  status)
	status master
	exit $?
	;;
  rebuilddb)
  	standard_db="access canonical relocated transport virtual"
	extra_db=$(ls -1 /etc/mail/*.db 2> /dev/null | grep -v aliases.db | sed -e 's#.db$##')
	for base in $standard_db $extra_db; do
	    	I=$(basename "$base")
		if [ -f /etc/mail/$I ] ; then
			/usr/sbin/postmap hash:/etc/mail/$I < /etc/mail/$I
		fi
	done
	/usr/bin/newaliases
  	;;
  *)
	msg_usage "$0 {start|stop|status|restart|reload|rebuilddb}"
	exit 1
esac

exit $RETVAL
