#!/usr/bin/make -f

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

include /usr/share/dpkg/architecture.mk

export QT_SELECT := qt5

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

# Do build the HelloWorld plugin, as it is not needed
export QTC_BUILD_PLUGIN_HELLOWORLD=FALSE
# Do not build the ClangFormat plugin, as it requires clang-format with
# a patch not accepted yet (and available only in the QtC fork):
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=939825#49
# https://bugreports.qt.io/browse/QTCREATORBUG-22923
# https://reviews.llvm.org/D53072
export QTC_BUILD_PLUGIN_CLANGFORMAT=FALSE

# The clang-N used for the build by the unversioned clang
current_clang_pkg = $(shell dpkg -S "$(shell realpath $(shell env PATH=$$(echo "$$PATH" | sed -r -e 's|/usr/lib/ccache/?:||') which clang))" | cut -d: -f1)
# The upstream version of a package passed as parameter
upstream_version_of_pkg = $(shell dpkg-query -f '$${Version}' -W $(1) | sed -e 's/-[^-]*$$//')

%:
	dh $@ --builddirectory=builddir

override_dh_auto_configure:
	dh_auto_configure --buildsystem=cmake -- \
		-DBUILD_DEVELOPER_DOCS=ON \
		-DBUILD_QBS=OFF \
		-DWITH_DOCS=$(if $(filter qtcreator-doc, $(shell dh_listpackages)),ON,OFF)

execute_after_dh_auto_build-indep:
	dh_auto_build -- docs

execute_after_dh_auto_install:
ifneq (,$(filter qtcreator-doc, $(shell dh_listpackages)))
	DESTDIR=$(CURDIR)/debian/tmp cmake --install $(CURDIR)/builddir --component qch_docs
	DESTDIR=$(CURDIR)/debian/tmp cmake --install $(CURDIR)/builddir --component html_docs
endif

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

	# Remove extra exec permissions
	find debian/tmp -name qmldir -type f -exec chmod -x {} \;

execute_after_dh_install:
	# Copied in qtcreator-doc already
	rm -rf debian/qtcreator-data/usr/share/qtcreator/doc/

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

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/$(DEB_HOST_MULTIARCH)/qtcreator/

override_dh_shlibdeps:
	echo 'libQt5DesignerComponents 5 libqt5designercomponents5 (>= $(call upstream_version_of_pkg,libqt5designercomponents5)~)' > 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=$(current_clang_pkg) (>= $(call upstream_version_of_pkg,$(current_clang_pkg))~)'
