#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

PYTHONS	:= $(shell pyversions -vr)
QTS=4

py_qt_version = $(shell dpkg -s python-qt$(strip $(1)) | grep '^Version' | sed 's,Version: \(.*\)-[^-]*$$,\1,')
libqwt5_version = $(shell dpkg -s libqwt5-qt$(strip $(1)) | grep '^Version' | sed 's,Version: \(.*\)-[^-]*$$,\1,')

#DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+)\+dfsg.*,\1,p')
DEB_UPSTREAM_VERSION=5.2.0
CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	chmod ugo+x qt4lib/PyQt4/Qwt5/grace.py
	set -e; for pyvers in ${PYTHONS};\
	do\
		for qtvers in ${QTS};\
		do\
			mkdir -p build/py$$pyvers-qt$$qtvers;\
			cp -Rl `ls . |grep -v build|grep -v debian` build/py$$pyvers-qt$$qtvers;\
			(set -e; cd build/py$$pyvers-qt$$qtvers/configure;\
			python$$pyvers configure.py -$$qtvers -I /usr/include/qwt-qt$$qtvers/ --extra-libs=qwt-qt$$qtvers --disable-numarray --disable-numeric);\
		done;\
	done
	touch configure-stamp

build: build-arch build-indep
build-arch: build-arch-stamp
build-arch-stamp: configure-stamp 
	dh_testdir
	set -e; for pyvers in ${PYTHONS};\
	do\
		for qtvers in ${QTS};\
		do\
			(set -e; cd build/py$$pyvers-qt$$qtvers/configure;\
			$(MAKE));\
		done;\
	done
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f sip/qwt5qt4/QwtTypes.sip
	rm -rf build
	dh_clean 

install: install-indep install-arch
install-indep: build-indep-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -i
	mkdir -p debian/tmp/usr/share/doc/python-qwt5-doc
	cp -a qt4examples debian/tmp/usr/share/doc/python-qwt5-doc


install-arch: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -s
	for pyvers in ${PYTHONS};\
	do\
		for qtvers in ${QTS};\
		do\
			export QTDIR=/usr/share/qt$$qtvers;\
			(cd build/py$$pyvers-qt$$qtvers/configure;\
			$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install);\
		done;\
	done

binary-indep: install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i ANNOUNCEMENT-$(DEB_UPSTREAM_VERSION)
	dh_installdocs -i
	dh_install -i --sourcedir=debian/tmp
	dh_compress -i --exclude=.py --exclude=.resX
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ANNOUNCEMENT-$(DEB_UPSTREAM_VERSION)
	dh_installdocs -a
	dh_install -a --sourcedir=debian/tmp
	dh_strip -a
	dh_compress -a --exclude=.py --exclude=.resX
	dh_python2 -a
	dh_sip -a
	echo 'pythonqt4:Depends=python-qt4 (>= $(call py_qt_version, 4))' >> debian/python-qwt5-qt4.substvars
	echo 'libqwt5qt4:Depends=libqwt5-qt4 (>= $(call libqwt5_version, 4))' >> debian/python-qwt5-qt4.substvars
	dh_numpy -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

get-orig-source:
	REV_DATE=$(shell dpkg-parsechangelog | sed -rne 's,^Version: .*cvs([0-9]+).*,\1,p'); \
	VER=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-+~]+).*,\1,p'); \
	if [ x$$REV_DATE = x ]; then \
	 uscan --force-download --rename --download-version=$$VER --destdir=. && (\
	 tar -xz --exclude 'Doc/html' --exclude 'qwt-[0-9\.]*' -f pyqwt5_$$VER.orig.tar.gz && \
	 tar -czf pyqwt5_$$VER+dfsg.orig.tar.gz PyQwt-$$VER; \
	 rm -f pyqwt5_$$VER.orig.tar.gz;\
	 rm -rf PyQwt-$$VER); \
	else \
	 cvs -rQz5 -d:pserver:anonymous@pyqwt.cvs.sourceforge.net:/cvsroot/pyqwt export \
		-D $$REV_DATE -d pyqwt5-$$VER~cvs$$REV_DATE+dfsg pyqwt5 && (\
	 find pyqwt5-$$VER~cvs$$REV_DATE+dfsg -name .cvsignore -type f -delete; \
	 rm -rf pyqwt5-$$VER~cvs$$REV_DATE+dfsg/Doc/html ;\
	 tar -zcf ./pyqwt5_$$VER~cvs$$REV_DATE+dfsg.orig.tar.gz pyqwt5-$$VER~cvs$$REV_DATE+dfsg; \
	 rm -rf pyqwt5-$$VER~cvs$$REV_DATE+dfsg); \
	fi

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure
