#!/usr/bin/make -f

%:
	dh $@ --with python3

# Enable hardening flags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-as-needed

# Enable verbose output on the testsuite if a debug build is requested
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
   RUNTESTFLAGS = -v -v -v --debug
endif

override_dh_auto_configure:
	DEFAULT_CUPS_CONFDIR=/usr/share/cups/mime \
	PYTHON_CONFIG=/usr/bin/python3-config \
	dh_auto_configure -- \
	  --libexecdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
	  --with-lispdir=\$${prefix}/share/emacs/site-lisp \
	  --enable-virtual-domains --enable-mh --enable-python \
	  --with-gnutls --with-kyotocabinet --with-gssapi --with-gsasl \
	  --with-guile --with-mysql --disable-rpath --disable-static \
	  --disable-cxx --disable-nntp

override_dh_auto_build:
	dh_auto_build
	$(MAKE) -C doc html

override_dh_auto_install:
	dh_auto_install
	# These binaries conflict with elm-me. We need to rename them, as
	# we're using alternatives for them.
	cd debian/tmp; \
	for i in dotlock frm from messages mail movemail readmsg; do \
	   mv usr/bin/$$i usr/bin/$$i.mailutils; \
	done

	# Generate manpages, but remove the s[ug]id bits from binaries
	# so LD_LIBRARY_PATH works.
	chmod 755 $(CURDIR)/debian/tmp/usr/bin/dotlock.mailutils
	sh debian/mangen.sh $(CURDIR)/debian/tmp/usr
	# Restore correct perms to the binaries that need it.
	chmod 2755 $(CURDIR)/debian/tmp/usr/bin/dotlock.mailutils

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) check RUNTESTFLAGS="$(RUNTESTFLAGS)" || true
endif

override_dh_fixperms:
	dh_fixperms -Xdotlock.mailutils
