#!/usr/bin/make -f

# DH_VERBOSE := 1

# Enable hardening build flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

CXXFLAGS += $(CPPFLAGS)

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

BUILD_DATE = $(shell LC_ALL=C date -u "+%d %B %Y" -d "@$(SOURCE_DATE_EPOCH)")

MANPAGES := $(wildcard debian/man/*.*.xml)

%:
	dh $@ --with python2,pkgkde_symbolshelper,sphinxdoc --parallel

override_dh_clean:
	if [ -e debian/gtest.config.guess ]; then \
		mv debian/gtest.config.guess vendor/gtest/build-aux/config.guess ; \
	fi
	if [ -e debian/gtest.config.sub ]; then \
		mv debian/gtest.config.sub   vendor/gtest/build-aux/config.sub ; \
	fi

	dh_clean debian/man/*.1

	$(RM) -r doc/html

override_dh_auto_configure:
	# Don't use outdated autotools helper files
	mv vendor/gtest/build-aux/config.guess debian/gtest.config.guess
	mv vendor/gtest/build-aux/config.sub   debian/gtest.config.sub

	cp /usr/share/misc/config.guess vendor/gtest/build-aux/config.guess
	cp /usr/share/misc/config.sub   vendor/gtest/build-aux/config.sub

	dh_auto_configure -- -DCMAKE_BUILD_TYPE=RelWithDebInfo \
	                     -DCMAKE_INSTALL_LIBDIR=lib \
	                     -DBUILD_PLUGIN_GREYHOUND=ON \
	                     -DBUILD_PLUGIN_ICEBRIDGE=ON \
	                     -DBUILD_PLUGIN_PYTHON=ON \
	                     -DBUILD_PLUGIN_SQLITE=ON \
	                     -DWITH_COMPLETION=ON

override_dh_auto_build:
	# Create man pages from DocBook XML
	for x in $(MANPAGES) ; do \
	  docbook2x-man --string-param header-3="$(BUILD_DATE)" $$x ; \
	  mv `basename $$x | sed 's/.xml$$//'` `dirname $$x` ; \
	done

	dh_auto_build

	sphinx-build doc/ doc/html/ && ln -s /usr/share/javascript/mathjax doc/html/_static/

override_dh_auto_test:
	# Exclude tests:
	# - pgpointcloudtest, requires configured PostgreSQL database
	# - pdal_spatial_reference_test, fails sometimes
	(cd obj-* && ctest --force-new-ctest-process --output-on-failure --exclude-regex "pgpointcloudtest|pdal_spatial_reference_test" || echo "Ignoring test failures")

override_dh_auto_install:
	dh_auto_install

	# Strip RPATH
	chrpath --delete debian/tmp/usr/lib/libpdal_plugin_*.so

override_dh_install:
	dh_install --list-missing

override_dh_python2:
	dh_python2 -plibpdal-plugin-python
	dh_numpy -plibpdal-plugin-python

override_dh_makeshlibs:
	dh_makeshlibs -- -v$(UPSTREAM_VERSION) -c0

