#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

%:
	dh $@

override_dh_auto_configure:
	# Backup original vanilla files if backups don't exist yet
	[ -f conf-cc.orig ] || cp conf-cc conf-cc.orig
	[ -f conf-ld.orig ] || cp conf-ld conf-ld.orig
	[ -f conf-qmail.orig ] || cp conf-qmail conf-qmail.orig
	[ -f conf-spawn.orig ] || cp conf-spawn conf-spawn.orig
	[ -f Makefile.orig ] || cp Makefile Makefile.orig
	[ -f chkshsgr.c.orig ] || cp chkshsgr.c chkshsgr.c.orig
	[ -f error.h.orig ] || cp error.h error.h.orig
	[ -f dns.c.orig ] || cp dns.c dns.c.orig
	[ -f cdb_seek.c.orig ] || cp cdb_seek.c cdb_seek.c.orig

	# Reference: https://salsa.debian.org/debian/ucspi-tcp/-/blob/master/debian/rules
	# Bypass the following error:
	# Oops. Your getgroups() returned 0, and setgroups() failed; this means
	# that I can't reliably do my shsgr test. Please either ``make'' as root
	# or ``make'' while you're in one or more supplementary groups.
	echo 'int main () { return 0; }' > chkshsgr.c

	# Replace all string head -1 to head -n 1 in Makefile
	sed -i -e "s/head -1/head -n 1/g" Makefile

	fds=`ulimit -n`; \
	spawnlimit=$$(( (fds - 6) / 2 )); \
	echo $$spawnlimit > conf-spawn

	# Pass Debian hardening tools into compiler configuration structures
	echo "$(CC) $(CFLAGS) $(CPPFLAGS) -D_GNU_SOURCE -O2 -DINET6 -DTCPTO_REFUSED -Wno-incompatible-pointer-types" > conf-cc
	echo "$(CC) $(LDFLAGS) -s -g -fPIE -Wl,--no-as-needed -lresolv" > conf-ld
	echo "/var/qmail" > conf-qmail


override_dh_auto_build:
	# Compile standard target binaries and documentation files
	#$(MAKE) syncdir it man
	$(MAKE) compile makelib
	./compile syncdir.c
	./makelib libsyncdir.a syncdir.o
	$(MAKE) it man

override_dh_auto_install:
	# Create the files locally in the root source dir so ./install can read them
	mkdir -p owners
	./make-owners .

	# Generate staging build paths 
	mkdir -p $(CURDIR)/debian/qmail/var/qmail
	mkdir -p $(CURDIR)/debian/qmail/etc/qmail

	install -d $(CURDIR)/debian/qmail/var/qmail/owners
	./make-owners debian/qmail/var/qmail

	# INSTALL IT
	./install $(CURDIR)/debian/qmail/var/qmail
	# CHECK IT
	./instcheck $(CURDIR)/debian/qmail/var/qmail

	# Rename tcp-environ.5 to qmail-tcp-environ.5 as it will conflict with ucspi-tcp6 package
	mv $(CURDIR)/debian/qmail/var/qmail/man/man5/tcp-environ.5 $(CURDIR)/debian/qmail/var/qmail/man/man5/qmail-tcp-environ.5

	# Clean out duplicate documentation inside var/ before debhelper compresses it into /usr/share/doc
	rm -rf $(CURDIR)/debian/qmail/var/qmail/doc
	# Remove preformatted man pages
	#rm -rf $(CURDIR)/debian/qmail/var/qmail/man/cat*
	#rm -rf $(CURDIR)/debian/qmail/var/qmail/man

	# Install some extra configuration programs
	install ipmeprint $(CURDIR)/debian/qmail/var/qmail/bin

	# /var/qmail/control
	install -d $(CURDIR)/debian/qmail/var/qmail/control

	# /var/qmail/users
	install -d $(CURDIR)/debian/qmail/var/qmail/users

# This target runs under fakeroot safely handles system account mappings
override_dh_fixperms:

	dh_fixperms
	# Safely apply custom qmail ownership requirements
	#chown root:nofiles   $(CURDIR)/debian/qmail/var/qmail/owners/gidn
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail/owners/gidq
	#chown alias:qmail    $(CURDIR)/debian/qmail/var/qmail/owners/uida
	#chown qmaild:qmail   $(CURDIR)/debian/qmail/var/qmail/owners/uidd
	#chown qmaill:qmail   $(CURDIR)/debian/qmail/var/qmail/owners/uidl
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail/owners/uido
	#chown qmailp:qmail   $(CURDIR)/debian/qmail/var/qmail/owners/uidp
	#chown qmailq:qmail   $(CURDIR)/debian/qmail/var/qmail/owners/uidq
	#chown qmailr:qmail   $(CURDIR)/debian/qmail/var/qmail/owners/uidr
	#chown qmails:qmail   $(CURDIR)/debian/qmail/var/qmail/owners/uids
	# Fixing ownership and permission
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail
	# Standard subdirectories ownership
	#chown alias:nofiles  $(CURDIR)/debian/qmail/var/qmail/alias
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail/bin
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail/boot
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail/man
	#chown qmaill:nofiles $(CURDIR)/debian/qmail/var/qmail/users
	#chown qmailq:qmail   $(CURDIR)/debian/qmail/var/qmail/queue
	# Set up specific binary file permission protections
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail/bin/*
	#chown qmailq:qmail   $(CURDIR)/debian/qmail/var/qmail/bin/qmail-queue
	#chmod 4711           $(CURDIR)/debian/qmail/var/qmail/bin/qmail-queue
	# Map control folder permissions
	#chown root:qmail     $(CURDIR)/debian/qmail/var/qmail/control
	#chmod 0755           $(CURDIR)/debian/qmail/var/qmail/control
	bash debian/contrib/fix-qmail-perms.sh $(CURDIR)/debian/qmail/var/qmail

override_dh_dwz:
	dh_dwz -Xvar/qmail

override_dh_strip:
	dh_strip -Xvar/qmail

override_dh_auto_test:
	@echo "Skipping upstream check target..."

override_dh_auto_clean:
	# 1. Revert compilation settings back to original vanilla states
	[ ! -f conf-cc.orig ] || mv conf-cc.orig conf-cc
	[ ! -f conf-ld.orig ] || mv conf-ld.orig conf-ld
	[ ! -f conf-qmail.orig ] || mv conf-qmail.orig conf-qmail
	[ ! -f conf-spawn.orig ] || mv conf-spawn.orig conf-spawn
	[ ! -f Makefile.orig ] || mv Makefile.orig Makefile
	[ ! -f chkshsgr.c.orig ] || mv chkshsgr.c.orig chkshsgr.c
	[ ! -f error.h.orig ] || mv error.h.orig error.h
	[ ! -f dns.c.orig ] || mv dns.c.orig dns.c
	[ ! -f cdb_seek.c.orig ] || mv cdb_seek.c.orig cdb_seek.c

	# 2. Mimic a clean sequence manually since vanilla qmail lacks 'make clean'
	rm -f *.o TARGETS instcheck install sharp bangocsi auto-str \
	thash find-systype make-compile make-load tryssl.c \
	qmail-users qmail-local qmail-lspawn qmail-getpw qmail-remote \
	qmail-rspawn qmail-clean qmail-send qmail-start qmail-newu \
	qmail-newmalias qmail-popup qmail-pop3d qmail-inject qmail-showctl \
	qmail-newu qmail-pw2u splogger qmail-queue qmail-todo predate \
	datemail mailsubj qmail-upq qmail-showctl qmail-tcpok qmail-tcpto \
	addresses remoteinfo timeoutread timeoutwrite timeoutconn

	[ ! -d owners ] || rm -rf owners
