#!/usr/bin/make -f
# debian/rules for exim4
# This file is public domain software, originally written by Joey Hess.
#
# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

# avoid uname -m detection
ifdef DEB_TARGET_GNU_CPU
export EXIM_ARCHTYPE = $(DEB_TARGET_GNU_CPU)
else
export EXIM_ARCHTYPE = $(shell dpkg-architecture -qDEB_TARGET_GNU_CPU)
endif

buildname = $(shell scripts/os-type)-$(shell scripts/arch-type)
DEBIAN =    $(CURDIR)/debian

export DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

EXIM4VERSIONLOCALSCAN = \
	$(shell sed -rn \
	's/^\s*.define LOCAL_SCAN_ABI_VERSION_(MAJOR|MINOR)\s*([0-9]+).*/\2/p' \
	src/local_scan.h | \
	{ read MAJOR; read MINOR; echo $${MAJOR}.$${MINOR} ; }\
	)

# The build system ignores CPPFLAGS, append them to CFLAGS
CFLAGS += $(shell getconf LFS_CFLAGS) -D_LARGEFILE_SOURCE \
	  -fno-strict-aliasing -Wall $(CPPFLAGS)
export CFLAGS
# LFLAGS is used where GNU would use LDFLAGS
export LFLAGS = $(LDFLAGS)

LC_ALL=C
export LC_ALL

# If you want to build a daemon with a configuration tailored to YOUR special
# needs, uncomment the exim4-daemon-custom package in debian/control,
# call "debian/rules unpack-configs", copy EDITME.exim4-light to
# EDITME.exim4-custom and modify it, then call "debian/rules pack-configs".
#
# Afterwards EITHER uncomment the customdaemon definition below, or set it
# to the desired value via the environment.
# e.g run:
# env customdaemon=exim4-daemon-custom dpkg-buildpackage -uc -us
#
# If you want to create multiple custom packages with different names, use
# the script debian/create-custom-package [suffix].

# customdaemon = exim4-daemon-custom
daemons = exim4-daemon-light exim4-daemon-heavy $(customdaemon)

# If you want to build with OpenSSL instead of GnuTLS, uncomment this
# OPENSSL:=1
# Please note that building exim4-daemon-heavy with OpenSSL is a GPL
# violation.

# for reproducible build. If set exim would use $TZ as default value for
# TIMEZONE_DEFAULT
undefine TZ
unexport TZ


unpack-configs: unpack-configs-stamp

unpack-configs-stamp: src/EDITME exim_monitor/EDITME
	patch -o EDITME.eximon exim_monitor/EDITME \
		$(DEBIAN)/EDITME.eximon.diff
	patch -o EDITME.exim4-light src/EDITME \
		$(DEBIAN)/EDITME.exim4-light.diff
ifdef OPENSSL
	patch EDITME.exim4-light $(DEBIAN)/EDITME.openssl.exim4-light.diff
endif
	for editme in $(DEBIAN)/EDITME.exim4-*.diff; do \
	  if [ "$$editme" != "$(DEBIAN)/EDITME.exim4-light.diff" ]; then \
	    TARGETNAME=`basename $$editme .diff`; \
	    echo patch -o $$TARGETNAME EDITME.exim4-light $$editme; \
	    patch -o $$TARGETNAME EDITME.exim4-light $$editme || \
	    exit $$? ;\
	  fi; \
	done
	touch unpack-configs-stamp

pack-configs:
	-diff -u src/EDITME EDITME.exim4-light \
		> $(DEBIAN)/EDITME.exim4-light.diff
	-for editme in EDITME.exim4-*; do \
	  if [ "$$editme" != "EDITME.exim4-light" ]; then \
	    echo diff -u EDITME.exim4-light $$editme; \
	    diff -u EDITME.exim4-light $$editme > $(DEBIAN)/$${editme}.diff; \
	  fi; \
	done
	-diff -u exim_monitor/EDITME EDITME.eximon \
		> $(DEBIAN)/EDITME.eximon.diff

bdir-stamp: unpack-configs-stamp
	for i in $(daemons) ; do \
		mkdir b-$$i && \
		find . -mindepth 1 -maxdepth 1 \
		-name debian -prune -o \
		-name 'b-*' -o -print0 | \
		xargs --no-run-if-empty --null \
		cp -a --target-directory=b-$$i ; \
	done
	touch $@

override_dh_auto_configure: unpack-configs-stamp bdir-stamp
	for i in $(daemons) ; do \
		mkdir -p b-$$i/Local && \
		cp EDITME.`echo $$i | sed -e s/exim4-daemon/exim4/` \
		b-$$i/Local/Makefile && \
		cp EDITME.eximon b-$$i/Local/eximon.conf ;\
		done

override_dh_auto_build:
	set -e ; for i in $(daemons) ; do \
		echo building $$i; \
		cd $(CURDIR)/b-$$i && \
		$(MAKE) FULLECHO='' ; \
		done
	# Which version of Berkeley DB are we building against?
	printf '#include <db.h>\ninstdbversionis DB_VERSION_MAJOR DB_VERSION_MINOR\n' | \
		cpp -P | grep instdbversionis |\
		sed -e 's/[[:space:]]*instdbversionis[[:space:]]//' \
		-e 's/[[:space:]][[:space:]]*/./' \
		-e 's_^_s/^BDBVERSION=.*/BDBVERSION=_' \
		-e 's_$$_/_' \
		> $(DEBIAN)/berkeleydb.sed
	# Store Berkeley DB version in postinst script.
	sed -i -f $(DEBIAN)/berkeleydb.sed \
		$(DEBIAN)/exim4-base.postinst
	# symlink identical maintainerscripts
	for i in `echo $(daemons) | sed -e s/exim4-daemon-light//` ; do \
		ln -sfv exim4-daemon-light.prerm \
			"$(DEBIAN)/$$i.prerm" ; \
		ln -sfv exim4-daemon-light.postinst \
			"$(DEBIAN)/$$i.postinst" ; \
	done

override_dh_auto_install-arch: debian/README.Debian
	cd b-exim4-daemon-light && \
	  $(MAKE) install FULLECHO='' \
		INSTALL_ARG=-no_symlink \
		inst_conf=$(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf \
		inst_aliases=$(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/aliases \
		inst_dest=$(DEBIAN)/exim4-base/usr/sbin
	if [ -e "$(DEBIAN)/example.conf.md5" ] && [ "$$(< $(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf md5sum)" != "$$(cat $(DEBIAN)/example.conf.md5)" ] ; then \
	  echo "upstream example configuration has changed, new md5sum:"; \
	  < $(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf md5sum; \
	  echo "aborting build."; \
	  exit 1; \
	fi
	< $(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf md5sum > $(DEBIAN)/example.conf.md5
	sed -e 's,/[a-zA-Z/0-9.-]*exim4-base/examples/,/etc/,' \
		< $(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf \
		> $(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf.tmp
	mv $(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf.tmp \
		$(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples/example.conf
	install -m755 b-exim4-daemon-light/build-$(buildname)/convert4r4 \
		$(DEBIAN)/exim4-base/usr/sbin/exim_convert4r4
	install -m755 \
		b-exim4-daemon-light/build-$(buildname)/transport-filter.pl \
		b-exim4-daemon-light/util/ratelimit.pl \
		$(DEBIAN)/exim4-base/usr/share/doc/exim4-base/examples
	rm $(DEBIAN)/exim4-base/usr/sbin/exim
	mv $(DEBIAN)/exim4-base/usr/sbin/eximon \
		$(DEBIAN)/eximon4/usr/sbin
	mv $(DEBIAN)/exim4-base/usr/sbin/eximon.bin \
		$(DEBIAN)/eximon4/usr/libexec/exim4
	pod2man --center=EXIM4 --section=8 \
		$(DEBIAN)/exim4-base/usr/sbin/exipick \
		$(DEBIAN)/exim4-base/usr/share/man/man8/exipick.8
	pod2man --center=EXIM4 --section=8 \
		$(DEBIAN)/exim4-base/usr/sbin/eximstats \
		$(DEBIAN)/exim4-base/usr/share/man/man8/eximstats.8
	install -m755 $(DEBIAN)/syslog2eximlog $(DEBIAN)/exim4-base/usr/sbin/
	pod2man --center=EXIM4 --section=8 \
		$(DEBIAN)/syslog2eximlog \
		$(DEBIAN)/exim4-base/usr/share/man/man8/syslog2eximlog.8
	for i in b-exim4-daemon-*/build-$(buildname)/exim ; do \
		install -m4755 -oroot -groot $$i \
		$(DEBIAN)/`echo $$i | sed -e 's/^b-//' -e 's_/.*__'`/usr/sbin/exim4 ; \
		done

override_dh_auto_install-indep: debian/README.Debian
	# if you change anything here, you will have to change
	# config-custom/debian/rules as well
	sed -e \
	"s/^UPEX4C_version=\"\"/UPEX4C_version=\"$(DEB_VERSION)\"/" \
	< $(DEBIAN)/debconf/update-exim4.conf \
	> $(DEBIAN)/exim4-config/usr/sbin/update-exim4.conf
	chmod 755 $(DEBIAN)/exim4-config/usr/sbin/update-exim4.conf
	install -m 755 $(DEBIAN)/update-exim4defaults \
		$(DEBIAN)/exim4-config/usr/sbin

	cd $(DEBIAN)/debconf/conf.d && \
		tar cf - `find \( -path '*/.svn/*' -prune \) -or \
		\( -type f -print \)` | \
		{ cd $(DEBIAN)/exim4-config/etc/exim4/conf.d/ && \
		tar xf - ; }

	install -m644 $(DEBIAN)/email-addresses $(DEBIAN)/exim4-config/etc/
	install -m640 -oroot -groot $(DEBIAN)/passwd.client \
		$(DEBIAN)/exim4-config/etc/exim4/
	chmod 755 $(DEBIAN)/debconf/update-exim4.conf.template
	env CONFDIR=$(DEBIAN)/debconf \
		$(DEBIAN)/debconf/update-exim4.conf.template --nobackup --run

# only called manually by maintainer before upload.
update-mtaconflicts:
	command -v grep-aptavail > /dev/null && \
		grep-aptavail --show-field=Package --field=Provides \
		mail-transport-agent --no-field-names \
		| grep -v exim | sort -u | \
		tr '\n' ',' | sed -e 's/,/, /g;s/, $$//' > $(DEBIAN)/mtalist

# Generate README.Debian as text/html ...
debian/README.Debian.html: debian/README.Debian.xml
	xsltproc --nonet --stringparam section.autolabel 1 \
		-o $@ \
		/usr/share/xml/docbook/stylesheet/nwalsh/html/docbook.xsl \
		$<
# ... and text/plain
debian/README.Debian: debian/README.Debian.html
	chmod 755 $(DEBIAN)/lynx-dump-postprocess
	lynx -force_html -dump $< | $(DEBIAN)/lynx-dump-postprocess > $@.tmp
	mv $@.tmp $@

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# it is not possible to run exim unless the compile-time specified
	# user exists.
	if id -u Debian-exim ; then \
		echo Debian-exim user found, running minimal testsuite ; \
		chmod +x debian/minimaltest ; \
		rm -rf $(CURDIR)/test ; \
		for i in b-exim4-daemon*/build-$(buildname)/exim ;\
			do mkdir $(CURDIR)/test && \
			debian/minimaltest $(CURDIR)/test $$i || \
			{ echo testsuite error  ; exit 1 ; } ; \
			rm -rf $(CURDIR)/test ; \
		done \
	fi
endif

override_dh_auto_clean:
	debconf-updatepo

	-rm -rf build-* doc/tmp test/ b-exim*
	-rm -f EDITME.* unpack-configs-stamp bdir-stamp
	-rm -f $(DEBIAN)/debconf/exim4.conf.template $(DEBIAN)/files \
		$(DEBIAN)/README.Debian $(DEBIAN)/README.Debian.html \
		$(DEBIAN)/berkeleydb.sed

	#these are identical for all daemon-* and therefore symlinked
	@cd $(DEBIAN) && find . -maxdepth 1 \
		-regex '^\./exim4-daemon-.*\.\(postinst\|prerm\)$$' \
		-and -not -name 'exim4-daemon-light.*' -delete
	#pwd
	chmod 755 $(DEBIAN)/exim-gencert \
		$(DEBIAN)/lynx-dump-postprocess $(DEBIAN)/script \
		$(DEBIAN)/exim-adduser

override_dh_installchangelogs:
	dh_installchangelogs -pexim4-base doc/ChangeLog
	dh_installchangelogs --no-package=exim4-base \
		-XCHANGES -Xdoc/ChangeLog

override_dh_installppp:
	dh_installppp --name=exim4

override_dh_fixperms:
	dh_fixperms -X/etc/exim4/passwd.client -Xusr/sbin/exim4

override_dh_gencontrol:
	dh_gencontrol -- \
		-VUpstream-Version=$(DEB_VERSION_EPOCH_UPSTREAM) \
		-VMTA-Conflicts="$(shell cat $(DEBIAN)/mtalist)" \
		-Vlocalscanabiversion="exim4-localscanapi-$(EXIM4VERSIONLOCALSCAN)"

override_dh_installlogrotate:
	dh_installlogrotate
	dh_installlogrotate --name=exim4-paniclog

override_dh_installinit:
	dh_installinit --noscripts --name=exim4

override_dh_install:
	# install config.h from daemon package, but not from exim4-daemon-light
	dh_install -p exim4-dev \
		$(shell ls -1 b-exim4-daemon-*/build-$(buildname)/config.h | grep -v ^b-exim4-daemon-light/) \
		usr/include/exim4
	dh_install

override_dh_link:
	rm -rf debian/exim4/usr/share/doc/exim4
	dh_link

%:
	dh $@ --no-parallel

.PHONY: pack-configs unpack-configs update-mtaconflicts