#!/usr/bin/make -f

#export DH_VERBOSE=1
export DEB_BUILD_HARDENING=1

# The default Mumble build for Unix-like systems
# uses the "-z now" linker flag by default on all
# build products that support it, causing all dynamic
# symbols to be resolved when the program is loaded.
#
# However, the Mumble overlay (libmumble.so, a shared
# library that is often LD_PRELOAD'ed into games),
# expects to be able to resolve OpenGL symbols at
# runtime without linking against a specific libGL
# implementation.
#
# Linking it as "-z now" defeats that, as the
# loader will attempt to resolve the overlay's
# OpenGL symbols at program start-up, which will
# fail because it isn't linked against a library
# providing those symbols.
#
# Disabling the hardening option below is equivalent
# to not linking libmumble.so using the "-z now"
# option. All other products are still linked with
# "-z now".
export DEB_BUILD_HARDENING_BINDNOW=0

MAKEFILE = $(firstword $(MAKEFILE_LIST))
SOURCE_DIR = $(dir $(MAKEFILE))..
VERSION := $(shell dpkg-parsechangelog -l$(dir $(MAKEFILE))changelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')
MUMBLE_DEB_VERSION := $(shell dpkg-parsechangelog -l$(dir $(MAKEFILE))changelog | sed -ne 's/^Version: \(.*\).*/\1/p')

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

export DH_OPTIONS

configure: configure-stamp
configure-stamp:
	dh_testdir
	qmake-qt4 -recursive main.pro \
	CONFIG*=release \
	CONFIG*=symbols \
	CONFIG*=no-embed-qt-translations \
	CONFIG*=packaged \
	CONFIG*=quiet-build-log \
	CONFIG*=bundled-celt \
	CONFIG*=no-bundled-opus \
	DEFINES*=NO_UPDATE_CHECK \
	DEFINES*=PLUGIN_PATH=/usr/lib/mumble \
	DEFINES*=MUMBLE_VERSION=$(MUMBLE_DEB_VERSION) \
	DEFINES*=HAVE_LIMITS_H \
	DEFINES*=HAVE_ENDIAN_H
	touch configure-stamp

build: build-arch

build-arch: build-arch-stamp

build-arch-stamp: configure-stamp
	$(MAKE) release
	slice2html -I/usr/share/Ice-3.4.1/slice -I/usr/share/Ice-3.3.1/slice -I/usr/share/Ice/slice src/murmur/Murmur.ice --output-dir Ice
	touch $@

build-indep:


clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp configure-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf release debug Ice
	[ ! -d release-32 ] || rm -rf release-32
	rm -f $(CURDIR)/debian/mumble-server.logrotate $(CURDIR)/mumble-server.init
	debconf-updatepo
	dh_clean

install: install-arch

install-arch:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -s
	dh_install -s
	install -m 0755 ./scripts/murmur.init $(CURDIR)/debian/mumble-server.init
	install -m 0755 ./scripts/murmur.logrotate $(CURDIR)/debian/mumble-server.logrotate
	install -m 0644 -D ./scripts/murmur.conf $(CURDIR)/debian/mumble-server/etc/dbus-1/system.d/mumble-server.conf
	install -m 0644 -D ./scripts/murmur.ini.system $(CURDIR)/debian/mumble-server/etc/mumble-server.ini

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES
	dh_installdocs
	dh_installmenu
	dh_installinit -- defaults 95
	dh_installman
	dh_lintian
	dh_installlogrotate
	dh_installdebconf
	dh_installexamples
	dh_link
	dh_gconf
	dh_icons
	dh_strip --dbg-package=mumble-dbg
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:

binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary:	binary-arch

.PHONY: clean configure build build-arch build-indep \
        binary binary-arch binary-indep binary-common \
        install install-arch
