#!/usr/bin/make -f

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

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

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

# specify SONAME version of libtgvoip
export SOVER = 1.0

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

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

override_dh_auto_configure:
	gyp $(GYPFLAGS)
	dh_auto_configure

override_dh_auto_install:
	mkdir -p debian/libtgvoip$(SOVER)/usr/lib/$(DEB_HOST_MULTIARCH)
	ln $(OUT)/lib.target/lib*.so.* \
		debian/libtgvoip$(SOVER)/usr/lib/$(DEB_HOST_MULTIARCH)
	mkdir -p debian/libtgvoip-dev/usr/lib/$(DEB_HOST_MULTIARCH)
	ln -s libtgvoip.so.$(SOVER) \
		debian/libtgvoip-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libtgvoip.so
	dh_auto_install

override_dh_auto_clean:
	dh_auto_clean || dh_auto_clean --sourcedirectory=$(CURDIR)

# Upstream does not provide a tarball
get-orig-source:
	git clone --depth=1 https://github.com/grishka/libtgvoip
	tar --exclude-vcs -cJf ../libtgvoip_$(SOVER)~git$$(cd libtgvoip && \
		git log -1 --date="format:%Y%m%d" --format="%cd.%h").orig.tar.xz \
		libtgvoip
	rm -rf libtgvoip
