#!/usr/bin/make -f

export QTDIR := $(shell pwd)
export PATH := $(QTDIR)/bin:$(PATH)
# workaround to use lrelease.
export LD_LIBRARY_PATH := $(QTDIR)/lib:$(LD_LIBRARY_PATH)

QTVERSION := $(shell ls changes-* | cut -f2 -d '-')
CURRENTVERSION := $(shell head -1 debian/changelog  | sed 's/[^(]*(\([^)]*\)).*/\1/')

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/makefile.mk
include /usr/share/cdbs/1/rules/patchsys-quilt.mk
include /usr/share/cdbs/1/rules/utils.mk

DEB_MAKE_INVOKE := $(MAKE)
DEB_MAKE_BUILD_TARGET := sub-src
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-dbg
DEB_DH_INSTALL_ARGS := --exclude=.debug

DEB_MAKE_CLEAN_TARGET := confclean distclean

# Shlibs of the current upstream version
#DEB_DH_MAKESHLIBS_ARGS_ALL := -V
# Specific shlibs version
SHLIBSVERSION := 4.4.0
$(foreach libpkg,$(filter-out %-dev %-dbg,$(filter lib%,$(DEB_ARCH_PACKAGES))),$(eval DEB_DH_MAKESHLIBS_ARGS_$(libpkg) := -V'$(libpkg) (>= $(SHLIBSVERSION))'))

DEB_DH_STRIP_ARGS_libqt45-dbg := --exclude=.debug

DEB_DH_SHLIBDEPS_ARGS_ALL := --exclude=.debug

DEB_INSTALL_CHANGELOGS_ALL := changes-$(QTVERSION)

DEB_INSTALL_DOCS_ALL := GPL_EXCEPTION_ADDENDUM.TXT

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

PLATFORM_ARG = linux-g++

common-build-arch::  debian/stamp-makefile-build-tools

debian/stamp-makefile-build-tools: debian/stamp-makefile-build
	$(MAKE) sub-tools
	touch $@

common-configure-arch:: config.status

config.status:
	# Test broken hppa kernel with glibc >= 2.5
ifeq ($(DEB_HOST_ARCH),hppa)
	mkdir -p debian/hppa-tmp
	echo "Testing whether getdents kernel bug is present on this buildd - see #433768"
	gcc -o debian/hppa-tmp/hppa-test-program debian/readdir-hppa-test.c
	cd $(CURDIR)/doc/html && $(CURDIR)/debian/hppa-tmp/hppa-test-program | sort > $(CURDIR)/debian/hppa-tmp/readdir_r-out
	cd $(CURDIR)/doc/html && ls -a | sort > $(CURDIR)/debian/hppa-tmp/ls-a-out
	@if ! diff -q $(CURDIR)/debian/hppa-tmp/readdir_r-out $(CURDIR)/debian/hppa-tmp/ls-a-out ; \
		then \
		echo "Kernel bug present. This will misbuild qt4 if proceeding. Failing" ; \
		echo "Please update kernel and test again" ; \
		exit 5 ; \
	fi
endif

	./configure -confirm-license \
	            -prefix "/usr" \
	            -bindir "/usr/bin" \
	            -libdir "/usr/lib" \
	            -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" \
	            -demosdir "/usr/lib/qt4/demos" \
	            -examplesdir "/usr/lib/qt4/examples" \
	            -platform $(PLATFORM_ARG) \
	            -fast \
	            -no-rpath \
	            -system-zlib \
	            -system-libtiff \
	            -system-libpng \
	            -system-libjpeg \
	            -system-nas-sound \
	            -qt-gif \
	            -plugin-sql-mysql \
	            -plugin-sql-sqlite \
	            -plugin-sql-sqlite2 \
	            -system-sqlite \
	            -I/usr/include/freetype2 \
	            -lfontconfig \
	            -exceptions \
	            -dbus \
	            -no-pch \
	            -phonon \
	            -svg \
	            -webkit \
	            -xmlpatterns \
	            -xinput \
 		    -no-optimized-qmake \
		    -opengl es2 \
		    -hildon \
		    -reduce-relocations \
		    -graphicssystem raster \
	            $(EXTRA_CONFIGURE_OPTS)


clean::
# Extra stuff missed by confclean/distclean
	
	# Misc. files
	rm -f \
	  config.status \
	  config.tests/.qmake.cache \
	  examples/dbus/*/Makefile.* \
	  mkspecs/qconfig.pri \
	  src/corelib/global/qconfig.* \
	;
	
	# Misc. directories
	rm -rf \
	  examples/tools/plugandpaint/plugins/ \
	  examples/tools/styleplugin/styles/ \
	  lib/ \
	  mkspecs/glibc-g++/ \
	  plugins/ \
	;
	
	# hppa test directory
	rm -rf debian/hppa-tmp

	# Leftover dirs
	find -depth -type d \( -false \
	  -o -name debug-shared \
	  -o -name debug-static \
	  -o -name \*.gch \
	  -o -name .moc\* \
	  -o -name .obj\* \
	  -o -name .pch \
	  -o -name pkgconfig \
	  -o -name .rcc \
	  -o -name release-shared \
	  -o -name release-static \
	  -o -name .uic \
	\) -print0 | xargs -0 rm -rf
		
	# Leftover files and all symlinks
	find \( -false \
	  -o -name \*.a \
	  -o -name Makefile.Debug \
	  -o -name Makefile.Release \
	  -o -name \*.o \
	  -o -name \*.prl \
	  -o -name \*.so \
	  -o -name \*.so.debug \
	  -o -type l \
	\) -print0 | xargs -0 rm -rf 

	# Recreate symlink
	ln -s debian.fremantle debian
	
	# Delete all Makefiles, excluding some from src/3rdparty
	find $(CURDIR) -name Makefile \
	  ! -path $(CURDIR)/src/3rdparty/Makefile \
	  ! -path $(CURDIR)/src/3rdparty/freetype/\* \
	  ! -path $(CURDIR)/src/3rdparty/zlib/\* \
	 -print0 | xargs -0 rm -rf 
	
	# Any remaining executables
	find $(CURDIR) -type f -exec file -i '{}' \; | grep \
	  -e application/x-executable \
	| cut -d ':' -f 1 | xargs rm -f

	# Generated on build
	rm -f debian/shlibs.local
	rm -f debian/stamp-makefile-build-tools

common-install-arch::
# Fix wrong path in pkgconfig files
	find $(DEB_DESTDIR)/usr/lib/pkgconfig -type f -name '*.pc' \
		-exec perl -pi -e "s, -L$(CURDIR)/?\S+,,g" {} \;
# Fix wrong path in prl files
	find $(DEB_DESTDIR)/usr/lib -type f -name '*.prl' \
		-exec perl -pi -e "s, -L$(CURDIR)/\S+,,g" {} \;
	find $(DEB_DESTDIR)/usr/lib -type f -name '*.prl' \
		-exec sed -i -e "/^QMAKE_PRL_BUILD_DIR/d;s/\(QMAKE_PRL_LIBS =\).*/\1/" {} \;

install/qt45-demos::
	mkdir -p debian/$(cdbs_curpkg)/usr/lib/qt4/demos/qtdemo
	uudecode -o debian/$(cdbs_curpkg)/usr/lib/qt4/demos/qtdemo/qtdemo.qhc \
		debian/collection/qtdemo.qhc.uu

install/qt45-designer::
	install -D -p -m0644 debian/desktop/designer-qt4.desktop \
		debian/$(cdbs_curpkg)/usr/share/applications/designer-qt4.desktop
	mkdir -p debian/$(cdbs_curpkg)/usr/share/pixmaps
	uudecode -o debian/$(cdbs_curpkg)/usr/share/pixmaps/designer.png \
		debian/desktop/designer.png.uu

install/qt45-dev-tools::
	install -D -p -m0644 debian/desktop/assistant-qt4.desktop \
		debian/$(cdbs_curpkg)/usr/share/applications/assistant-qt4.desktop
	install -D -p -m0644 debian/desktop/linguist-qt4.desktop \
		debian/$(cdbs_curpkg)/usr/share/applications/linguist-qt4.desktop
	mkdir -p debian/$(cdbs_curpkg)/usr/share/pixmaps
	uudecode -o debian/$(cdbs_curpkg)/usr/share/pixmaps/assistant.png \
		debian/desktop/assistant.png.uu
	uudecode -o debian/$(cdbs_curpkg)/usr/share/pixmaps/linguist.png \
		debian/desktop/linguist.png.uu

install/qt45-qtconfig::
	install -D -p -m0644 debian/desktop/qt4config.desktop \
		debian/$(cdbs_curpkg)/usr/share/applications/qt4config.desktop
	mkdir -p debian/$(cdbs_curpkg)/usr/share/pixmaps
	uudecode -o debian/$(cdbs_curpkg)/usr/share/pixmaps/qtconfig.png \
		debian/desktop/qtconfig.png.uu

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

binary-install/libqt45-webkit-dbg::
# Run dh_install without the default DEB_DH_INSTALL_ARGS to install the *.debug files
	dh_install -plibqt45-webkit-dbg --sourcedir=$(DEB_DH_INSTALL_SOURCEDIR)

binary-install/libqt45-xmlpatterns-dbg::
# Run dh_install without the default DEB_DH_INSTALL_ARGS to install the *.debug files
	dh_install -plibqt45-xmlpatterns-dbg --sourcedir=$(DEB_DH_INSTALL_SOURCEDIR)

binary-install/libqt45-phonon-dbg::
# Run dh_install without the default DEB_DH_INSTALL_ARGS to install the *.debug files
	dh_install -plibqt45-phonon-dbg --sourcedir=$(DEB_DH_INSTALL_SOURCEDIR)

binary-post-install/libqt45-dev::
	doxytag -t debian/$(cdbs_curpkg)/usr/share/qt4/doc/qt4.tag \
		$(DEB_DESTDIR)/usr/share/qt4/doc/html

# 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

# Generate shlibs local files
$(patsubst %,binary-fixup/%,$(DEB_ALL_PACKAGES)) :: binary-fixup/%: binary-strip/%
	if test -e debian/$(cdbs_curpkg)/DEBIAN/shlibs ; then \
		sed 's/>=[^)]*/= $(CURRENTVERSION)/' debian/$(cdbs_curpkg)/DEBIAN/shlibs >> debian/shlibs.local ;\
	fi

