#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# linphone-app/CMakeLists.txt using PREPEND() probably results in goofy paths.
updirs=$(shell pwd|sed 's|[^/]\+|..|g;s|^/||')

ifeq (noopt,$(filter noopt,$(DEB_BUILD_PROFILES)))
export DEB_CFLAGS_MAINT_APPEND := -Og -ggdb -g3
export DEB_CXXFLAGS_MAINT_APPEND := -Og -ggdb -g3
endif

%:
	dh $@ --buildsystem=cmake --builddirectory=build

override_dh_auto_configure-arch:
	dh_auto_configure -- -DCMAKE_SKIP_RPATH=ON \
		-DGIT_EXECUTABLE=/usr/share/bctoolbox/upstream-version-from-changelog \
		-DLINPHONE_QT_ONLY=ON \
		-DENABLE_BUILD_VERBOSE=ON \
		-DENABLE_QT_KEYCHAIN=NO \
		-DENABLE_UPDATE_CHECK=NO \
		-DCMAKE_INSTALL_LIBDIR=lib

override_dh_auto_configure-indep:
	if ! [ -d build ]; then mkdir -p build; fi
	if ! [ -f build/Makefile ]; then printf 'all:\n\ttrue\n' > build/Makefile ; fi

execute_after_dh_auto_install-arch:
# make linphone runnable from build directory
	if [ "$(DEB_BUILD_MULTIARCH)" = "$(DEB_HOST_MULTIARCH)" ]; then patchelf --set-rpath $(CURDIR)/build/linphone-app build/linphone-app/linphone; fi
	if [ ! -e build/lib ]; then ln -s /usr/lib build/lib; fi # needed for mediastreamer2 to find its libmsqogl.so plugin
	if [ ! -h build/share ]; then if [ -d build/share ]; then rmdir build/share; fi ; ln -s /usr/share build/share; fi # needed for liblinphone to find rootca.pem
# Force debian/tmp into existence even with only one binary package
# declared since we rely on dh_install to move to packaging directory
# only what is mentioned in the .install files.
	if [ ! -d debian/tmp ]; then \
		mv -v debian/$(shell dh_listpackages) debian/tmp; \
	fi

execute_before_dh_auto_install-indep:
	if [ -d debian/tmp ]; then false; else true; fi # Error: debian/tmp exists, but shouldn't: clean up by hand and retry
# Force debian/tmp into existence even with only one binary package
# declared since we rely on dh_install to move to packaging directory
# only what is mentioned in the .install files.
	if [ ! -d debian/tmp ]; then \
		mv -v debian/$(shell dh_listpackages) debian/tmp; \
	fi

execute_before_dh_auto_install-arch:
	debian/shlib-collision-watchdog build/linphone-app/linphone
	if [ -d debian/tmp ]; then false; else true; fi # Error: debian/tmp exists, but shouldn't: clean up by hand and retry
	sed -i '/linphone-sdk/d' build/linphone-app/cmake_builder/linphone_package/cmake_install.cmake
	sed -i "s|$(CURDIR)/build/OUTPUT|$(updirs)/../usr|" \
		build/linphone-app/cmake_install.cmake \
		build/cmake_install.cmake
