#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

DH_VERBOSE = 1

UPSTREAM_VERSION=$(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+.*//')

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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# reduce debug info to fix FTBFS on archs with few address space
ifneq (,$(filter $(DEB_HOST_ARCH),arm64 armhf mips mipsel ppc64el))
    CMAKECXXFLAGS="-DCMAKE_CXX_FLAGS_RELWITHDEBINFO=' -O2 -g1 -DNDEBUG'"
endif

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

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

override_dh_auto_test:
ifneq (,$(filter $(DEB_BUILD_ARCH),i386 alpha hurd-i386 kfreebsd-i386 riscv64 sparc64 x32))
	# Skip tests
else ifneq (,$(filter $(DEB_BUILD_ARCH),mips mips64el 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 --dbgsym-migration='sfcgal-dbg (<< 1.3.3)'
