#!/usr/bin/make -f
# -*- makefile -*-

# Rules for building Debian package. Based on the following files:
#  * docs/building-cmake.md
#  * Telegram/gyp/refresh.sh
#  * Telegram/Patches/gyp.diff
# Please recheck this file if they are changed in future versions.

include /usr/share/dpkg/default.mk
OUT = obj-$(DEB_HOST_GNU_TYPE)

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
 BUILD_MODE = Release
 ifneq ($(DEB_HOST_ARCH),mips64el)
  DEB_CXXFLAGS_MAINT_APPEND = -flto
  DEB_LDFLAGS_MAINT_APPEND = -flto
 endif
else
 BUILD_MODE = Debug
endif

GYPFLAGS += \
	--format=cmake \
	--depth=Telegram/gyp \
	--generator-output=../.. \
	-Duse_packed_resources \
	-Goutput_dir=$(OUT) \
	-Gconfig=$(BUILD_MODE)

EXTRA_MACROS += \
	DEBIAN_API_ID \
	RPL_CONSUMER_TYPE_ERASED_ALWAYS \
	TDESKTOP_DISABLE_AUTOUPDATE \
	TDESKTOP_DISABLE_CRASH_REPORTS \
	TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME

# Workarounds for non-Linux systems
ifneq ($(DEB_HOST_ARCH_OS),linux)
 EXTRA_MACROS += Q_OS_LINUX  # brilliant bluff :)
 GYPFLAGS += --no-parallel  # see bugs.debian.org/799356
endif

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CPPFLAGS_MAINT_APPEND += $(EXTRA_MACROS:%=-D%)
export DEB_CXXFLAGS_MAINT_APPEND += -Werror=invalid-pch
export DEB_LDFLAGS_MAINT_APPEND

# A generated CMake config will be placed in the next "source" directory.
%:
	dh $@ --sourcedirectory=$(OUT)/$(BUILD_MODE)

# We use a default source directory as fallback when an user tries to clear a
# clean tree or when the clean target is called twice in a row. In this case,
# debhelper says that the source directory doesn't exist so we omit the
# sourcedirectory parameter.
override_dh_auto_clean:
	dh_auto_clean || dh_auto_clean --sourcedirectory=$(CURDIR)
	rm -f Telegram/gyp/tests/*.test

# We paste the content of CMakeLists.inj into the generated CMakeLists.txt file
# instead of patching gyp utility. $NUM is the number of the line before
# penultimate one in CMakeLists.txt, it's the finish of the Telegram target in
# this file. The next sed script inserts all lines from debian/CMakeLists.inj file.
override_dh_auto_configure:
	gyp $(GYPFLAGS) Telegram/gyp/Telegram.gyp
	NUM=$$((`wc -l < $(OUT)/$(BUILD_MODE)/CMakeLists.txt` - 2)); \
	sed -i "$$NUM r debian/CMakeLists.inj" $(OUT)/$(BUILD_MODE)/CMakeLists.txt
	dh_auto_configure

override_dh_installchangelogs:
	dh_installchangelogs changelog.txt
