#!/usr/bin/make -f

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

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

export QT_SELECT := qt5
export LLVM_INSTALL_DIR = $(shell llvm-config --prefix)

# Upstream changelog
upstream_changes := $(wildcard dist/changes-*)

%:
	dh $@ --builddirectory=builddir

override_dh_auto_configure:
	dh_auto_configure --buildsystem=qmake -- \
		-recursive \
		QBS_INSTALL_DIR=/usr \
		LLVM_INSTALL_DIR="$(LLVM_INSTALL_DIR)" \
		KSYNTAXHIGHLIGHTING_LIB_DIR="/usr/lib/$(DEB_HOST_MULTIARCH)/" \
		KSYNTAXHIGHLIGHTING_INCLUDE_DIR="/usr/include/KF5/KSyntaxHighlighting/" \
		CONFIG+="disable_external_rpath" \
		$(CURDIR) IDE_LIBRARY_BASENAME=lib/$(DEB_HOST_MULTIARCH) IDE_PACKAGE_MODE=1 $(extra_configure_opts)

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp/usr
ifneq (,$(filter qtcreator-doc, $(shell dh_listpackages)))
	dh_auto_build -- qch_docs
	dh_auto_build -- install_inst_qch_docs install_inst_html_docs INSTALL_ROOT=$(CURDIR)/debian/tmp/usr
endif

	# Do not install the file, we better symlink it.
	rm -vf debian/tmp/usr/share/qtcreator/debugger/LICENSE.LGPL

	# Not needed
	rm debian/tmp/usr/bin/qtcreator.sh

override_dh_missing:
	dh_missing --fail-missing

override_dh_installdocs:
	dh_installdocs -pqtcreator-data $(upstream_changes)
	dh_installdocs --remaining-packages

override_dh_shlibdeps:
	echo 'libQt5DesignerComponents 5 libqt5designercomponents5 (>= $(shell dpkg-query -f '$${Version}' -W libqt5designercomponents5 | sed -e 's/-[^-]*$$//')~)' > debian/shlibs.local
	dh_shlibdeps -l$(CURDIR)/debian/qtcreator/usr/lib/$(DEB_HOST_MULTIARCH)/qtcreator:\
	$(CURDIR)/debian/qtcreator/usr/lib/$(DEB_HOST_MULTIARCH)/qtcreator/plugins/QtProject
	rm debian/shlibs.local

override_dh_gencontrol-arch:
	dh_gencontrol -a -- -V'qtcreator:clang=$(shell dpkg -S "$(shell realpath $(shell env PATH=$$(echo "$$PATH" | sed -r -e 's|/usr/lib/ccache/?:||') which clang))" | cut -d: -f1)'
