# Template file for 'postfix'
pkgname=postfix
version=3.0.1
revision=1
short_desc="High-performance mail transport agent"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
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=cd2bd6447fe3368bb1a39f482f8dd1eec87c63067a0eb75f9aec8e4eadd21328

hostmakedepends="perl"
makedepends="libressl-devel db-devel pcre-devel libsasl-devel
 libldap-devel libmysqlclient-devel postgresql-libs-devel sqlite-devel
 icu-devel"

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

build_pie=yes
build_options="systemd"
system_accounts="postfix"
postfix_homedir="/var/spool/postfix"
system_groups="postdrop"
provides="smtp-server-0_1"
replaces="smtp-server>=0"
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"
make_dirs="
	/etc/postfix/postfix-files.d	0755	root	root
	/var/lib/postfix		0700	postfix	root
	/var/spool/postfix/active	0700	postfix	root
	/var/spool/postfix/bounce	0700	postfix	root
	/var/spool/postfix/corrupt	0700	postfix	root
	/var/spool/postfix/defer	0700	postfix	root
	/var/spool/postfix/deferred	0700	postfix	root
	/var/spool/postfix/flush	0700	postfix	root
	/var/spool/postfix/hold		0700	postfix	root
	/var/spool/postfix/incoming	0700	postfix	root
	/var/spool/postfix/private	0700	postfix	root
	/var/spool/postfix/maildrop	0730	postfix	postdrop
	/var/spool/postfix/public	0710	postfix	postdrop
	/var/spool/postfix/pid		0755	root	root
	/var/spool/postfix/saved	0700	postfix	root
	/var/spool/postfix/trace	0700	postfix	root"

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

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

	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}
	vmkdir usr/sbin
	ln -s /usr/bin/sendmail ${DESTDIR}/usr/sbin/sendmail

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

	cd $DESTDIR
	patch -p0 -i ${FILESDIR}/aliases.patch

	# systemd
	if [ "$build_option_systemd" ]; then
		vinstall ${FILESDIR}/postfix.service 644 usr/lib/systemd/system
	fi
	vsv postfix
}
