#!/usr/bin/make -f

export DH_VERBOSE=1
# Attempt to harden the build
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Remove remove "-z,now" LDFLAGS because mumble-overlay (libmumble.so)
# needs to be able to resolve OpenGL symbols at runtime rather than linking
# to a specific libGL implementation
export DEB_LDFLAGS_MAINT_STRIP = -r,now
# NOTE: there seems to be difficulties with build hardening for packages using
#       CMake; see: https://wiki.debian.org/Hardening#Notes_for_packages_using_CMake
DEB_BUILD_NUMBER := $(shell dpkg-parsechangelog -S Version | cut -d'.' -f 3 | cut -d'-' -f 1)
# Try to set Debian hardening build flags
CMAKE_C_FLAGS := $(CFLAGS)
CMAKE_XX_FLAGS := $(CXXFLAGS)
CMAKE_SHARED_LINKER_FLAGS := $(LDFLAGS)

# qtchooser will try to use qmake from qt4 even if it's not installed
export QT_SELECT=qt5

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

export DH_OPTIONS

%:
	dh  $@ --buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure -- \
	-DBUILD_NUMBER=$(DEB_BUILD_NUMBER) \
	-DCMAKE_BUILD_TYPE=Release \
	-Dbundle-qt-translations=OFF \
	-Dbundled-gsl=OFF \
	-Dbundled-json=OFF \
	-Dbundled-renamenoise=ON \
	-Dbundled-speex=OFF \
	-Doverlay=ON \
	-Doverlay-xcompile=OFF \
	-Dsymbols=ON \
	-Dupdate=OFF \
	-Duse-pkgconf-install-paths=ON \
	-Dwarnings-as-errors=OFF

override_dh_clean:
	rm -rfv Ice
	# logratate and init file have both been removed
	#rm -fv $(CURDIR)/debian/mumble-server.logrotate $(CURDIR)/mumble-server.init
	debconf-updatepo
	dh_clean

override_dh_auto_build:
	# Create Ice HTML files and place in Ice/ directory to be installed with mumble-server.docs
	slice2html -I/usr/share/ice/slice -I/usr/share/slice src/murmur/MumbleServer.ice --output-dir Ice
	dh_auto_build
