#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

TMP_BUILD_OPTS = $(subst $(comma),$(space),$(DEB_BUILD_OPTIONS))
MAKEDEMOS = 1

ifneq (,$(filter parallel=%,$(TMP_BUILD_OPTS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(TMP_BUILD_OPTS)))
    PARALLEL_MAKEFLAGS += -j$(NUMJOBS)
endif

# Disable building of demos by setting DEB_BUILD_OPTION=nodemos,...
ifneq (,$(filter nodemos,$(TMP_BUILD_OPTS)))
	#No demos
	EXCLUSIONS += --no-package=qmlshadersplugin-demos
	EXCLUSIONS += --no-package=qmlshadersplugin-demos-dbg
	MAKEDEMOS = 0
endif

QMAKE_OPTIONS += CONFIG+=release
# Add debug symbols to release configuration, and prevent make to strip them
# (these are later stripped from binaries into dbg package with dh_strip).
QMAKE_OPTIONS += QMAKE_CXXFLAGS_RELEASE+=-g
QMAKE_OPTIONS += QMAKE_STRIP=echo

build: build-stamp
build-stamp:
	dh_testdir
	qmake -r $(QMAKE_OPTIONS) ./qmlshadersplugin.pro
	+$(MAKE) $(PARALLEL_MAKEFLAGS)
ifneq (0,$(MAKEDEMOS))
	qmake -r $(QMAKE_OPTIONS) ./demos.pro -o Makefile_demos
	+$(MAKE) $(PARALLEL_MAKEFLAGS) -f Makefile_demos
endif
	touch $@

install: install-stamp

install-stamp: build-stamp
	dh_testdir
	dh_testroot

# FIXME check if dh_prep is available for maemo5 - N900 to replace deprecated dh_clean -k
	dh_clean -k

	dh_installdirs
	INSTALL_ROOT=$(CURDIR)/debian/tmp $(MAKE) install
ifneq (0,$(MAKEDEMOS))
	INSTALL_ROOT=$(CURDIR)/debian/tmp $(MAKE) -f Makefile_demos install
endif
	touch $@

clean:
	dh_testdir
	dh_testroot

	$(MAKE) distclean -f Makefile || true
	$(MAKE) distclean -f Makefile_demos || true

	dh_clean build-stamp install-stamp

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir $(EXCLUSIONS)
	+./debian/configure_package_install_files
	dh_testroot $(EXCLUSIONS)
	dh_installchangelogs $(EXCLUSIONS)
	dh_installdocs $(EXCLUSIONS)
	dh_install --sourcedir=debian/tmp/ $(EXCLUSIONS) -v
	# print warnings for not installed files
	dh_install --sourcedir=debian/tmp/ $(EXCLUSIONS) --list-missing
	dh_link $(EXCLUSIONS)
	dh_strip -p qmlshadersplugin --dbg-package=qmlshadersplugin-dbg
ifneq (0,$(MAKEDEMOS))
	dh_strip -p qmlshadersplugin-demos --dbg-package=qmlshadersplugin-demos-dbg
endif
	dh_compress $(EXCLUSIONS)
	dh_fixperms $(EXCLUSIONS)
	dh_makeshlibs $(EXCLUSIONS)
	dh_installdeb $(EXCLUSIONS)
	dh_shlibdeps $(EXCLUSIONS)
	dh_gencontrol $(EXCLUSIONS)
	dh_md5sums $(EXCLUSIONS)
	dh_builddeb $(EXCLUSIONS)

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install

