# Template file for 'postfix'
pkgname=postfix
version=2.10.2
revision=2
short_desc="High-performance mail transport agent"
maintainer="Juan RP <xtraeme@gmail.com>"
license="IBM Public License 1.0, BSD-alike"
homepage="http://www.postfix.org/"
distfiles="ftp://ftp.porcupine.org/mirrors/postfix-release/official/$pkgname-$version.tar.gz"
checksum=f1a735a5a9ffeac8fca59046c437c3b76e7f923fb4249db2a55b2bff0306ddd4

hostmakedepends="perl"
makedepends="openssl-devel db-devel>=5.3 pcre-devel>=8.30 libsasl-devel
 libldap-devel libmysqlclient-devel>=5.5.27 postgresql-libs-devel sqlite-devel"

if [ "$CROSS_BUILD" ]; then
	# XXX postfix-install needs native binaries.
	hostmakedepends+=" ${pkgname}>=2.10.2"
fi

conf_files="
	/etc/postfix/access
	/etc/postfix/aliases
	/etc/postfix/canonical
	/etc/postfix/generic
	/etc/postfix/header_checks
	/etc/postfix/main.cf
	/etc/postfix/master.cf
	/etc/postfix/relocated
	/etc/postfix/transport
	/etc/postfix/virtual"
systemd_services="postfix.service true"
system_accounts="postfix"
postfix_homedir="/var/spool/postfix"
system_groups="postdrop"
provides="smtp-server-0_1"
replaces="smtp-server>=0"
make_dirs="
	/var/lib/postfix		0750	postfix	root
	/var/spool/postfix/active	0750	postfix	root
	/var/spool/postfix/bounce	0750	postfix	root
	/var/spool/postfix/corrupt	0750	postfix	root
	/var/spool/postfix/defer	0750	postfix	root
	/var/spool/postfix/deferred	0750	postfix	root
	/var/spool/postfix/flush	0750	postfix	root
	/var/spool/postfix/hold		0750	postfix	root
	/var/spool/postfix/incoming	0750	postfix	root
	/var/spool/postfix/private	0750	postfix	root
	/var/spool/postfix/maildrop	0750	postfix	postdrop
	/var/spool/postfix/public	0750	postfix	postdrop
	/var/spool/postfix/pid		0750	root	root
	/var/spool/postfix/saved	0750	postfix	root
	/var/spool/postfix/trace	0750	postfix	root"

do_build() {
	sed -e "s,\(/usr/include/db.h\),${XBPS_CROSS_BASE}/\1,g" -i makedefs

	make makefiles DEBUG='' CCARGS=' \
		-DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl \
		-DHAS_LDAP \
		-DUSE_TLS \
		-DHAS_MYSQL -I/usr/include/mysql \
		-DHAS_PGSQL -I/usr/include/postgresql \
		-DHAS_SQLITE \
		-DDEF_COMMAND_DIR=\"/usr/bin\" \
		-DDEF_SENDMAIL_PATH=\"/usr/bin/sendmail\" \
		-DDEF_README_DIR=\"/usr/share/doc/postfix\" \
		-DDEF_SAMPLE_DIR=\"/usr/share/examples/postfix/\" \
		-DDEF_MANPAGE_DIR=\"/usr/share/man\" \
	' AUXLIBS=' \
		-lsasl2 \
		-lldap -llber \
		-lssl -lcrypto \
		-lmysqlclient -lz -lm \
		-lpq \
		-lsqlite3 -lpthread \
	' OPT="${CFLAGS} ${LDFLAGS}"

	if [ "$CROSS_BUILD" ]; then
		sed -e "s,/usr/include,${XBPS_CROSS_BASE}/usr/include,g" -i Makefile
	fi
	make ${makejobs}
}

pre_install() {
	if [ "$CROSS_BUILD" ]; then
		sed -e 's,bin/postconf,/usr/bin/postconf,g' -i postfix-install
	fi
}

do_install() {
	sh postfix-install -non-interactive install_root=${DESTDIR}

	vinstall LICENSE 644 usr/share/licenses/${pkgname}
	vinstall TLS_LICENSE 644 usr/share/licenses/${pkgname}

	cd $DESTDIR
	sed 's/\(^\$manpage[^:]*\):/\1.gz:/' -i usr/libexec/${pkgname}/postfix-files
	patch -p0 -i ${FILESDIR}/aliases.patch

	# Remove unused stuff.
	rm -f ${DESTDIR}/etc/postfix/*LICENSE
	rm -f ${DESTDIR}/etc/postfix/*.default

	# systemd.
	vinstall ${FILESDIR}/postfix.service 644 usr/lib/systemd/system
}
