#!/usr/bin/make -f

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

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

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
export QT_SELECT := qt5

# Retrieve packages that have a .install-common file
pkgs_with_common = $(patsubst debian/%.install-common,%,$(wildcard debian/*.install-common))
# Retrieve packages that have a .install-$DEB_HOST_ARCH file
pkgs_with_arch = $(patsubst debian/%.install-$(DEB_HOST_ARCH),%,$(wildcard debian/*.install-$(DEB_HOST_ARCH)))

%:
	dh $@ --with pkgkde_symbolshelper

override_dh_auto_configure:
	qmake QT_BUILD_PARTS+=tests

override_dh_auto_build-indep:
	dh_auto_build -Smakefile -- docs

override_dh_auto_test-arch:
ifeq ($(DEB_HOST_ARCH), armhf)
# Don't run the tests on armhf, they're spotty.
else
	$(MAKE) install -Csrc/imports INSTALL_ROOT=$(CURDIR)/test_root
	QT_LOGGING_RULES="default.debug=true" \
		xvfb-run -a -s "-screen 0 1024x768x24 +extension RANDR +extension RENDER +extension GLX" \
		dh_auto_test -- -Ctests/auto/unit QML2_IMPORT_PATH=$(CURDIR)/test_root/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/qml
endif

# Do not attempt to run tests during indep build
override_dh_auto_test-indep:

override_dh_auto_install-arch:
	dh_auto_install

	# Remove private headers.
	rm -rfv debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/qt5/QtMultimedia/*/QtMultimedia/private
	rm -rfv debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/qt5/QtMultimediaWidgets/*/QtMultimediaWidgets/private
	rm -rfv debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/qt5/QtMultimediaQuick_p

	# Remove some other private stuff.
	rm -fv debian/tmp/usr/lib/*/qt5/mkspecs/modules/qt_lib_multimedia_private.pri
	rm -fv debian/tmp/usr/lib/*/qt5/mkspecs/modules/qt_lib_qtmultimediaquicktools_private.pri
	rm -fv debian/tmp/usr/lib/*/qt5/mkspecs/modules/qt_lib_multimediawidgets_private.pri
	
	# Fix wrong path in pkgconfig files
	find $(CURDIR)/debian/tmp/usr/lib/*/pkgconfig -type f -name '*.pc' \
	-exec sed -i -e 's/$(DEB_HOST_MULTIARCH)\/$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/g' {} \;
	
	# Remove libtool-like files
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la

	# Remove CMake files for plugins, we don't need them.
	rm -fv debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qt5Multimedia/*Plugin.cmake

	# Remove empty directories.
	find debian/tmp -type d -empty -delete

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

override_dh_install:
	# Now flush all the .install-* files as .install files.
	set -e; for pkg in $(pkgs_with_common); do \
		cat debian/$$pkg.install-common >> debian/$$pkg.install ; \
	done

	set -e; for pkg in $(pkgs_with_arch); do \
		cat debian/$$pkg.install-$(DEB_HOST_ARCH) >> debian/$$pkg.install ; \
	done

	# Call dh_install normally. It will process .install, .install.ARCH
	# and/or .install.OS files.
	dh_install --fail-missing

override_dh_clean:
	dh_clean

	# Don't forget to remove the .install files we generated.
	for pkg in $(pkgs_with_common); do \
		rm -f debian/$$pkg.install ; \
	done

	for pkg in $(pkgs_with_arch); do \
		rm -f debian/$$pkg.install ; \
	done

override_dh_strip:
	dh_strip --dbgsym-migration='qtmultimedia5-dbg (<< 5.7.0-1~)'
