#!/usr/bin/make -f

DH_VERBOSE = 1

UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p' | sed -e 's/\+.*//; s/^[0-9]://')

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_BUILD_ARCH     ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# main packaging script based on dh7 syntax
%:
	dh $@ --with pkgkde_symbolshelper --parallel

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DSFCGAL_BUILD_TESTS=ON

override_dh_auto_test:
ifneq (,$(findstring $(DEB_BUILD_ARCH),"alpha i386 kfreebsd-i386 hurd-i386"))
	# Skip tests
else ifneq (,$(findstring $(DEB_BUILD_ARCH),"mips mipsel"))
	dh_auto_test || echo "Ignoring test failures"
else
	dh_auto_test
endif

override_dh_install:	
	# removing embedded rpath in binaries
	-find $(CURDIR)/debian/tmp/usr/bin -type f -not -name sfcgal-config -exec chrpath --delete {} \;

	# removing embedded rpath in libraries
	-find $(CURDIR)/debian/tmp/usr/lib -name "*.so*" -type f -exec chrpath --delete {} \;

	# Remove .la files
	$(RM) $(CURDIR)/debian/tmp/usr/lib/libSFCGAL.la

	# Remove test executables
	$(RM) $(CURDIR)/debian/tmp/usr/bin/*test*

	dh_install --list-missing

override_dh_installchangelogs:
	dh_installchangelogs NEWS
	
override_dh_makeshlibs:
	dh_makeshlibs -- -c0 -v$(UPSTREAM_VERSION)

override_dh_strip:
	dh_strip --dbg-package=sfcgal-dbg

