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

# Set tqt root directory
export TQTDIR=$(QTDIR)

# 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 = \
		-thread -debug -stl -system-zlib -system-libpng -system-libmng -system-libjpeg -qt-gif -qt-imgfmt-png -qt-imgfmt-jpeg -plugin-imgfmt-mng -prefix /usr \
		-I/usr/include/mysql		\
		-I/usr/include/freetype2	\
		-I`pg_config --includedir`	\
						\
		-L/usr/lib/$(DEB_HOST_MULTIARCH)	\
		# End of CONFIGURE_OPTS

build: libqt-thread-stamp

	touch build-stamp

libqt-thread-stamp: $(QUILT_STAMPFN)

	@echo "QTDIR is ${QTDIR}"

	dh_testdir

	echo yes | ./configure $(CONFIGURE_OPTS)

	# 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

	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

	# 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 $(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)

binary-indep: build install

	# Build architecture-independent files here.
	dh_testdir
	dh_testroot

	dh_installchangelogs -i changes-3.4.0

	# 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

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

	dh_installchangelogs -a $(IBASE) changes-3.4.0

	dh_link -a $(IBASE)
	dh_strip -a $(IBASE) --dbg-package=libtqtinterface-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/libtqtinterface/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 -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
