#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

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

include /usr/share/dpkg/pkg-info.mk
ver := $(DEB_VERSION_UPSTREAM)
ver_major := $(firstword $(subst ., ,$(ver)))

export DEB_CFLAGS_MAINT_APPEND = -Wall -D__NO_STRING_INLINES \
	$(if $(filter $(DEB_HOST_ARCH_BITS),32),-D_FILE_OFFSET_BITS=64)

MUTABLE_FILES := conf.h.in configure docs/dmalloc.html docs/dmalloc.pdf

%:
	dh $@

override_dh_update_autotools_config:
	dh_update_autotools_config
	for f in $(MUTABLE_FILES); do \
		cp -an $$f $$f.orig; \
	done

override_dh_clean:
	dh_clean -X.orig
	for f in $(MUTABLE_FILES); do \
		test ! -f $$f.orig || mv -f $$f.orig $$f; \
	done

override_dh_auto_configure:
	dh_auto_configure -- --enable-threads --enable-shlib

override_dh_auto_build:
	dh_auto_build
	cd docs && $(MAKE) dmalloc.info \
			&& makeinfo --html --no-split dmalloc.texi \
			&& texi2pdf dmalloc.texi

override_dh_auto_test:
	$(MAKE) heavy

override_dh_auto_install:
	$(MAKE) install prefix=$(CURDIR)/debian/tmp/usr

	# Creating symlinks
	cd $(CURDIR)/debian/tmp/usr/lib/* && \
		ln -s libdmalloc.so.$(ver) libdmalloc.so.$(ver_major) && \
		ln -s libdmalloc.so.$(ver) libdmalloc.so
	cd $(CURDIR)/debian/tmp/usr/lib/* && \
		ln -s libdmallocth.so.$(ver) libdmallocth.so.$(ver_major) && \
		ln -s libdmallocth.so.$(ver) libdmallocth.so
	cd $(CURDIR)/debian/tmp/usr/lib/* && \
		ln -s libdmalloccxx.so.$(ver) libdmalloccxx.so.$(ver_major) && \
		ln -s libdmalloccxx.so.$(ver) libdmalloccxx.so
	cd $(CURDIR)/debian/tmp/usr/lib/* && \
		ln -s libdmallocthcxx.so.$(ver) libdmallocthcxx.so.$(ver_major) && \
		ln -s libdmallocthcxx.so.$(ver) libdmallocthcxx.so

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog.1

override_dh_compress:
	dh_compress -Xusr/share/doc/libdmalloc-dev/examples

override_dh_makeshlibs:
	ln -sf libdmalloc5.symbols.cxx.$(DEB_HOST_ARCH_BITS)bit \
		debian/libdmalloc5.symbols.cxx
	dh_makeshlibs
