#!/usr/bin/make -f

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

# build variables
export QTDIR=$(shell pwd)

# re-set $(LD_LIBRARY_PATH)
OLD_LD_LIBRARY_PATH := $(LD_LIBRARY_PATH)
export LD_LIBRARY_PATH=$(QTDIR)/lib:$(OLD_LD_LIBRARY_PATH)

# fix path
OLD_PATH := $(PATH)
export PATH=$(QTDIR)/bin:$(OLD_PATH)

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)

# dpatch stuff
include /usr/share/dpatch/dpatch.make

# build variables (directorys)
DEBIAN = $(shell pwd)/debian
P_LIBS = $(DEBIAN)/libqt3c102
P_DOC = $(DEBIAN)/qt3-doc
P_DEV = $(DEBIAN)/libqt3-dev
P_APPSDEV = $(DEBIAN)/qt3-apps-dev
P_HEADERS = $(DEBIAN)/libqt3-headers
P_QTMTDEV = $(DEBIAN)/libqt3-mt-dev

TMP_INSTALL = $(DEBIAN)/tmp-install

CONFIGURE_OPTS = -prefix "/usr" -docdir "/usr/share/qt3/doc" -headerdir "/usr/include/qt3" \
		-datadir "/usr/share/qt3" -plugindir "/usr/lib/qt3/plugins" -qt-gif \
		-system-zlib -system-libpng -system-libjpeg -system-libmng \
		-plugin-imgfmt-jpeg -qt-imgfmt-png -plugin-imgfmt-mng \
		-plugin-sql-odbc -plugin-sql-psql -plugin-sql-mysql \
		-plugin-style-windows -plugin-style-compact -plugin-style-platinum \
		-plugin-style-sgi -plugin-style-cde -plugin-style-motif \
		-plugin-style-motifplus -I/usr/include/mysql -I/usr/include/freetype2 \
		-I/usr/include/postgresql -I/usr/include/postgresql/server \
		-lfontconfig -no-stl -system-nas-sound -cups -xft \
		-sm -no-exceptions -platform linux-g++ -fast

build: patch libqt-stamp libqt-thread-stamp

	touch build-stamp

libqt-stamp:

	@echo "QTDIR is ${QTDIR}"

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

	dh_testdir

	echo yes | ./configure $(CONFIGURE_OPTS) -no-thread -disable-opengl

	make sub-src sub-plugins
	make -C src INSTALL_ROOT=$(TMP_INSTALL) install_target
	make INSTALL_ROOT=$(TMP_INSTALL) plugins-install
	
	# rename the plugins to save them from being overidden
	for a in `find $(TMP_INSTALL)/usr/lib/qt3/plugins/ -name '*.so'`; do rename 's/.so/-non-mt.so/' "$$a"; done

	# install qmake.cache file
	install -d $(P_DEV)/usr/share/doc/libqt3-dev/
	cat .qmake.cache | sed "s#$(QTDIR)#/usr/share/qt3#g" > $(P_DEV)/usr/share/doc/libqt3-dev/qmake.cache

	# install qconfig.h for the non-mt build
	install -m 644 -D include/qconfig.h $(P_DEV)/usr/include/qt3/qconfig.h
	install -m 644 -D include/qmodules.h $(P_DEV)/usr/include/qt3/qmodules.h

	touch libqt-stamp

libqt-thread-stamp:

	dh_testdir

	make -C src clean
	make -C plugins/src clean

	echo yes | ./configure $(CONFIGURE_OPTS) -thread -enable-opengl

	# proceed
	make sub-src sub-plugins sub-tools
	make INSTALL_ROOT=$(TMP_INSTALL) install
	
	# archives get accidentally stripped by make install. Copy over unstripped ones for now.
	cp lib/lib*.a $(TMP_INSTALL)/usr/lib/

	cp bin/qtrename140 $(TMP_INSTALL)/usr/bin/
	cp bin/qt20fix $(TMP_INSTALL)/usr/bin/
	cp bin/findtr $(TMP_INSTALL)/usr/bin/

	# build conv2ui
	cd tools/designer/tools/conv2ui && make
	cp bin/conv2ui $(TMP_INSTALL)/usr/bin/conv2ui

	# build qvfb
	cd tools/qvfb/ && make
	cp tools/qvfb/qvfb $(TMP_INSTALL)/usr/bin/qvfb

	# install qmake.cache file
	install -d $(P_QTMTDEV)/usr/share/doc/libqt3-mt-dev/
	cat .qmake.cache | sed "s#$(QTDIR)#/usr/share/qt3#g" > $(P_QTMTDEV)/usr/share/doc/libqt3-mt-dev/qmake.cache
	
	# install qconfig.h for the mt
	install -m 644 -D include/qconfig.h $(P_QTMTDEV)/usr/include/qt3/qconfig.h
	install -m 644 -D include/qmodules.h $(P_QTMTDEV)/usr/include/qt3/qmodules.h

	touch libqt-thread-stamp

clean: unpatch

	dh_testdir

	-rm -rf debian/patched
	-rm -rf build-stamp libqt-stamp libqt-thread-stamp

	-chmod -R u+w *
	-chmod a-x doc/html/layout?.png

	if [ -f "src/Makefile" ]; then \
		make -C src clean; make -C plugins/src distclean; make -C tools distclean; \
		make -C tools/makeqpf distclean; make -C tools/qconfig distclean; make -C tools/qvfb distclean; \
		make -C tools/msg2qm distclean; make -C tools/mergetr distclean; make -C tools/qembed distclean; \
		make -C tools/designer/tools/conv2ui distclean; make -C tools/designer/tools/createcw distclean; \
		make -C tools/designer/plugins/glade distclean; make -C tools/designer/plugins/qglwidget distclean; \
		make -C config.tests/unix/largefile distclean; make -C qmake distclean; \
	fi

	-rm -rf .qmake.cache src/.qmake.internal.cache tools/designer/designer/.qmake.internal.cache qmake/GNUmakefile \
		config.status bin/moc bin/qmake src/moc/*.o mkspecs/default lib/lib* `pwd`/debian/doc $(TMP_INSTALL) \
		src/tools/qconfig.cpp include/qconfig.h include/qmodules.h plugins/accessibleqtwidgets.prl

	# delete generated Makefiles but save the toplevel Makefile
	-mv Makefile Makefile.save
	for a in `find . -name 'Makefile'`; do rm -f "$$a"; done
	-mv Makefile.save Makefile

	dh_clean

install: build

	dh_testdir
	dh_testroot
	
	dh_clean -i
	dh_installdirs

	# fix .prl files
	for a in debian/tmp-install/usr/lib/*prl; do cat "$$a" | sed \
	"s#$(QTDIR)#/usr/share/qt3#g" > "$$a".new && mv "$$a".new "$$a"; done

	dh_install --sourcedir=debian/tmp-install

	## build qt3-doc package
	# copy all docs there first
	install -d $(P_DOC)/usr/share/qt3/doc/html/
	for a in `cd $(TMP_INSTALL)/usr/share/qt3/doc/html/ && find`; do cp $(TMP_INSTALL)/usr/share/qt3/doc/html/"$$a" $(P_DOC)/usr/share/qt3/doc/html/; done

	## build designer package documentation
	# qt3-designer
	install -d `pwd`/debian/qt3-designer/usr/share/qt3/doc/html/
	for a in `cat doc/html/designer*.{html,xml} | grep png | sed 's/^.*src=\"\([^\"]+\)\".*$$/\1/' | \
	perl -pe 's#<\?p[^>]+>##' | tee outputfile | perl -ne '/<img [^>]*(src=\"[^"]+\")/; print $$1' | \
	sed 's/src=//g' | sed 's/"/ /g'`; do cp doc/html/"$$a" `pwd`/debian/qt3-designer/usr/share/qt3/doc/html/ && \
	rm -rf $(P_DOC)/usr/share/qt3/doc/html/"$$a" || true; done
	rm -rf outputfile `pwd`/debian/qt3-designer/usr/share/qt3/doc/html/logo32.png
	rm -rf `pwd`/debian/qt3-doc/usr/share/qt3/doc/html/designer*

	## build linguist package documentation
	# qt3-linguist
	install -d `pwd`/debian/qt3-linguist/usr/share/qt3/doc/html/
	for a in `cat doc/html/linguist*.{html,xml} | grep png | sed 's/^.*src=\"\([^\"]+\)\".*$$/\1/' | \
	perl -pe 's#<\?p[^>]+>##' | tee outputfile | perl -ne '/<img [^>]*(src=\"[^"]+\")/; print $$1' | \
	sed 's/src=//g' | sed 's/"/ /g'`; do cp doc/html/"$$a" `pwd`/debian/qt3-linguist/usr/share/qt3/doc/html/ && \
	rm -rf $(P_DOC)/usr/share/qt3/doc/html/"$$a" || true; done
	rm -rf outputfile `pwd`/debian/qt3-linguist/usr/share/qt3/doc/html/logo32.png
	rm -rf `pwd`/debian/qt3-doc/usr/share/qt3/doc/html/linguist*
	
	## qt3-assistant
	# remove docs from qt3-doc for qt-assistant
	rm -rf `pwd`/debian/qt3-doc/usr/share/qt3/doc/html/assistant*

	## all packages
	# install the 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
	
binary-indep: build install

	# Build architecture-independent files here.
	dh_testdir
	dh_testroot
	
	dh_installdocs -i -XREADME.Debian
	dh_installchangelogs -i changes-3.1.1
								
	find doc/man -path \*/CVS -prune -o -print | cpio -pmd $(P_DOC)/usr/share/qt3/doc/
			
	-rm -f `find $(P_DOC)/usr/share/qt3/doc/ -name "*.o"`
	find $(P_DOC) -type f -perm +0100 | xargs --no-run-if-empty rm -f

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

	# other i18n files
	for a in `cd translations/ && find . -name 'qt_*.qm' | sed 's/qt_//' | sed 's/\.qm//' | sed 's/\.\///g'`; do \
	install -D `pwd`/translations/qt_"$$a".qm `pwd`/debian/libqt3-i18n/usr/share/qt3/translations/qt_"$$a".qm; done

	# logo32.png
	cp `pwd`/doc/html/logo32.png `pwd`/debian/qt3-doc/usr/share/qt3/doc/html/

	# remove qmake html docu from qt3-doc
	rm -rf `pwd`/debian/qt3-doc/usr/share/qt3/doc/html/qmake*

	# create examples package
	install -d `pwd`/debian/doc/qt3-examples/tools/{designer,linguist}
	cp -ax examples `pwd`/debian/doc/qt3-examples
	cp -ax tutorial `pwd`/debian/doc/qt3-examples
	cp -ax tools/designer/examples `pwd`/debian/doc/qt3-examples/tools/designer/
	cp -ax tools/linguist/tutorial `pwd`/debian/doc/qt3-examples/tools/linguist/
	for a in `cd $(DEBIAN)/doc/qt3-examples/ && find $(DEBIAN)/doc/qt3-examples/ -name 'tt1'`; do rm -f "$$a"; done
	for a in `cd $(DEBIAN)/doc/qt3-examples/ && find $(DEBIAN)/doc/qt3-examples/ -name 'tt2'`; do rm -f "$$a"; done
	for a in `cd $(DEBIAN)/doc/qt3-examples/ && find $(DEBIAN)/doc/qt3-examples/ -name 'tt3'`; do rm -f "$$a"; done
	for a in `cd $(DEBIAN)/doc/qt3-examples/ && find $(DEBIAN)/doc/qt3-examples/ -name '.moc'`; do rm -rf "$$a"; done
	for a in `cd $(DEBIAN)/doc/qt3-examples/ && find $(DEBIAN)/doc/qt3-examples/ -name '.obj'`; do rm -rf "$$a"; done
	for a in `cd $(DEBIAN)/doc/qt3-examples/ && find $(DEBIAN)/doc/qt3-examples/ -name 'Makefile'`; do rm -f "$$a"; done
	install -D `pwd`/debian/maintain/build-examples.sh `pwd`/debian/doc/qt3-examples/build-examples
	chmod 755 `pwd`/debian/doc/qt3-examples/build-examples
	cd `pwd`/debian/doc/ && tar cvvfz qt3-examples.tar.gz qt3-examples/
	install -D `pwd`/debian/doc/qt3-examples.tar.gz `pwd`/debian/qt3-examples/usr/share/doc/qt3-examples/qt3-examples.tar.gz
	
	# build attic package and copy it to libqt3-ompat-headers
	cd `pwd`/src/ && tar cvvfz attic.tar.gz attic/
	install -D `pwd`/src/attic.tar.gz `pwd`/debian/libqt3-compat-headers/usr/share/doc/libqt3-compat-headers/attic.tar.gz
	rm -rf `pwd`/src/attic.tar.gz

	# proceed
	dh_compress -i -Xhtml/
	dh_link -i
	
	dh_fixperms -i
	dh_installdeb -i
	
	dh_perl -i
	dh_shlibdeps -i
	
	# fix shlibdeps madness
	for a in `find debian/ -name '*.substvars'`; do cat "$$a" | sed 's/, xlibs (>> 4.1.0)//g' > "$$a".new &&  mv "$$a.new" "$$a"; done

	dh_gencontrol -i
	dh_md5sums -i
	
	dh_builddeb -i
	
binary-arch: build install

	dh_testdir
	dh_testroot
	dh_installdirs -a

	## create qt3-apps-dev-package
	install -d $(P_APPSDEV)/usr/include/qt3/
	cp `pwd`/tools/designer/interfaces/*.h $(P_APPSDEV)/usr/include/qt3/
	cp `pwd`/tools/designer/editor/*.h $(P_APPSDEV)/usr/include/qt3/
	rm -rf `pwd`/debian/qt3-apps-dev/usr/include/qt3/preferences.ui.h
	
	## qvfb package
	install -D `pwd`/tools/qvfb/pda.skin `pwd`/debian/qt3-dev-tools-embedded/etc/X11/qvfb/pda.skin
	install -D `pwd`/tools/qvfb/pda_down.png `pwd`/debian/qt3-dev-tools-embedded/usr/share/qvfb/pda_down.png
	install -D `pwd`/tools/qvfb/pda_up.png `pwd`/debian/qt3-dev-tools-embedded/usr/share/qvfb/pda_up.png
	
	# language file for linguist
	install -d `pwd`/debian/qt3-linguist/usr/share/doc/qt3-linguist/
	cp translations/qt_untranslated.ts `pwd`/debian/qt3-linguist/usr/share/doc/qt3-linguist/

	## i18n files for designer, linguist and assistant
	(cd `pwd`/tools/designer/designer/ && lrelease designer.pro)
	for a in `cd tools/designer/designer/ && find . -name 'designer_*.qm' | sed 's/designer_//' | sed 's/\.qm//' | sed 's/\.\///g'`; do \
	install -D tools/designer/designer/designer_"$$a".qm `pwd`/debian/qt3-designer/usr/share/qt3/translations/designer_"$$a".qm; done
	rm -rf `pwd`/tools/designer/designer/*.qm
		
	(cd `pwd`/tools/assistant/ && lrelease assistant.pro)
	for a in `cd tools/assistant/ && find . -name 'assistant_*.qm' | sed 's/assistant_//' | sed 's/\.qm//' | sed 's/\.\///g'`; do \
	install -D tools/assistant/assistant_"$$a".qm `pwd`/debian/qt3-assistant/usr/share/qt3/translations/assistant_"$$a".qm; done
	rm -rf `pwd`/tools/assistant/*.qm

	(cd `pwd`/tools/linguist/linguist/ && lrelease linguist.pro)
	for a in `cd tools/linguist/linguist/ && find . -name 'linguist_*.qm' | sed 's/linguist_//' | sed 's/\.qm//' | sed 's/\.\///g'`; do \
	install -D tools/linguist/linguist/linguist_"$$a".qm `pwd`/debian/qt3-linguist/usr/share/qt3/translations/linguist_"$$a".qm; done
	rm -rf `pwd`/tools/linguist/linguist/*.qm

	# desktop lnk files
	install -D debian/maintain/designer-qt3.desktop `pwd`/debian/qt3-designer/usr/share/applnk/Development/designer-qt3.desktop
	install -D debian/maintain/linguist.desktop `pwd`/debian/qt3-linguist/usr/share/applnk/Development/linguist.desktop
	
	for a in assistant qtconfig; do install -d `pwd`/debian/qt3-"$$a"/usr/share/applnk/System/ && \
	cp debian/maintain/"$$a".desktop `pwd`/debian/qt3-"$$a"/usr/share/applnk/System/; done

	# include logo32 for every program
	cd `pwd`/debian/qt3-designer/usr/share/qt3/doc/html && for a in `find . -name '*.html'`; \
	do cat "$$a" | sed 's/logo32/logo32-designer/g' > "$$a".new && mv "$$a".new "$$a"; done

	cd `pwd`/debian/qt3-linguist/usr/share/qt3/doc/html && for a in `find . -name '*.html'`; \
	do cat "$$a" | sed 's/logo32/logo32-linguist/g' > "$$a".new && mv "$$a".new "$$a"; done

	cd `pwd`/debian/qt3-assistant/usr/share/qt3/doc/html && for a in `find . -name '*.html'`; \
	do cat "$$a" | sed 's/logo32/logo32-assistant/g' > "$$a".new && mv "$$a".new "$$a"; done

	cd `pwd`/debian/qt3-dev-tools/usr/share/qt3/doc/html && for a in `find . -name '*.html'`; \
	do cat "$$a" | sed 's/logo32/logo32-qmake/g' > "$$a".new && mv "$$a".new "$$a"; done
	
	for a in designer linguist assistant; do install -D `pwd`/doc/html/logo32.png `pwd`/debian/qt3-"$$a"/usr/share/qt3/doc/html/logo32-"$$a".png; done
	install -D `pwd`/doc/html/logo32.png `pwd`/debian/qt3-dev-tools/usr/share/qt3/doc/html/logo32-qmake.png

	# rename some binaries to make qt2/3 installations possible
	mv `pwd`/debian/qt3-designer/usr/bin/designer `pwd`/debian/qt3-designer/usr/bin/designer-qt3
	mv `pwd`/debian/qt3-dev-tools/usr/bin/uic `pwd`/debian/qt3-dev-tools/usr/bin/uic-qt3
	mv `pwd`/debian/qt3-dev-tools/usr/bin/moc `pwd`/debian/qt3-dev-tools/usr/bin/moc-qt3
	install -D debian/maintain/man/designer.1 `pwd`/debian/qt3-designer/usr/share/man/man1/designer-qt3.1 			
	install -D doc/man/man1/moc.1 `pwd`/debian/qt3-dev-tools/usr/share/man/man1/moc-qt3.1
	install -D doc/man/man1/uic.1 `pwd`/debian/qt3-dev-tools/usr/share/man/man1/uic-qt3.1
	
	# install the manpages we have
	dh_installman -pqt3-designer debian/maintain/man/createcw.1
	dh_installman -pqt3-dev-tools-compat debian/maintain/man/qt20fix.1 debian/maintain/man/mergetr.1 debian/maintain/man/findtr.1 debian/maintain/man/msg2qm.1
	dh_installman -pqt3-dev-tools-embedded debian/maintain/man/makeqpf.1 debian/maintain/man/qvfb.1
	dh_installman -pqt3-dev-tools doc/man/man1/lupdate.1 doc/man/man1/lrelease.1 debian/maintain/man/qembed.1
	dh_installman -pqt3-qtconfig debian/maintain/man/qtconfig.1

	# copy README.Debian
	for a in libqt3-dev libqt3-mt-dev qt3-doc; do install -D `pwd`/debian/README.Debian `pwd`/debian/"$$a"/usr/share/doc/"$$a"/README.Debian; done

	# run remaining debhelper scripts
	dh_installdocs -a -XREADME.Debian
	dh_installmenu -a
	
	dh_installchangelogs -a changes-3.1.1

	dh_link -a
	dh_strip -a 
	dh_compress -a
	dh_fixperms -a
	
	# run remaining debhelper scripts
	dh_makeshlibs -a -V
	dh_installdeb -a
	dh_perl -a
	dh_shlibdeps -a -l`pwd`/debian/libqt3c102/usr/lib:`pwd`/debian/libqt3c102-mt/usr/lib
       
	# fix shlibdeps madness
	for a in `find debian/ -name '*.substvars'`; do cat "$$a" | sed 's/, xlibs (>> 4.1.0)//g' > "$$a".new &&  mv "$$a.new" "$$a"; done

	dh_gencontrol
	dh_md5sums -a
	dh_builddeb -a

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