#!/usr/bin/make -f

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

export QT_SELECT=qt5
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export CFLAGS := $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

# Run tests only on archs known to pass in the past
testrun_architectures := amd64 arm64 armhf i386 ppc64el

%:
	dh $@ --fail-missing --parallel --with pkgkde_symbolshelper --dbg-package=qt3d5-dbg

override_dh_auto_configure:
	qmake QT_BUILD_PARTS+=tests

override_dh_auto_build-indep:
	dh_auto_build -- docs

override_dh_auto_install-arch:
	dh_auto_install
	
	# Remove libtool-like files
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la
	
	# Remove private stuff
	rm -rfv $(CURDIR)/debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/qt5/Qt3D*/5.*/Qt3D*/private
	rm -fv $(CURDIR)/debian/tmp/usr/lib/*/qt5/mkspecs/modules/qt_lib_*_private.pri
	
	# Remove cmake config files of plugins
	rm -fv $(CURDIR)/debian/tmp/usr/lib/*/cmake/Qt53DRenderer/Qt53DRenderer_*.cmake

	# Remove empty directories
	find $(CURDIR)/debian/tmp -type d -empty -delete

	# Fix wrong path in pkgconfig files
	find $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig -type f -name '*.pc' \
	-exec sed -i -e 's/$(DEB_HOST_MULTIARCH)\/$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/g' {} \;

override_dh_auto_install-indep:
	$(MAKE) INSTALL_ROOT=$(CURDIR)/debian/tmp install_docs

override_dh_auto_test-arch:
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),$(testrun_architectures)))
	xvfb-run -a -s "-screen 0 640x480x24" dh_auto_test -- QT_PLUGIN_PATH=$(CURDIR)/plugins QML2_IMPORT_PATH=$(CURDIR)/qml
else
	-xvfb-run -a -s "-screen 0 640x480x24" dh_auto_test -- QT_PLUGIN_PATH=$(CURDIR)/plugins QML2_IMPORT_PATH=$(CURDIR)/qml
endif

override_dh_auto_test-indep:
