#!/usr/bin/make -f

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

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

%:
	dh $@


# 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)

# 1. Before configuring, back up any pre-existing upstream conf-* files
execute_before_dh_auto_configure:
	for file in conf-*; do \
		if [ -f "$$file" ] && [ ! -f "$$file.orig" ]; then \
			cp "$$file" "$$file.orig"; \
		fi; \
	done
	# Now apply your custom debian paths safely
	echo "/usr/include/bglibs" > conf-bgincs
	echo "/usr/lib/$(DEB_HOST_MULTIARCH)/bglibs" > conf-bglibs
	echo "/usr/include/mailfront" > conf-mailfrontincs
	echo "/usr/lib/$(DEB_HOST_MULTIARCH)/mailfront" > conf-modules
	echo "gcc $(CFLAGS) $(CPPFLAGS) -D_GNU_SOURCE" > conf-cc
	echo "gcc $(CFLAGS) $(CPPFLAGS) -fPIC -shared -D_GNU_SOURCE" > conf-ccso
	echo "gcc $(LDFLAGS) -rdynamic -fPIE -Wl,--no-as-needed -lresolv" > conf-ld
	echo "/usr/bin" > conf-bin

# 2. When cleaning up, restore original configuration structures
execute_after_dh_auto_clean:
	# Restore backed up configurations if they exist
	for file in conf-*.orig; do \
		if [ -f "$$file" ]; then \
			mv "$$file" "$${file%.orig}"; \
		fi; \
	done
	# Remove any newly generated conf-* targets that didn't have an original backup
	# (Matches what your RPM %prep block generated)
	rm -f conf-include

# Override the install stage to feed the target directory to bg-installer
override_dh_auto_install:
	dh_auto_install -- install_prefix=$(CURDIR)/debian/tmp
