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

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

PY3VERS := $(shell py3versions -vs)
CURDIR  := $(shell pwd)
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.8

export DEB_BUILD_MAINT_OPTIONS=hardening=+pie,-fortify
export CFLAGS   := $(shell dpkg-buildflags --get CFLAGS)   $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export CPPFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed


configure: configure-stamp
configure-stamp:
	dh_testdir
	export QTDIR=/usr/share/qt5
	set -e ;\
	for py in $(PY3VERS) ; do \
		mkdir -p build/py$$py-qt5; \
		cp -Rl `ls . |grep -v build|grep -v debian` build/py$$py-qt5; \
		(cd build/py$$py-qt5/configure;\
		python$$py configure-qt5.py -5 -I /usr/include/qwtplot3d --extra-libs=qwtplot3d-qt5 \
			--sip-include-dirs=/usr/lib/python3/dist-packages/PyQt5/bindings/ \
			-D GL2PS_HAVE_ZLIB); \
	done
	touch $@

build: build-arch build-indep
build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
	dh_testdir
	export QTDIR=/usr/share/qt5
	set -e ;\
	for py in $(PY3VERS) ; do \
		mkdir -p build/py$$py-qt5; \
		cp -Rl `ls . |grep -v build|grep -v debian` build/py$$py-qt5; \
		(cd build/py$$py-qt5/configure && $(MAKE)); \
	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
	export QTDIR=/usr/share/qt5
	set -e ;\
	for py in $(PY3VERS) ; do \
		mkdir -p build/py$$py-qt5; \
		cp -Rl `ls . |grep -v build|grep -v debian` build/py$$py-qt5; \
		(cd build/py$$py-qt5/configure && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install); \
	done
	mkdir -p debian/tmp/usr/share/doc/python-qwt3d-doc/
	cp -a qt5examples 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 --no-automatic-dbgsym
	dh_compress -a --exclude=.cpp --exclude=.h --exclude=.resX
	dh_python3 -a
	dh_sip3 -a
	dh_installdeb -a
	dh_fixperms -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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