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

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

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

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

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

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


configure: configure-stamp
configure-stamp:
	dh_testdir
	chmod 644 qt4lib/PyQt4/Qwt3D/ezplot.py
	for pyvers in ${PYTHONS};\
	do\
		for qtvers in ${QTS};\
		do\
			export QTDIR=/usr/share/qt$$qtvers;\
			mkdir -p build/py$$pyvers-qt$$qtvers;\
			cp -Rl `ls . |grep -v build|grep -v debian` build/py$$pyvers-qt$$qtvers;\
			(cd build/py$$pyvers-qt$$qtvers/configure;\
			python$$pyvers configure.py -$$qtvers -I /usr/include/qwtplot3d-qt$$qtvers --extra-libs=qwtplot3d-qt$$qtvers -D GL2PS_HAVE_ZLIB);\
		done;\
	done
	touch $@

build: build-arch build-indep
build-arch: build-arch-stamp
build-arch-stamp: configure-stamp 
	dh_testdir
	for pyvers in ${PYTHONS};\
	do\
		for qtvers in ${QTS};\
		do\
			export QTDIR=/usr/share/qt$$qtvers;\
			(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 -rf build
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	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
	mkdir -p debian/tmp/usr/share/doc/python-qwt3d-doc/
	cp -a qt4examples debian/tmp/usr/share/doc/python-qwt3d-doc/
	cp -a Doc/html debian/tmp/usr/share/doc/python-qwt3d-doc/

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

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ANNOUNCEMENT-$(DEB_UPSTREAM_VERSION).TXT
	dh_installdocs -a
	dh_install -a --sourcedir=debian/tmp
	dh_link -a
	dh_strip -a
	dh_compress -a --exclude=.cpp --exclude=.h --exclude=.resX
	#echo 'python-qt4-gl:Depends=python-qt4-gl (>= $(call py_qt_version, 4))' >> debian/python-qwt3d-qt4.substvars
	dh_numpy -a
	dh_python2 -a
	dh_sip -a
	dh_installdeb -a
	dh_fixperms -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=.; \
	else \
	 cvs -rQz5 -d:pserver:anonymous@pyqwt.cvs.sourceforge.net:/cvsroot/pyqwt export \
	 	-D $$REV_DATE -d pyqwt3d-$$VER~cvs$$REV_DATE pyqwt3d && (\
	 find pyqwt3d-$$VER~cvs$$REV_DATE -name .cvsignore -type f -delete; \
         tar -zcf ./pyqwt3d_$$VER~cvs$$REV_DATE.orig.tar.gz pyqwt3d-$$VER~cvs$$REV_DATE; \
	 rm -rf pyqwt3d-$$VER~cvs$$REV_DATE); \
	fi

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