#!/usr/bin/make -f
# -*- makefile -*-
# Debianrules for building a Debian package
# Version 1.5
#
# These rules have been specifically designed NOT to require root to
# run them. At any time root privileges are required, the command to be
# executed will be made obvious and root's password will be prompted for.
# Of course, root may still run this and no password will be required.
#
# Robert Leslie <rob@mars.org>
# modified for Postfix by LaMont Jones <lamont@debian.org>

PACKAGE=postfix
DISTRO:=$(shell (lsb_release -is 2>/dev/null || echo Debian) | sed s:Debian:Debian/GNU:)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_STRIP = -Wl,-Bsymbolic-functions
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

package=postfix
base=debian/$(package)
docpkg=${package}-doc
exampledir=${base}-doc/usr/share/doc/${package}
docdir=${base}-doc/usr/share/doc/$(package)
chlogdir=${base}/usr/share/doc/$(package)
sharedir=${base}/usr/share/postfix
libdir=${base}/usr/lib/postfix
plibdir=usr/lib/postfix
sbindir=usr/sbin
bindir=${base}/usr/bin
confdir=${base}/etc/postfix

#ifeq ($(DEB_BUILD_ARCH),sparc)
#  OFLAGS = -O1
#else
#  OFLAGS = -O1
#endif

OFLAGS += -O2
SHELL=/bin/bash

DEBUG += -g
ifneq (,$(findstring nodebug,$(DEB_BUILD_OPTIONS)))
DEBUG = ''
endif

ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
STRIP=y
endif

CCARGS += -DDEBIAN -DHAS_PCRE -DHAS_LDAP -DUSE_LDAP_SASL \
	-DHAS_SQLITE -DMYORIGIN_FROM_FILE \
	$(shell getconf LFS_CFLAGS) \
	-DHAS_CDB \
	-DHAS_LMDB \
	-DHAS_MYSQL -I/usr/include/mysql \
	-DHAS_PGSQL -I`pg_config --includedir` \
	-DHAS_SQLITE -I/usr/include \
	-DHAS_SSL -I/usr/include/openssl \
	-DUSE_SASL_AUTH -I/usr/include/sasl \
	-DUSE_CYRUS_SASL \
	-DUSE_TLS

AUXLIBS += -lssl -lcrypto -lsasl2 -lpthread

DOCFILES=README_FILES/*_README README_FILES/OVERVIEW COMPATIBILITY PORTING
TLSDOCFILES=${TLSSRC}/TLS_TODO ${TLSSRC}/TLS_ACKNOWLEDGEMENTS

.PHONY: install install-doc binary binary-arch binary-indep clean
.PHONY: checkroot build-arch build-indep

build: build-arch build-indep
build-arch:
	$(checkdir)
	${MAKE} makefiles CCARGS="${CFLAGS} ${CCARGS}" DEBUG="${DEBUG}" \
		AUXLIBS="${AUXLIBS} ${LDFLAGS} -L$$(pwd)/debian" OPT="$(OFLAGS)" \
		AUXLIBS_CDB="-lcdb -L../../lib -L. -lpostfix-util" \
		AUXLIBS_LDAP="-lldap -llber -L../../lib -L. -lpostfix-util -lpostfix-global" \
		AUXLIBS_LMDB="-llmdb -L../../lib -L. -lpostfix-util" \
		AUXLIBS_MYSQL="-lmysqlclient -L../../lib -L. -lpostfix-util -lpostfix-global" \
		AUXLIBS_PCRE="-lpcre -L../../lib -L. -lpostfix-util" \
		AUXLIBS_PGSQL="-lpq -L../../lib -L. -lpostfix-util -lpostfix-global" \
		AUXLIBS_SQLITE="-lsqlite3 -L../../lib -L. -lpostfix-util -lpostfix-global -lpthread" \
		shared=yes pie=yes dynamicmaps=yes \
		daemon_directory=${CUR_DIR}/usr/lib/postfix/sbin \
		shlibs_directory=${CUR_DIR}/usr/lib/postfix manpage_directory=/usr/share/man \
		sample_directory=/usr/share/doc/postfix/examples readme_directory=/usr/share/doc/postfix \
		html_directory=/usr/share/doc/postfix/html
	${MAKE} LD_LIBRARY_PATH=$$(pwd)/lib:$${LD_LIBRARY_PATH}
	${MAKE} manpages
	sed "s:@@DISTRO@@:$(DISTRO):g" < debian/main.cf.in > conf/main.cf.debian
	sed "s:@@DISTRO@@:$(DISTRO):g" < conf/main.cf > conf/main.cf.dist
	touch build

build-indep:
	$(checkdir)
	${MAKE} makefiles CCARGS="${CFLAGS} ${CCARGS}" DEBUG="${DEBUG}" \
		AUXLIBS="${AUXLIBS} ${LDFLAGS} -L$$(pwd)/debian" OPT="$(OFLAGS)" \
		AUXLIBS_CDB="-lcdb -L../../lib -L. -lpostfix-util" \
		AUXLIBS_LDAP="-lldap -llber -L../../lib -L. -lpostfix-util -lpostfix-global" \
		AUXLIBS_LMDB="-llmdb -L../../lib -L. -lpostfix-util" \
		AUXLIBS_MYSQL="-lmysqlclient -L../../lib -L. -lpostfix-util -lpostfix-global" \
		AUXLIBS_PCRE="-lpcre -L../../lib -L. -lpostfix-util" \
		AUXLIBS_PGSQL="-lpq -L../../lib -L. -lpostfix-util -lpostfix-global" \
		AUXLIBS_SQLITE="-lsqlite3 -L../../lib -L. -lpostfix-util -lpostfix-global -lpthread" \
		shared=yes pie=yes dynamicmaps=yes \
		daemon_directory=${CUR_DIR}/usr/lib/postfix/sbin \
		shlibs_directory=${CUR_DIR}/usr/lib/postfix manpage_directory=/usr/share/man \
		sample_directory=/usr/share/doc/postfix/examples readme_directory=/usr/share/doc/postfix \
		html_directory=/usr/share/doc/postfix/html
	${MAKE} LD_LIBRARY_PATH=$$(pwd)/lib:$${LD_LIBRARY_PATH}
	${MAKE} manpages
	sed "s:@@DISTRO@@:$(DISTRO):g" < debian/main.cf.in > conf/main.cf.debian
	sed "s:@@DISTRO@@:$(DISTRO):g" < conf/main.cf > conf/main.cf.dist
	touch build

install-indep: build-indep
	dh_prep
	dh_installdirs -i
	install -m 0444 html/* $(docdir)/html; rm $(docdir)/html/Makefile.in
	dh_installexamples -p ${docpkg} examples/{qmail-local,smtpd-policy}
	dh_installexamples -p ${docpkg} -Xmain.cf -Xmaster.cf -Xfiles conf/[a-z]*
	dh_installexamples -p ${docpkg} conf/main.cf.default debian/mailqfmt.pl
	install -m 0444 RELEASE_NOTES $(docdir)/RELEASE_NOTES
	install -m 0444 AAAREADME $(docdir)/README
	for file in */README; do \
	    install -m 0444 $${file} $(docdir)/README.$${file%/README}; \
	done
	if [ ! -f meta/doc.files ] ; then \
		cd meta && for i in sample readme html ; do \
			grep $${i} postfix-files >> doc.files ; \
		done \
	fi
	rm -f $(docdir)/README.mantools
	for file in ${DOCFILES}; do					\
		install -m 0444 $${file} $(docdir)/$${file##*/};	\
	done
	rm -f $(docdir)/ULTRIX_README $(docdir)/MACOSX_README
	cd meta && for h in ULTRIX MACOSX mantools ; do \
		grep -v $${h} doc.files > doc.files.$$ ; \
		mv doc.files.$$ doc.files ; \
	done
	mkdir -m 755 -p $(base)-doc/etc/postfix/postfix-files.d
	install -m 644 meta/doc.files ${base}-doc/etc/postfix/postfix-files.d

install-arch: build-arch
	dh_prep
	dh_installdirs -a
	install lib/lib* $(libdir)
	install debian/postfix_groups.pl $(libdir)
	install -m 0644 meta/postfix-files $(confdir)
	cd $(confdir) && for i in sample readme html LICENSE ; do \
		grep -v $${i} postfix-files > postfix-files.$$ ; \
		mv postfix-files.$$ postfix-files ; \
	done
	for j in cdb ldap lmdb mysql pcre pgsql sqlite ; do \
		mkdir -m 755 -p $(base)-$${j}/etc/postfix/postfix-files.d ; \
		grep $${j} $(confdir)/postfix-files >> $(base)-$${j}/etc/postfix/postfix-files.d/$${j}.files ; \
		grep -v $${j} $(confdir)/postfix-files > $(confdir)/postfix-files.$$ ; \
			mv $(confdir)/postfix-files.$$ $(confdir)/postfix-files ; \
		install lib/postfix-$${j}.so ${base}-$${j}/${plibdir} ; \
		ln -s postfix-$${j}.so ${base}-$${j}/${plibdir}/postfix-$${j}.so.1.0.1 ; \
	done
	install libexec/[a-z]* ${base}/${plibdir}/sbin
	install bin/[a-z]* ${base}/${sbindir}
	# See conf/postfix-files
	ln ${libdir}/sbin/smtp ${libdir}/sbin//lmtp
	ln ${libdir}/sbin/qmgr ${libdir}/sbin/nqmgr
	install debian/postfix-add-filter ${base}/${sbindir}
	install debian/postfix-add-policy ${base}/${sbindir}
	install auxiliary/qshape/qshape.pl ${base}/${sbindir}/qshape
	install -m 0444 HISTORY $(chlogdir)/changelog
	ln -s ../sbin/rmail $(bindir)/rmail
	ln -s ../sbin/sendmail $(bindir)/newaliases
	ln -s ../sbin/sendmail $(bindir)/mailq
	ln -s ../sbin/sendmail ${base}/usr/lib/sendmail
	install -m 0755 conf/postfix-script conf/post-install $(confdir)
	install -m 0644 conf/makedefs.out $(sharedir)
	install -m 0644 debian/functions $(sharedir)/postinst.functions
	install -m 0644 conf/master.cf $(sharedir)/master.cf.dist
	install -m 0644 conf/main.cf.dist $(sharedir)/main.cf.dist
	install -m 0644 conf/main.cf.debian $(sharedir)/main.cf.debian
	install -m 0644 conf/main.cf.tls $(sharedir)/main.cf.tls
	install -m 0644 meta/dynamicmaps.cf $(sharedir)/dynamicmaps.cf
	install -m644 debian/postfix.ufw.profile debian/postfix/etc/ufw/applications.d/postfix
	install -m644 debian/rsyslog.conf debian/postfix/etc/rsyslog.d/postfix.conf
	install -m644 debian/postfix.insserv.conf debian/postfix/etc/insserv.conf.d/postfix

	install man/man1/*.1 ${base}/usr/share/man/man1
	install man/man5/*.5 ${base}/usr/share/man/man5
	for j in ldap lmdb mysql pcre pgsql sqlite ; do \
		mkdir -m 755 -p $(base)-$${j}/usr/share/man/man5 ; \
		mv ${base}/usr/share/man/man5/$${j}_table.5 ${base}-$${j}/usr/share/man/man5 ; \
        done

	for f in man/man8/*.8; do \
	  sed '/^\.TH/s/ 8 / 8postfix /' $${f}>${base}/usr/share/$${f}postfix; \
	  chmod 644 ${base}/usr/share/$${f}postfix; \
	  chown root:root ${base}/usr/share/$${f}postfix; \
	done
	install rmail/rmail.8 debian/*.8 ${base}/usr/share/man/man8
	gzip -9n ${base}/usr/share/man/man8/*.8postfix
	ln -sf bounce.8postfix.gz ${base}/usr/share/man/man8/trace.8postfix.gz
	ln -sf bounce.8postfix.gz ${base}/usr/share/man/man8/defer.8postfix.gz

	install debian/configure-instance.sh $(libdir)
	install debian/postfix-instance-generator ${base}/lib/systemd/system-generators/
	install -m 644 debian/postfix@.service ${base}/lib/systemd/system/
	install debian/ip-up.d ${base}/etc/ppp/ip-up.d/postfix
	install debian/ip-down.d ${base}/etc/ppp/ip-down.d/postfix
	install debian/ip-up.d ${base}/etc/network/if-up.d/postfix
	install debian/ip-down.d ${base}/etc/network/if-down.d/postfix
	install debian/update-libc.d ${base}/etc/resolvconf/update-libc.d/postfix
	install debian/postfix-cdb.lintian-override ${base}-cdb/usr/share/lintian/overrides/${package}-cdb
	install debian/postfix-ldap.lintian-override ${base}-ldap/usr/share/lintian/overrides/${package}-ldap
	install debian/postfix-lmdb.lintian-override ${base}-lmdb/usr/share/lintian/overrides/${package}-lmdb
	install debian/postfix.lintian-override ${base}/usr/share/lintian/overrides/${package}
	install debian/postfix-mysql.lintian-override ${base}-mysql/usr/share/lintian/overrides/${package}-mysql
	install debian/postfix-pcre.lintian-override ${base}-pcre/usr/share/lintian/overrides/${package}-pcre
	install debian/postfix-pgsql.lintian-override ${base}-pgsql/usr/share/lintian/overrides/${package}-pgsql

	if dpkg-vendor --is ubuntu; then \
	    install -m 644 -D debian/postfix.apport ${base}/usr/share/apport/package-hooks/postfix.py; \
	fi

debian/vars:
	cp debian/vars.in $@

binary-indep: checkroot install-indep debian/vars
	dh_installdocs -i
##	dh_installexamples -i
##	dh_installmenu -i
##	dh_installcron -i
	dh_installchangelogs -i
	dh_installdebconf -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	for i in $$(sed -n '/^Package:/s/^.* //p' debian/control); do cat debian/vars >> debian/$$i.substvars; done
	cat debian/vars.in >> debian/substvars
	dh_gencontrol -i
##	dh_makeshlibs -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: checkroot install-arch debian/vars

	dh_installdocs -a
##	dh_installexamples -a
##	dh_installmenu -a
##	dh_installcron -a
	dh_installchangelogs -a
	dh_installdebconf -a
	dh_installsystemd -a
	dh_installinit -a
##	dh_movefiles -a
	[ -n "$(STRIP)" ] || dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -l -a
	for i in $$(sed -n '/^Package:/s/^.* //p' debian/control); do cat debian/vars >> debian/$$i.substvars; done
	cat debian/vars.in >> debian/substvars
	if [ $(DISTRO) = Ubuntu ]; then echo postfix:Provides=default-mta >> debian/postfix.substvars; fi
	dh_gencontrol -a
##	dh_makeshlibs -a
	dh_md5sums -a
	dh_builddeb -a

clean-preunpatch:
	$(checkdir)
	dh_clean build
	test ! -d ${base} || rm -rf ${base}
	$(MAKE) tidy

clean: clean-preunpatch
	rm -rf debian/{files*,vars,*substvars,*.debhelper}
	find .. -maxdepth 1 -name $(package)*.asc -size 0 -exec rm {} ";"
	rm -f debian/stamp-* conf/main.cf.debian conf/main.cf.dist meta/doc.files

buildinfo:
	@echo; dpkg -l gcc "libc6*" binutils ldso make dpkg-dev $(BUILDINFO) \
		| awk '$$1 == "ii" { printf("%s-%s\n", $$2, $$3) }' \
		| tee $(docdir)/buildinfo.Debian; echo
	chmod 644 $(docdir)/buildinfo.Debian

define checkdir
	test -f debian/rules
endef

# Below here is fairly generic really

binary: binary-arch binary-indep

newtemplate:
	debconf-updatepo

msgstats:
	@cd debian/po && for i in *.po; do x=$$(msgfmt --statistics $$i 2>&1); echo $$i $$x; done; rm -f messages.mo *.po~

msg-email:
	@podebconf-report-po

checkroot:
	$(checkdir)
	test "`id -u`" -eq 0