#!/usr/bin/make -f
# -*- makefile -*-

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

PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))

# ABI major version
somajor:=$(shell perl -ne 'print $$1 if m{libvsqlitepp.so.(\d+)};' Makefile.am)
# The base library name
plib=$(PKG)$(somajor)
pdev=$(PKG)-dev
pdoc=$(PKG)-doc
pdbg=$(PKG)-dbg

# Ensure the build aborts when there are still references to undefined
# symbols: LDFLAGS += -Wl,-z,defs

# Make the linker work a bit harder so dynamic loading can be done
# faster: #LDFLAGS += -Wl,-O1

export DEB_LDFLAGS_MAINT_APPEND=-Wl,-z,defs -Wl,-O1

%:
	dh $@ --parallel --with autoreconf

override_dh_clean:
	$(RM) -r html
	dh_clean

override_dh_autoreconf: $(info I: library-name=$(plib))
	dh_autoreconf

override_dh_auto_test:
	dh_auto_test
	# Run mini-test
	./vsqlitepp_example

override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/tmp

override_dh_install:
	dh_install -p$(plib) --autodest \
            usr/lib/*/lib*.so.*
	dh_install -p$(pdev) --autodest \
            usr/include/*/*             \
            usr/lib/*/lib*.so

html:
	doxygen
	$(RM) -v html/*.md5 html/*.map
	# De-duplication in "html" folder:
	echo $$(hardlink --verbose --dry-run html \
                | grep \\[DryRun\\] \
                | awk '{print "ln -sfv","$(CURDIR)/"$$3,$$5,";"}') \
        | sh -v
	symlinks -rc html

override_dh_installdocs: html
	dh_installdocs -p$(pdev) --link-doc=$(plib)
	dh_installdocs

override_dh_makeshlibs:
	dh_makeshlibs -v -V

CHK_FILES=debian/$(plib)
$(CHK_FILES):
	@echo "E: invalid library name -- expected \"$@\"."
	false

override_dh_gencontrol: $(CHK_FILES)
	dh_gencontrol -v -- -Vplib=$(plib) -Vapiver=$(apiver)

# gracefully handle stripping if -dbg package (un-)commented in debian/control
override_dh_strip:
	[ -d "$(CURDIR)/debian/$(pdbg)" ] \
        && dh_strip --dbg-package=$(pdbg) \
        || dh_strip

override_dh_builddeb:
	dh_builddeb -- -Zxz

