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

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

export DEB_BUILD_MAINT_OPTIONS += hardening=+all
include /usr/share/dpkg/default.mk

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
 BUILD_MODE = Release
else
 BUILD_MODE = Debug
endif

DEFINITIONS += \
	DEBIAN_API_ID \
	TDESKTOP_DISABLE_AUTOUPDATE \
	TDESKTOP_DISABLE_CRASH_REPORTS \
	TDESKTOP_DISABLE_REGISTER_CUSTOM_SCHEME
export CPPFLAGS += $(DEFINITIONS:%=-D%) -Winvalid-pch

# Workaround for non-Linux, see bugs.debian.org/799356
ifneq ($(DEB_HOST_ARCH_OS),linux)
 EXTRA_GYP_FLAGS += --no-parallel
endif

%:
	dh $@ --sourcedirectory=out/$(BUILD_MODE)

override_dh_auto_configure:
	gyp --format=cmake --depth=Telegram/gyp --generator-output=../.. \
	  -Goutput_dir=out -Gconfig=$(BUILD_MODE) Telegram/gyp/Telegram.gyp \
	  $(EXTRA_GYP_FLAGS)
	@# Patch CMakeLists.txt instead gyp utility. $NUM is the number of the line
	@# before penultimate one in the generated CMakeLists.txt file. The follow
	@# sed script inserts all lines from debian/CMakeLists.inj file.
	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_auto_clean:
	dh_auto_clean || dh_auto_clean --sourcedirectory=$(CURDIR)
	rm -rf out
