#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

DEBDIR = ..

comma :=,
empty :=
space := $(empty) $(empty)

# Set the build type (debug/release)
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	QMAKE_OPTIONS+=CONFIG+=debug
else
	QMAKE_OPTIONS+=CONFIG+=release
endif

QMAKE_OPTIONS+=CONFIG+=nomeegotouch

# Don't build documentation by setting DEB_BUILD_OPTION=nodocs
ifeq (,$(findstring nodocs,$(DEB_BUILD_OPTIONS)))
	QMAKE_OPTIONS+=CONFIG+=docs
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

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

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

ifneq (,$(filter coverage,$(TMP_BUILD_OPTS)))
        QMAKE_OPTIONS += COV_OPTION=on
endif

ifneq (,$(filter notranslucency,$(TMP_BUILD_OPTS)))
        QMAKE_OPTIONS += DEFINES+=M_IM_DISABLE_TRANSLUCENCY

        ifneq (,$(filter shapewindow,$(TMP_BUILD_OPTS)))
            QMAKE_OPTIONS += DEFINES+=M_IM_USE_SHAPE_WINDOW
        endif
endif

ENABLE_MULTITOUCH = true

ifneq (,$(filter nomultitouch,$(TMP_BUILD_OPTS)))
        ENABLE_MULTITOUCH = false
endif

ARCHITECTURE := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifneq (armel, ${ARCHITECTURE})
        #multi-touch could be used for ARM only
        ENABLE_MULTITOUCH = false
endif

QMAKE_OPTIONS += M_IM_ENABLE_MULTITOUCH=$(ENABLE_MULTITOUCH)

ifneq (,$(filter timestamps,$(TMP_BUILD_OPTS)))
    QMAKE_OPTIONS += DEFINES+=M_TIMESTAMP
endif


# shared library versions, option 1
version=0.1.0
major=0

QT_INSTALL_DATA:=$(shell /opt/qt4-maemo5/bin/qmake -query QT_INSTALL_DATA)
QT_INSTALL_PLUGINS:=$(shell /opt/qt4-maemo5/bin/qmake -query QT_INSTALL_PLUGINS)

configure: configure-stamp
configure-stamp:
	dh_testdir
	/opt/qt4-maemo5/bin/qmake -r $(QMAKE_OPTIONS)
	sed -e 's|QT_INSTALL_DATA|${QT_INSTALL_DATA}|' > debian/libmeegoimframework-dev.install < debian/libmeegoimframework-dev.install.in
	sed -e 's|QT_INSTALL_DATA|${QT_INSTALL_DATA}|' > debian/libmeegoimquick-dev.install < debian/libmeegoimquick-dev.install.in
	sed -e 's|QT_INSTALL_PLUGINS|${QT_INSTALL_PLUGINS}|' > debian/meego-im-context.install < debian/meego-im-context.install.in

	touch configure-stamp


build: build-stamp
build-stamp: configure-stamp
	dh_testdir

	make  $(PARALLEL_MAKEFLAGS)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	rm -f build-stamp configure-stamp

	make  $(PARALLEL_MAKEFLAGS) clean || true
	rm -f lib/*
	rm -rf .obj

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp

	# generate tests.xml
	cd tests && ./gen-tests-xml.sh > tests.xml

	#doc package
	make  $(PARALLEL_MAKEFLAGS) doc

	# Then proceed to normal installation
	INSTALL_ROOT=$(CURDIR)/debian/tmp make  $(PARALLEL_MAKEFLAGS) install



# 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
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_install --sourcedir=debian/tmp -v
	dh_gconf
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_installman
	dh_link
	dh_strip --dbg-package=libmeegoimframework0 --dbg-package=meego-im-context --dbg-package=meego-im-uiserver -Xmeego-im-framework-tests
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb --destdir=$(DEBDIR)

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