#!/bin/sh -e

# Start or stop Postfix
#
# LaMont Jones <lamont@debian.org>
# based on sendmail's init.d script

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/postfix
PIDFILE=/var/run/postfix.pid
NAME=Postfix

test -x $DAEMON -a -f /etc/postfix/main.cf || exit 0

case "$1" in
    start)
	echo -n "Starting mail transport agent: Postfix"

	# Make sure that the chroot environment is set up correctly.
	oldumask=$(umask)
	umask 022
	cd $(postconf -h queue_directory)
	for file in etc/localtime etc/services etc/resolv.conf etc/hosts \
		etc/nsswitch.conf usr/lib/zoneinfo/localtime ; do
	    [ -d ${file%/*} ] || mkdir -p ${file%/*}
	    if [ -f /${file} ]; then cp /${file} ${file}; fi
	    if [ -f  ${file} ]; then chmod a+rX ${file}; fi
	done
	rm -f lib/libnss_*so*
	tar cf - /lib/libnss_*so* 2>/dev/null |tar xf -
	umask $oldumask

	/usr/sbin/postfix start 2>&1 |
		(grep -v 'starting the Postfix' 1>&2 || /bin/true)
	echo "."
    ;;

    stop)
	echo -n "Stopping mail transport agent: Postfix"
	/usr/sbin/postfix stop 2>&1 |
		(grep -v 'stopping the Postfix' 1>&2 || /bin/true)
	echo "."
    ;;

    restart)
        $0 stop
        $0 start
    ;;
    
    reload)
	echo -n "Reloading Postfix configuration..."
	/usr/sbin/postfix reload 2>&1 |
		(grep -v 'refreshing the Postfix' 1>&2 || /bin/true)
	echo "done."
    ;;

    flush)
	/usr/sbin/postfix flush
    ;;

    check)
	/usr/sbin/postfix check
    ;;

    *)
	echo "Usage: /etc/init.d/postfix {start|stop|restart|reload|flush|check|force-reload}"
	exit 1
    ;;
esac

exit 0
