#!/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 \
	CMAKE_SKIP_INSTALL_ALL_DEPENDENCY=ON

# Built against package rlottie is currently broken due to incorrect order of
# macro definitions. We try to fix the issue here.
DEB_CPPFLAGS_MAINT_APPEND += -DDESKTOP_APP_USE_PACKAGED_RLOTTIE

# Reveal any errors with pre-compiled headers.
DEB_CPPFLAGS_MAINT_APPEND += -Werror=invalid-pch

# Print full stack trace of template instantiations.
DEB_CXXFLAGS_MAINT_APPEND += -ftemplate-backtrace-limit=0

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.
 DEB_CXXFLAGS_MAINT_APPEND += -Wno-deprecated-declarations
endif

# Unconditionally ignore Qt WaylandClient plugin.
EXTRA_CMAKE_VARIABLES += DESKTOP_APP_DISABLE_WAYLAND_INTEGRATION=ON
DEB_CPPFLAGS_MAINT_APPEND += -UQT_WAYLANDCLIENT_LIB

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
 # Attempt to reduce RAM consumption even more.
 export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-keep-memory
endif

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CPPFLAGS_MAINT_APPEND DEB_CXXFLAGS_MAINT_APPEND

# 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 and tgcalls modules.
execute_after_dh_auto_clean:
	rm -f Telegram/ThirdParty

override_dh_installchangelogs:
	dh_installchangelogs changelog.txt

# Maximize the limit of debug information entries, ignore possible errors.
override_dh_dwz:
	-dh_dwz -- -L 250000000

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