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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
include /usr/share/dpkg/pkg-info.mk
export DEB_VERSION_UPSTREAM

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# On some arches (mips64el, s390x, alpha) antlr3 is extremely slow
# and throws a heap space OutOfMemory exception. Workaround by increasing
# heap space
export _JAVA_OPTIONS = -Xms512m -Xmx1024m

CONFIGURE_FEATURES += -DENABLE_UNIT_TESTS=OFF

# Seems useful enough to enable.
CONFIGURE_FEATURES += -DENABLE_MDNS=ON

# No one should need the static library, file a bug if you do.
CONFIGURE_FEATURES += -DBUILD_SHARED_LIBS=ON

%:
	dh $@ --buildsystem=cmake --no-parallel --without autoreconf

# Upstream unconditionally sets CMAKE_INSTALL_RPATH. Make it ineffective by
# setting CMAKE_SKIP_RPATH
# Disable tests as this would require bctoolbox to be built with tests
# enabled as well, which has additional dependencies not yet in Debian
override_dh_auto_configure:
# If src/sdp/sdp_grammar changes the version constraint of
# libbellesip3 on belle-sip data may need to be adjusted in d/control.
	md5sum --strict --warn --status -c debian/sdp.md5sum
# Only regenerate the lexer/parser files if faketime and antlr3 are
# enabled in d/control since Java is rather heavy.  To screen any
# differences clang-format-diff is recommended.
	if [ -x "$(shell command -v antlr3)" -a -x "$(shell command -v faketime)" ]; then \
		for gram in belle_sdp belle_sip_message; do \
	        ( cd src/antlr3c && \
		  rm -f ../grammars/$${gram}*.[ch] && \
	          faketime "$$(date -d "$$(dpkg-parsechangelog -SDate -l ../../debian/changelog)" +"%Y-%m-%d %H:%M:%S")" antlr3 -Xmultithreaded -Xconversiontimeout 10000 -fo ../grammars ../grammars/$${gram}.g \
		); \
		done; \
	fi
	dh_auto_configure -- -DCMAKE_SKIP_RPATH=ON \
		-DDEB_VERSION_UPSTREAM=$${DEB_VERSION_UPSTREAM%+dfsg*} \
		$(CONFIGURE_FEATURES)

execute_after_dh_auto_install:
	echo 'Requires.private: bctoolbox >= '$${DEB_VERSION_UPSTREAM%+dfsg*}', belr >= '$${DEB_VERSION_UPSTREAM%+dfsg*} \
		>> debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/belle-sip.pc

# No need for autoreconf as we use CMake
override_dh_autoreconf:
override_dh_autotools_update_config:
