#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

#!/usr/bin/make -f
# Ensure hardening flags match standard Debian profiles
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Explicitly force the legacy C++ standard to satisfy modern GCC
export DEB_CXXFLAGS_MAINT_APPEND = -std=c++11

# Force debhelper to execute all make operations sequentially using a single core
#export DEB_BUILD_OPTIONS = parallel=1

%:
	#dh $@ --with python3
	dh $@

override_dh_auto_configure:
	# Fix duplicate man page error by correcting noinst_PODS
	sed -i 's/vadduser.pod vadduser.pod/vadduser.pod/g' commands/Makefile.am
	if [ -f commands/Makefile.in ]; then \
		sed -i 's/vadduser.pod vadduser.pod/vadduser.pod/g' commands/Makefile.in; \
	fi

	sed -i -e 's@head -1@head -n 1@' scripts/vmailmgrd.run
	# Replace upstream occurrences of legacy python execution parameters
	sed -i -e 's@python -c@python3 -c@' python/Makefile*
	sed -i -e 's@python -O -c@python3 -O -c@' python/Makefile*
	# Passes modern C/C++ compilation flags and mirrors your ./configure
	dh_auto_configure -- --prefix=/usr

override_dh_auto_build:
	# 1. Instruct debhelper to back up the file and restore it on dh_clean
	dh_assistant restore-file-on-clean doc/HOWTO.texi
	
	# 2. Your fix to resolve the fild text encoding error (e.g., stripping bad bytes or iconv)
	iconv -f iso-8859-1 -t utf-8 doc/HOWTO.texi > doc/HOWTO.texi.tmp && mv doc/HOWTO.texi.tmp doc/HOWTO.texi

	# Explicitly list subdirectories to build, completely bypassing the 'doc' folder
	#dh_auto_build -- all SUBDIRS="authenticate commands daemon cgi php"
	dh_auto_build -- all CFLAGS="$$(dpkg-buildflags --get CFLAGS) -fPIC -lcrypt";
	
	# Explicitly pass the modern binary down to the python directory's Makefile
	#dh_auto_build -- all PYTHON=python3
	
	#if [ "$(DEB_HOST_ARCH)" = "amd64" ]; then \
	#	dh_auto_build -- -f Makefile.checkvpw-dovecot-wrapper prefix=/usr CFLAGS="$$(dpkg-buildflags --get CFLAGS) -fPIC"; \
	#else \
	#	dh_auto_build -- -f Makefile.checkvpw-dovecot-wrapper prefix=/usr; \
	#fi

override_dh_auto_install:
	# Make directories that your Makefile installation scripts require
	mkdir -p debian/vmailmgr/usr/share/vmailmgr/php \
	         debian/vmailmgr/usr/share/vmailmgr/cgi
	
	# Pass python3 site-packages destination path to match standard Debian layout
	# Mirroring your explicit make install mapping
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp \
		prefix=/usr \
		bindir=/usr/bin \
		datarootdir=/usr/share \
		cgidir=/usr/share/vmailmgr/cgi \
		phpdir=/usr/share/vmailmgr/php \
		pythonlibdir=/usr/lib/python3/dist-packages/vmailmgr \
		install-strip

	mkdir -p $(CURDIR)/debian/vmailmgr-daemon/var/qmail/service/vmailmgrd/log
	install -m 755 scripts/vmailmgrd.run $(CURDIR)/debian/vmailmgr-daemon/var/qmail/service/vmailmgrd/run
	install -m 755 scripts/vmailmgrd-log.run $(CURDIR)/debian/vmailmgr-daemon/var/qmail/service/vmailmgrd/log/run
	mkdir -p $(CURDIR)/debian/tmp/etc/vmailmgr
	echo users > $(CURDIR)/debian/tmp/etc/vmailmgr/user-dir
	echo passwd > $(CURDIR)/debian/tmp/etc/vmailmgr/password-file
	echo ./Maildir/ > $(CURDIR)/debian/tmp/etc/vmailmgr/default-maildir
	echo maildir > $(CURDIR)/debian/tmp/etc/vmailmgr/maildir-arg-str
	echo /var/qmail/service/vmailmgrd/socket > $(CURDIR)/debian/tmp/etc/vmailmgr/socket-file

	# Mirroring your explicit make install mapping
	#$(MAKE) -f Makefile.checkvpw-dovecot-wrapper prefix=/usr DESTDIR=$(CURDIR)/debian/tmp install

override_dh_install:
	# Let standard dh_install handle files configured in vmailmgr.install
	dh_install

override_dh_fixperms:
	dh_fixperms
	# Ensure scripts inside your qmail structure retain executable permissions
	chmod 0755 debian/vmailmgr-daemon/var/qmail/service/vmailmgrd/run
	chmod 0755 debian/vmailmgr-daemon/var/qmail/service/vmailmgrd/log/run
	#chmod 0755 debian/tmp/usr/bin/courier-dovecot-migrate.pl

override_dh_auto_clean:
	# Run the standard upstream clean if a Makefile exists
	[ ! -f Makefile ] || dh_auto_clean
	
	# Clean up the custom dovecot wrapper build objects
	[ ! -f Makefile.checkvpw-dovecot-wrapper ] || $(MAKE) -f Makefile.checkvpw-dovecot-wrapper clean || true
	
	# Revert the sed modifications to the run script so git/quilt stays clean
	if [ -f scripts/vmailmgrd.run.bak ]; then \
		mv scripts/vmailmgrd.run.bak scripts/vmailmgrd.run; \
	fi
	
	# Remove files copied manually into the root directory during the build
	#rm -f checkvpw-dovecot-wrapper.c Makefile.checkvpw-dovecot-wrapper
