NAME=postfix
VERSION=2.2.10
SOURCE=(ftp://ftp.jaquet.dk/mirror/ftp.porcupine.org/official/postfix-$VERSION.tar.gz
	rc.postfix.gz
	doinst.sh.gz )
SOURCE_MD5=(	-
		de4175ba3caa9f8b971cef5f47c668f2
		b72661a2313d88ec64e2b96f68f65719 )
BUILD=${BUILD:-1}
OPTIMIZE=${OPTIMIZE:-"-O2"}
ROOT_REQUIRED=1

build() {
	_explode 0
	cd $NAME-$VERSION

	make makefiles \
		OPT="$CFLAGS" \
		DEBUG="" \
		CCARGS="-DDEF_README_DIR=\\\"$DOCDIR/postfix-$VERSION\\\" -DFD_SETSIZE=2048 -DUSE_TLS -DUSE_SASL_AUTH -I/usr/include/sasl -DUSE_SSL" \
		AUXLIBS="-lsasl2 -lssl -lcrypto"
	make
	sh postfix-install install_root="$PKG" manpage_directory=$MANDIR -non-interactive

	# Dumb but efficient modification to configuration file which
	# sets Postfix to listen only on loopback. Of course, this is
	# stupid on most setups but admin should modify the configuration
	# files anyway before Postfix is ready for production. So let's
	# keep ports closed to the outside world by default:
	sed -i 's/^#inet_interfaces = all$/inet_interfaces = localhost\n&/' \
			"$PKG/etc/postfix/main.cf"

	# These have only man pages in comments:
	for I in access canonical generic header_checks relocated transport virtual; do
		_new_delete 0 etc/postfix/$I
	done

	for I in aliases main.cf master.cf; do
		_new 0 etc/postfix/$I
	done

	mkdir -p "$PKG/etc/rc.d"
	zcat "${SOURCE[1]}" > "$PKG/etc/rc.d/rc.postfix"
	_new_delete 0 etc/rc.d/rc.postfix

	mkdir -p "$PKG/install"
	zcat "${SOURCE[2]}" >> "$PKG/install/doinst.sh"


	### Be careful with the rest of this file ###

	# Hard link to symlink:
	rm -f "$PKG$LIBEXECDIR/postfix/nqmgr"
	ln -sf qmgr "$PKG$LIBEXECDIR/postfix/nqmgr"
	sed -i 's#nqmgr:h:#nqmgr:l:#' "$PKG$SYSCONFDIR/postfix/postfix-files"

	[ "$COMPRESS_MAN" = "1" ] && sed -i \
			'/^\$manpage_directory/{s#:f:root#.gz:f:root#}' \
			"$PKG$SYSCONFDIR/postfix/postfix-files"

	chown -R root:root "$PKG"
	_chgrp_bin
	(
		cd "$PKG$LOCALSTATEDIR/spool/postfix"
		chown 0:0 pid
		chown 89:0 active bounce corrupt defer deferred flush \
				hold incoming private saved trace
		chown 89:88 maildrop public
	)
	(
		cd "$PKG$SBINDIR"
		chown 0:88 postqueue postdrop
		chmod 2751 postqueue postdrop
		sed -i 's#setgid_group:2755:u#setgid_group:2751:u#' \
				"$PKG$SYSCONFDIR/postfix/postfix-files"
	)
}
