#!/usr/bin/make -f

export QTDIR := $(shell pwd)
export PATH := $(QTDIR)/bin:$(PATH)
export LD_LIBRARY_PATH := $(QTDIR)/lib:$(LD_LIBRARY_PATH)

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/makefile.mk
include /usr/share/cdbs/1/rules/dpatch.mk

DEB_MAKE_INVOKE := $(MAKE)
DEB_MAKE_BUILD_TARGET := sub-src sub-tools 
DEB_MAKE_INSTALL_TARGET := INSTALL_ROOT=$(DEB_DESTDIR) install
DEB_DH_INSTALL_SOURCEDIR := debian/tmp

# Ensure the *.debug files aren't included in any package other than
# libqt4-debug.
DEB_DH_INSTALL_ARGS := --exclude=.debug

DEB_MAKE_CLEAN_TARGET := confclean distclean
DEB_DH_MAKESHLIBS_ARGS_ALL := -V

DEB_INSTALL_CHANGELOGS_ALL := changes-4.2.3

ifeq ($(DEB_HOST_ARCH),arm)
	EXTRA_CONFIGURE_OPTS += -DQT_QLOCALE_USES_FCVT
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
        PLATFORM_ARG = linux-g++
else
        PLATFORM_ARG = glibc-g++
endif


common-configure-arch::
	./configure -confirm-license \
	            -prefix "/usr" \
	            -docdir "/usr/share/qt4/doc" \
	            -headerdir "/usr/include/qt4" \
	            -datadir "/usr/share/qt4" \
	            -plugindir "/usr/lib/qt4/plugins" \
	            -translationdir "/usr/share/qt4/translations" \
	            -sysconfdir "/etc/xdg" \
	            -platform $(PLATFORM_ARG) \
		    -debug-and-release \
	            -fast \
	            -no-rpath \
	            -system-zlib \
	            -system-libpng \
	            -system-libjpeg \
	            -system-nas-sound \
	            -qt-gif \
	            -qt-sql-psql \
	            -qt-sql-mysql \
		    -qt-sql-sqlite \
	            -system-sqlite \
	            -qt-sql-sqlite2 \
	            -I/usr/include/freetype2 \
	            -lfontconfig \
	            -cups \
	            $(EXTRA_CONFIGURE_OPTS)

clean::

# Extra stuff missed by confclean/distclean
	rm -f Makefile \
	      bin/qmake \
	      mkspecs/default \
	      src/corelib/global/arch \
	      examples/tools/plugandpaint/plugins/*.so \
	      config.status \
	      demos/shared/libdemo_shared.prl \
	      mkspecs/qconfig.pri \
	      src/corelib/global/qconfig.*

	rm -rf lib/ plugins/

	find include/ -type l -print0 | xargs -0r rm -f
	find . -mindepth 2 -name Makefile -print0 \
	                -o -name Makefile.Debug -print0 \
	                -o -name Makefile.Release -print0 \
	                | xargs -0r rm -f

# Hack to fix dpatch-edit-patch
unpatch: deapply-dpatches


BINDIR := $(DEB_DESTDIR)/usr/bin
common-install-arch::
# Tack on -qt4 to the binaries that use the alternatives system
	for f in qmake lupdate lrelease uic moc assistant designer linguist qtconfig; do \
	    mv "$(BINDIR)/$$f" "$(BINDIR)/$$f-qt4"; \
	done

binary-install/qt4-doc::
# Tar up the examples to include them in qt4-doc
	-$(MAKE) -k -C examples distclean
	find examples/ -name '*.debug' -print0 | xargs -0r rm -f
	tar -cv examples/ | \
	gzip -9 > debian/qt4-doc/usr/share/doc/qt4-doc/qt4-examples.tar.gz

	-$(MAKE) -k -C demos distclean
	find demos/ -name '*.debug' -print0 | xargs -0r rm -f
	tar -cv demos/ | \
	gzip -9 > debian/qt4-doc/usr/share/doc/qt4-doc/qt4-demos.tar.gz

binary-install/libqt4-debug::
# Run dh_install without the default DEB_DH_INSTALL_ARGS to install the
# *.debug files.
	dh_install -plibqt4-debug --sourcedir=$(DEB_DH_INSTALL_SOURCEDIR)

# Automatically install lintian overrides, stolen from debian-qt-kde.mk
$(patsubst %,binary-install/%,$(DEB_PACKAGES)) :: binary-install/%:
	if test -e debian/$(cdbs_curpkg).lintian; then \
	    install -p -D -m644 debian/$(cdbs_curpkg).lintian \
	    debian/$(cdbs_curpkg)/usr/share/lintian/overrides/$(cdbs_curpkg); \
        fi
