#!/usr/bin/make -f

# https://pkg-perl.alioth.debian.org/debhelper.html#Occasionally_Useful
PACKAGE = $(shell dh_listpackages)
TMP     = $(CURDIR)/debian/$(PACKAGE)

# https://wiki.debian.org/HardeningWalkthrough
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

#export DH_VERBOSE=1

# Needed to pass the dh-generated buildflags to the configure script
export EXTRA_CFLAGS=$(CFLAGS) $(CPPFLAGS)
export EXTRA_LDFLAGS=$(LDFLAGS)

%:
	dh $@

override_dh_auto_clean:
ifneq ($(wildcard mk-config.mk),)
	# dh_auto_clean doesn't play well with s-nail's build system
	make distclean
endif

override_dh_auto_configure:
	# TODO: with s-nail >14.9.10 it should be possible to drop the OSENV override.
	# https://git.sdaoden.eu/cgit/s-nail.git/commit/?id=80b82c24e513ac462ed6e444a3423ab3f41746bb
	OSENV=debian $(MAKE) config \
		VERBOSE=1 \
		VAL_PREFIX=/usr \
		VAL_LIBEXECDIR=/usr/lib/s-nail \
		VAL_SYSCONFDIR=/etc \
		VAL_MTA=/usr/sbin/sendmail \
		VAL_MAIL=/var/mail

override_dh_fixperms:
	dh_fixperms

	# This could be avoided with VAL_PRIVSEP_GROUP=mail, but this
	# setting has been deprecated upstream.
	chgrp mail $(TMP)/usr/lib/s-nail/s-nail-privsep

override_dh_auto_test: export HOME=$(shell mktemp -d TESTHOME.XXXXXXXX)
override_dh_auto_test:
# Avoid override_dh_auto_test-does-not-check-DEB_BUILD_OPTIONS
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test
	rm -rf TESTHOME.*
endif

override_dh_installchangelogs:
	dh_installchangelogs NEWS
