#!/usr/bin/make -f

# 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_HOST_ARCH		?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_CPU	?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)

ifeq ($(DEB_HOST_ARCH_OS),hurd)
	PLATFORM_ARG = hurd-g++
else #hurd
ifeq ($(DEB_HOST_ARCH),sparc)
	PLATFORM_ARG = linux-g++-sparc
else #sparc
	PLATFORM_ARG = linux-g++
endif #sparc
endif #hurd

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

# build variables (directorys)
DEBIAN = $(shell pwd)/debian
P_LIBS = $(DEBIAN)/libqt3-mt
P_DOC = $(DEBIAN)/qt3-doc
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"				\
		-sysconfdir	"/etc/qt3"			\
		-datadir	"/usr/share/qt3"		\
		-headerdir	"/usr/include/qt3"		\
		-docdir		"/usr/share/qt3/doc"		\
		-plugindir	"/usr/lib/qt3/plugins"		\
		-translationdir	"/usr/share/qt3/translations"	\
						\
		-thread				\
		-shared				\
		-fast				\
		-no-exceptions			\
		-platform $(PLATFORM_ARG)	\
						\
		-nis				\
		-no-pch				\
		-cups				\
		-stl				\
		-ipv6				\
						\
		-sm				\
		-xshape				\
		-xinerama			\
		-xcursor			\
		-xrandr				\
		-xrender			\
		-xft				\
		-tablet				\
		-xkb				\
						\
		-system-zlib			\
		-system-libpng			\
		-system-libmng			\
		-system-libjpeg			\
		-system-nas-sound		\
						\
		-enable-opengl			\
		-dlopen-opengl			\
						\
		-qt-gif				\
		-qt-imgfmt-png			\
		-qt-imgfmt-jpeg			\
		-plugin-imgfmt-mng		\
						\
		-plugin-sql-odbc		\
		-plugin-sql-psql		\
		-plugin-sql-mysql		\
		-plugin-sql-sqlite		\
						\
		-I/usr/include/mysql		\
		-I/usr/include/freetype2	\
		-I`pg_config --includedir`	\
						\
		-L/usr/lib/$(DEB_HOST_MULTIARCH)	\
						\
		-lfontconfig			\
		-inputmethod			\
		# End of CONFIGURE_OPTS

interbase_archs := i386 amd64 sparc powerpc
ifneq ($(DEB_HOST_ARCH_CPU),$(findstring $(DEB_HOST_ARCH_CPU),$(interbase_archs)))
	CONFIGURE_OPTS += -no-sql-ibase
	IBASE = -Nlibqt3-mt-ibase
else
	IBASE =
endif

ifeq ($(DEB_HOST_ARCH_OS),hurd)
	CONFIGURE_OPTS += -no-sql-ibase
	IBASE = -Nlibqt3-mt-ibase
endif

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

build: libqt-thread-stamp

	touch build-stamp

libqt-thread-stamp: $(QUILT_STAMPFN)

	@echo "QTDIR is ${QTDIR}"

	dh_testdir

	echo yes | ./configure $(CONFIGURE_OPTS)

	rm -rf include/qinputcontext.h include/qinputcontextfactory.h include/qinputcontextplugin.h
	ln -s ../src/kernel/qinputcontext.h include/qinputcontext.h
	ln -s ../src/inputmethod/qinputcontextfactory.h include/qinputcontextfactory.h
	ln -s ../src/inputmethod/qinputcontextplugin.h include/qinputcontextplugin.h

	# proceed
	$(MAKE) sub-src sub-plugins sub-tools
	$(MAKE) -C src INSTALL_ROOT=$(TMP_INSTALL) install_target
	$(MAKE) INSTALL_ROOT=$(TMP_INSTALL) install
	$(MAKE) INSTALL_ROOT=$(TMP_INSTALL) plugins-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/qt3/
	cat .qmake.cache | sed "s#$(QTDIR)#/usr/share/qt3#g" > $(P_QTMTDEV)/usr/share/qt3/.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

	install -m 644 -D include/qinputcontext.h $(P_HEADERS)/usr/include/qt3/qinputcontext.h
	install -m 644 -D include/qinputcontextfactory.h $(P_HEADERS)/usr/include/qt3/qinputcontextfactory.h
	install -m 644 -D include/qinputcontextplugin.h $(P_HEADERS)/usr/include/qt3/qinputcontextplugin.h

	touch libqt-thread-stamp

clean: unpatch

	dh_testdir

	-rm -rf debian/patched
	-rm -rf build-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 \
		config.tests/unix/ipv6/ipv6test config.tests/unix/ipv6/ipv6test.o config.tests/unix/ptrsize/ptrsizetest.o \
		config.tests/unix/ptrsize/ptrsizetest config.tests/unix/endian/endiantest.o \
		config.tests/unix/endian/endiantest lib/qt-mt.pc extensions/nsplugin/examples/trivial/libtrivial.prl \
		extensions/nsplugin/examples/grapher/libgrapher.prl bin/designer

	rm -f qmake/*o
	rm -rf include/qinputcontext.h include/qinputcontextfactory.h include/qinputcontextplugin.h
	rm -f src/.obj/debug-shared-mt/*o plugins/src/inputmethods/imsw-multi/.obj/debug-shared-mt/*o plugins/inputmethods/*so
	rm -f plugins/src/inputmethods/imsw-none/.obj/debug-shared-mt/*o plugins/src/inputmethods/simple/.obj/debug-shared-mt/*o plugins/src/inputmethods/xim/.obj/debug-shared-mt/*o

	# 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

	cd examples/ && find . -name '.obj' | xargs rm -rf

	dh_clean

install: build

	dh_testdir
	dh_testroot

	dh_clean -i
	dh_installdirs

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

	dh_install $(IBASE) --sourcedir=$(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
	#typo bugfix
	sed -i -e 's/reveives/receives/' $(P_DOC)/usr/share/qt3/doc/html/qwidget.html

	## 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 doc/html/designer*.dcf | 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 doc/html/linguist*.dcf | 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

	chmod 644 debian/qt3-dev-tools/usr/share/qt3/mkspecs/aix-g++-64/qplatformdefs.h
	chmod 644 debian/qt3-dev-tools/usr/share/qt3/mkspecs/macx-pbuilder/Info.plist.app

binary-indep: build install

	# Build architecture-independent files here.
	dh_testdir
	dh_testroot

	dh_installdocs -i -XREADME.Debian
	dh_installchangelogs -i changes-3.3.8
	dh_installchangelogs -i changes-3.3.8b

	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/

	for i in $(P_DOC)/usr/share/qt3/doc/doc/man/man3/* ; do mv $$i $(P_DOC)/usr/share/man/man3/ ; done

	-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 -m644 -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
	install -d `pwd`/debian/doc/qt3-examples/tools/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

	# 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 -m644 -D `pwd`/tools/qvfb/pda.skin `pwd`/debian/qt3-dev-tools-embedded/etc/qt3/qvfb/pda.skin
	install -m644 -D `pwd`/tools/qvfb/pda_down.png `pwd`/debian/qt3-dev-tools-embedded/usr/share/qvfb/pda_down.png
	install -m644 -D `pwd`/tools/qvfb/pda_up.png `pwd`/debian/qt3-dev-tools-embedded/usr/share/qvfb/pda_up.png

	# remove utterly ugle symlink
	rm -rf `pwd`/debian/qt3-dev-tools//usr/share/qt3/mkspecs/linux-g++/linux-g++

	# 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/

	# fix that stupid friggin professional file
	perl -pi -e 's{\$$\$$QT_SOURCE_TREE}{$(QTDIR)}' src/qt_professional.pri

	## 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 -m644 -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 -m644 -D tools/assistant/assistant_"$$a".qm `pwd`/debian/qt3-assistant/usr/share/qt3/translations/assistant_"$$a".qm; done
	rm -rf `pwd`/tools/assistant/*.qm `pwd`/debian/libqt3-i18n/usr/share/qt3/translations/assistant_de.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 -m644 -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 -m644 -D debian/maintain/designer-qt3.desktop `pwd`/debian/qt3-designer/usr/share/applications/designer-qt3.desktop
	install -m644 -D debian/maintain/linguist-qt3.desktop `pwd`/debian/qt3-linguist/usr/share/applications/linguist-qt3.desktop

	install -m644 -D debian/maintain/assistant-qt3.desktop `pwd`/debian/qt3-assistant/usr/share/applications/assistant-qt3.desktop
	install -m644 -D debian/maintain/qt3config.desktop `pwd`/debian/qt3-qtconfig/usr/share/applications/qt3config.desktop

	# 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 -m644 -D `pwd`/doc/html/logo32.png `pwd`/debian/qt3-"$$a"/usr/share/qt3/doc/html/logo32-"$$a".png; done
	install -m644 -D `pwd`/doc/html/logo32.png `pwd`/debian/qt3-dev-tools/usr/share/qt3/doc/html/logo32-qmake.png

	# fix qmake.conf files
	cd `pwd`/debian/qt3-dev-tools/usr/share/qt3/mkspecs/ && for a in *; do cd "$$a" && cat qmake.conf | sed 's/\$$(QTDIR)\/include/\/usr\/share\/qt3\/include/g' | sed 's/\$$(QTDIR)/\/usr\/share\/qt3/g' | \
	sed 's/\-I\/usr\/include/&\/qt3/g' >> qmake.conf.new && mv qmake.conf.new qmake.conf && cd ../; done

	# 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

	# rename some binaries to make qt3/4 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
	mv `pwd`/debian/qt3-dev-tools/usr/bin/lupdate `pwd`/debian/qt3-dev-tools/usr/bin/lupdate-qt3
	mv `pwd`/debian/qt3-dev-tools/usr/bin/lrelease `pwd`/debian/qt3-dev-tools/usr/bin/lrelease-qt3
	mv `pwd`/debian/qt3-dev-tools/usr/bin/qmake `pwd`/debian/qt3-dev-tools/usr/bin/qmake-qt3
	mv `pwd`/debian/qt3-qtconfig/usr/bin/qtconfig `pwd`/debian/qt3-qtconfig/usr/bin/qtconfig-qt3
	mv `pwd`/debian/qt3-assistant/usr/bin/assistant `pwd`/debian/qt3-assistant/usr/bin/assistant-qt3
	mv `pwd`/debian/qt3-linguist/usr/bin/linguist `pwd`/debian/qt3-linguist/usr/bin/linguist-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 -D doc/man/man1/lrelease.1 `pwd`/debian/qt3-dev-tools/usr/share/man/man1/lrelease-qt3.1
	install -D doc/man/man1/lupdate.1 `pwd`/debian/qt3-dev-tools/usr/share/man/man1/lupdate-qt3.1
	install -D debian/maintain/man/qtconfig.1 `pwd`/debian/qt3-qtconfig/usr/share/man/man1/qtconfig-qt3.1
	install -D debian/maintain/man/linguist.1 `pwd`/debian/qt3-linguist/usr/share/man/man1/linguist-qt3.1

	dh_link -pqt3-designer usr/share/qt3/doc/html usr/share/doc/qt3-designer/html
	dh_link -pqt3-assistant usr/share/qt3/doc/html usr/share/doc/qt3-assistant/html
	dh_link -pqt3-linguist usr/share/qt3/doc/html usr/share/doc/qt3-linguist/html
	dh_link -pqt3-dev-tools usr/share/qt3/doc/html usr/share/doc/qt3-dev-tools/html

	# 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 debian/maintain/man/qembed.1

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

	# install the qmake binary
	rm -rf `pwd`/debian/qt3-dev-tools/usr/bin/qmake
	install -D `pwd`/qmake/qmake `pwd`/debian/qt3-dev-tools/usr/bin/qmake-qt3

	# Install source for the designer tools, such as createcw.
	cp -ra tools/designer/tools debian/qt3-designer/usr/share/qt3/tools
	rm -f debian/qt3-designer/usr/share/qt3/tools/createcw/createcw
	rm -rf debian/qt3-designer/usr/share/qt3/tools/createcw/.moc
	rm -rf debian/qt3-designer/usr/share/qt3/tools/createcw/.obj
	rm -f debian/qt3-designer/usr/share/qt3/tools/createcw/Makefile
	rm -f debian/qt3-designer/usr/share/qt3/tools/conv2ui/conv2ui
	rm -rf debian/qt3-designer/usr/share/qt3/tools/conv2ui/.moc
	rm -rf debian/qt3-designer/usr/share/qt3/tools/conv2ui/.obj
	rm -f debian/qt3-designer/usr/share/qt3/tools/conv2ui/Makefile

	# run remaining debhelper scripts
	dh_installdocs -a $(IBASE) -XREADME.Debian
	dh_installmenu -a $(IBASE)

	dh_installchangelogs -a $(IBASE) changes-3.3.8
	dh_installchangelogs -a $(IBASE) changes-3.3.8b

	dh_link -a $(IBASE)
	dh_strip -a $(IBASE) --dbg-package=qt-x11-free-dbg
	dh_compress -a $(IBASE)
	dh_fixperms -a $(IBASE)

	# run remaining debhelper scripts
	dh_makeshlibs -a $(IBASE) -V
	dh_installdeb -a $(IBASE)
	dh_perl -a $(IBASE)
	dh_shlibdeps -a $(IBASE) -l`pwd`/debian/libqt3-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

	# fix conffiles file for qt3-dev-tools-embedded
	echo "/etc/qt3/qvfb/pda.skin" > `pwd`/debian/qt3-dev-tools-embedded/DEBIAN/conffiles

	dh_gencontrol -a $(IBASE)
	dh_md5sums -a $(IBASE)
	dh_builddeb -a $(IBASE)

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