#!/usr/bin/make -f

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

# build variables
export QTDIR=$(shell pwd)/qt-build-dir
OLD_LD_LIBRARY_PATH := $(LD_LIBRARY_PATH)
export LD_LIBRARY_PATH=$(OLD_LD_LIBRARY_PATH):$(QTDIR)/lib

# define BUILD_PATH
export BUILD_PATH=$(shell pwd)

YACC="byacc -d"
DEB_BUILD_GNU_ARCH  ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# check if xinerama should be enabled or not
ifeq ($(DEB_BUILD_GNU_TYPE),i386-linux)
  XINERAMA=-xinerama
else
  XINERAMA=-no-xinerama
endif
    
# build variables (directorys)
DEBIAN = $(shell pwd)/debian
P_LIBS = $(DEBIAN)/libqt3
P_DOC = $(DEBIAN)/qt3-doc
P_DEV = $(DEBIAN)/libqt3-dev
P_TOOLS = $(DEBIAN)/qt3-tools

TMP_INSTALL = $(DEBIAN)/tmp-install

BUILD_DIR = $(BUILD_PATH)/qt-build-dir

CONFIGURE_OPTS = -prefix "/usr" -docdir "/usr/share/doc/qt3-doc" -headerdir "/usr/include/qt" -datadir "/usr/share/qt" -qt-gif -system-zlib \
		 -system-libpng -system-libjpeg -system-libmng -qt-imgfmt-jpeg -qt-imgfmt-png -qt-imgfmt-mng -plugin-sql-mysql -plugin-sql-odbc \
		 -plugin-sql-psql -plugin-style-windows -plugin-style-compact -plugin-style-platinum -plugin-style-sgi -plugin-style-cde -stl \
		 $(XINERAMA) -system-nas-sound -cups -I/usr/include/mysql -I/usr/include/postgresql -I/usr/include/postgresql/server -sm \
		 -no-g++-exceptions -platform linux-g++
						   
build: patch libqt-thread-stamp libqt-thread-stamp-static libqt-nothread-stamp libqt-nothread-static-stamp

	touch build-stamp

libqt-thread-stamp:

	dh_testdir

	if [ -f "Makefile.cvs" ]; then make -f Makefile.cvs || true; fi

	@echo "QTDIR set to ${QTDIR}"

ifeq ($(DEB_BUILD_GNU_ARCH),alpha)
	cp debian/maintain/qmake-alpha.conf mkspecs/linux-g++/qmake.conf
endif
		
ifeq ($(DEB_BUILD_GNU_ARCH),hppa)
	cp debian/maintain/qmake-hppa.conf mkspecs/linux-g++/qmake.conf
endif

	rm -rf $(BUILD_DIR)
	install -d $(BUILD_DIR)

	cd $(BUILD_DIR) && echo yes | ../configure $(CONFIGURE_OPTS) -thread -plugindir "/usr/lib/qt3/plugins-mt" -enable-opengl

	# proceed
	cd $(BUILD_DIR) && make all
	cd $(BUILD_DIR) && make INSTALL_ROOT=$(TMP_INSTALL) install
	
	cp bin/qtrename140 $(TMP_INSTALL)/usr/bin/
	cp bin/qt20fix $(TMP_INSTALL)/usr/bin/
	cp bin/findtr $(TMP_INSTALL)/usr/bin/
	install -D $(BUILD_DIR)/.qmake.cache $(TMP_INSTALL)/usr/share/doc/qt3-doc/.qmake.cache

	touch libqt-thread-stamp

libqt-thread-stamp-static:

	dh_testdir

	rm -rf $(BUILD_DIR)
	install -d $(BUILD_DIR)
	
	cd $(BUILD_DIR) && echo yes | ../configure $(CONFIGURE_OPTS) -thread -static -enable-opengl -qt-sql-mysql \
	                                           -qt-sql-odbc -qt-sql-psql -qt-style-windows -qt-style-compact \
						   -qt-style-platinum -qt-style-sgi -qt-style-cde

	cd $(BUILD_DIR) && make sub-src
	cd $(BUILD_DIR) && make -C src INSTALL_ROOT=$(TMP_INSTALL) install_target
	
	touch libqt-thread-stamp-static

libqt-nothread-stamp:

	dh_testdir

	rm -rf $(BUILD_DIR)
	install -d $(BUILD_DIR)
	
	cd $(BUILD_DIR) && echo yes | ../configure $(CONFIGURE_OPTS) -plugindir "/usr/lib/qt3/plugins"
	
	cd $(BUILD_DIR) && make sub-src sub-plugins
	cd $(BUILD_DIR) && make -C src INSTALL_ROOT=$(TMP_INSTALL) install_target
	cd $(BUILD_DIR) && make INSTALL_ROOT=$(TMP_INSTALL) plugins-install
	
	touch libqt-nothread-stamp

libqt-nothread-static-stamp:

	dh_testdir

	rm -rf $(BUILD_DIR)
	install -d $(BUILD_DIR)
		
	cd $(BUILD_DIR) && echo yes | ../configure $(CONFIGURE_OPTS) -static -qt-sql-odbc -qt-sql-psql \
						   -qt-sql-mysql -qt-style-windows -qt-style-compact \
						   -qt-style-platinum -qt-style-sgi -qt-style-cde

	cd $(BUILD_DIR) && make sub-src
	cd $(BUILD_DIR) && make -C src INSTALL_ROOT=$(TMP_INSTALL) install_target
	
	touch libqt-nothread-static-stamp

clean: unpatch

	dh_testdir

	-rm -rf debian/patched
	-rm -rf build-stamp libqt-thread-stamp libqt-thread-stamp-static \
	        libqt-nothread-stamp libqt-nothread-static-stamp
					
	-chmod -R u+w *
	-chmod a-x doc/html/layout?.png

	-rm -f `find ./ -name "*.moc"`
	-rm -f `find ./ -name "*.o"`

	find -name Makefile | while read p; do [ -f $$p.in ] && rm -f $$p || true; done

	-rm -rf mkspecs/default bin/assistant bin/designer bin/createcw bin/uic bin/qmake bin/l* \
		bin/qconfig bin/qembed bin/qtconfig bin/moc bin/qm2ts bin/mergetr bin/makeqpf \
		bin/msg2qm extensions/xt/src/.obj

	-rm -rf $(TMP_INSTALL)
	
	dh_clean

patch: patch-stamp
patch-stamp:
	test -d debian/patched || install -d debian/patched
	@echo "Patches applied in the Debian version of ${PACKAGE}:" > $@T
	@for patch in `cat debian/patches/00list`; do \
                stamp=debian/patched/$$patch.dpatch; \
                patch=debian/patches/$$patch.dpatch; \
                author=`sed -n "s,^## *.*dpatch by *,,p" $$patch`; \
                test -x $$patch || chmod +x $$patch; \
		if test -f $$stamp; then \
                        echo "$$patch already applied."; \
                        echo -e "\n$$patch ($$author):" >> $@T; \
                        sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
                else \
                        echo -n "applying patch $$patch..."; \
                        if $$patch -patch >$$stamp.new 2>&1; then \
                                mv $$stamp.new $$stamp; \
                                touch $$stamp; \
                                echo -e "\n$$patch ($$author):" >> $@T; \
                                sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
                                echo " ok."; \
                        else \
                                echo " failed."; \
                                exit 1; \
                        fi; \
                fi; \
	done
	mv -f $@T $@

unpatch:

	@for patch in `tac debian/patches/00list`; do \
                stamp=debian/patched/$$patch.dpatch; \
                patch=debian/patches/$$patch.dpatch; \
                test -x $$patch || chmod +x $$patch; \
                if test -f $$stamp; then \
                        echo -n "reverting patch $$patch..."; \
                        if $$patch -unpatch 2>&1 >/dev/null; then \
                                rm -f $$stamp; \
                                echo " ok."; \
                        else \
                                echo " failed."; \
                                exit 1; \
                        fi; \
                fi; \
	done
	
	rm -f patch-stamp 

binary-indep: build

	# Build architecture-independent files here.

	dh_testdir
	dh_testroot
	dh_clean -i
	dh_installdirs -i
	dh_install -pqt3-doc --sourcedir=debian/tmp-install

	find doc/man -path \*/CVS -prune -o -print | cpio -pmd $(P_DOC)/usr/share/doc/qt3-doc/
	find examples -path \*/CVS -prune -o -print | cpio -pmd $(P_DOC)/usr/share/doc/qt3-doc/
	find tutorial -path \*/CVS -prune -o -print | cpio -pmd $(P_DOC)/usr/share/doc/qt3-doc/
	
	(cd $(P_DOC)/usr/share/doc/qt3-doc && tar -czvf	examples.tgz examples)
	rm -rf $(P_DOC)/usr/share/doc/qt3-doc/examples
	
	(cd $(P_DOC)/usr/share/doc/qt3-doc && tar -czvf ftutorial.tgz tutorial)
	rm -rf $(P_DOC)/usr/share/doc/qt3-doc/tutorial
			
	-rm -f `find $(P_DOC)/usr/share/doc/qt3-doc/ -name "*.o"`
	find $(P_DOC) -type f -perm +0100 | xargs --no-run-if-empty rm -f
	cd $(P_DOC)/usr/share/doc/libqt3 && ln -s ../qt3-doc/doc .

	dh_installdocs -i

	mv $(P_DOC)/usr/share/doc/qt3-doc/doc/man/man3 $(P_DOC)/usr/share/man/
	-rm -rf $(P_DOC)/usr/share/doc/qt3-doc/doc/man

	dh_installchangelogs -i changes-3.1.1
	cp debian/README.Debian $(P_DOC)/usr/share/doc/qt3-doc/
	dh_compress -i -Xhtml/
	dh_link -i
	
	# the overrides file
	install -d $(P_DOC)/usr/share/lintian/overrides/
	cp debian/overrides/qt3-doc $(P_DOC)/usr/share/lintian/overrides/
	
	dh_fixperms -i
	
	find $(P_DOC)/usr/share/doc -type f | xargs chmod 644

	# proceed
	dh_installdeb -i
	dh_perl -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i
	
binary-arch: build

	dh_testdir
	dh_testroot
	dh_clean -a
	dh_installdirs -a

	# move the files to where they belong
	dh_install -Nqt3-doc --sourcedir=debian/tmp-install

	# install undocumented(7) links
	dh_undocumented --package=libqt3-dev qmake.1 qtrename140.1 qm2ts.1
	dh_undocumented --package=qt3-tools linguist.1 assistant.1
				  
	# install other files
	cp tools/designer/interfaces/*.h $(P_TOOLS)/usr/share/qt/tools/designer/interfaces/
	cp debian/maintain/designer.desktop $(P_TOOLS)/usr/share/applnk/Development/
	cp debian/maintain/linguist.desktop $(P_TOOLS)/usr/share/applnk/Development/
	
	# install manpages
	-cp doc/man/man1/*.1 $(P_TOOLS)/usr/share/man/man1/
	-rm -rf $(P_TOOLS)/usr/share/man/man1/qvfb.1
	-mv $(P_TOOLS)/usr/share/man/man1/m*.1 $(P_DEV)/usr/share/man/man1/
	-mv $(P_TOOLS)/usr/share/man/man1/f*.1 $(P_DEV)/usr/share/man/man1/
	-mv $(P_TOOLS)/usr/share/man/man1/q*.1 $(P_DEV)/usr/share/man/man1/

	# install overrides files
	for a in debian/overrides/*; do install -d debian/`echo "$$a" | sed 's/debian\/overrides\///g'`/usr/share/lintian/overrides; done
	for a in debian/overrides/*; do cp "$$a" debian/`echo "$$a" | sed 's/debian\/overrides\///g'`/usr/share/lintian/overrides/`echo "$$a" | sed 's/debian\/overrides\///g'`; done

	# run remaining debhelper scripts
	dh_installdocs -a
	dh_installmenu -a
	dh_undocumented
	
	dh_installchangelogs -a changes-3.1.1

	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	
	# fix file permissions
	chmod 755 debian/qt3-tools/usr/bin/designer debian/libqt3-dev/usr/bin/qmake \
		  debian/qt3-tools/usr/bin/uic debian/qt3-tools/usr/bin/assistant

	# run remaining debhelper scripts
	dh_makeshlibs -a -V
	dh_installdeb -a
	dh_perl -a
	dh_shlibdeps -a -l`pwd`/debian/libqt3/usr/lib:`pwd`/debian/libqt3-mt/usr/lib
	
	# fix shlibdeps
	cd debian && cat libqt3-mt.substvars | sed 's/libqt3 ([^)]*), //g' > libqt3-mt.substvars.new
	cd debian && mv libqt3-mt.substvars.new libqt3-mt.substvars
	
	dh_gencontrol
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build binary-indep binary-arch binary clean patch unpatch
