#!/usr/bin/make -f
# -*- makefile -*-
include /usr/share/dpkg/default.mk

EXTRA_CMAKE_VARIABLES += \
	TDESKTOP_API_ID=50322 \
	TDESKTOP_API_HASH=9ff1a639196c0779c86dd661af8522ba \
	TDESKTOP_RESOURCES=Packed \
	DESKTOP_APP_USE_PACKAGED_QRCODE=ON \
	CMAKE_SKIP_INSTALL_ALL_DEPENDENCY=ON

ifeq ($(filter terse,$(DEB_BUILD_OPTIONS)),)
 # Print entire arguments of moc invocation.
 EXTRA_CMAKE_VARIABLES += CMAKE_AUTOGEN_VERBOSE=ON
endif

ifeq ($(filter noopt,$(DEB_BUILD_OPTIONS)),)
 # Hide warnings useless for final build.
 export DEB_CXXFLAGS_MAINT_APPEND = -Wno-deprecated-declarations
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
 QTWAYLAND_VERSION := $(shell dpkg-query -Wf '$${source:Upstream-Version}' libqt5waylandclient5-dev)
 EXTRA_CMAKE_VARIABLES += DESKTOP_APP_QTWAYLANDCLIENT_PRIVATE_HEADERS=$(CURDIR)/debian/qtwayland/$(QTWAYLAND_VERSION)
else
 # Make sure Qt WaylandClient plugin is not in use.
 export DEB_CPPFLAGS_MAINT_APPEND = -UQT_WAYLANDCLIENT_LIB
endif

ifeq ($(DEB_HOST_ARCH_BITS),32)
 # Less debug info by default to fit small address space.
 ifneq ($(filter -g,$(CXXFLAGS)),)
  export DEB_CXXFLAGS_MAINT_STRIP = -g
  export DEB_CXXFLAGS_MAINT_PREPEND = -g1
 endif
endif

CPPLIBS_PACKAGES = libexpected-dev libmapbox-variant-dev libmsgsl-dev librange-v3-dev libtgvoip-dev
EXTRA_SUBSTVARS += \
	cpplibs:Built-Using=$(shell dpkg-query -Wf '$${source:Package}(=$${Version}),' $(CPPLIBS_PACKAGES))

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Make debugging a little easier. https://stackoverflow.com/a/25817631/5000805
print-%:
	@echo $* = $($*)

%:
	dh $@

override_dh_auto_configure:
	ln -s -f -n -r debian Telegram/ThirdParty
	dh_auto_configure -- $(EXTRA_CMAKE_VARIABLES:%=-D%)

# Remove symbolic link for access to StatusNotifierItem module.
execute_after_dh_auto_clean:
	rm -f Telegram/ThirdParty

override_dh_installchangelogs:
	dh_installchangelogs changelog.txt

# Double up the limit of debug information entries.
override_dh_dwz:
	dh_dwz -- -L 100000000

override_dh_gencontrol:
	dh_gencontrol -- $(EXTRA_SUBSTVARS:%="-V%")

QTW_SRC = ../qtwayland-opensource-src-$(QTWAYLAND_VERSION)
QTW_DIR = $(CURDIR)/debian/qtwayland
JOBS_PARAM = $(or $(filter -j%,$(MAKEFLAGS)),-j$(shell nproc || echo 1))
update-qtwayland:
	test x$(QTWAYLAND_VERSION) != x  # check installed Qt version for deleting correct directory
	test -d $(QTW_SRC) || ( cd .. && apt-get source qtwayland-opensource-src )
	cd $(QTW_SRC) && qmake QT_TOOL.qtwaylandscanner.binary=/usr/lib/qt5/bin/qtwaylandscanner
	make -C $(QTW_SRC) $(JOBS_PARAM) qmake_all
	make -C $(QTW_SRC)/src/client $(JOBS_PARAM) install_private_headers INSTALL_ROOT=$(QTW_DIR)/tmp
	rm -rf $(QTW_DIR)/$(QTWAYLAND_VERSION)
	mv $(QTW_DIR)/tmp/usr/include/*/qt5/QtWaylandClient/*/ $(QTW_DIR)/
	rm -r $(QTW_DIR)/tmp
