#!/usr/bin/make -f

# DH_VERBOSE=1

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

PYVERS=$(shell pyversions -vr)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
NUMJOBS=$(filter parallel=%,$(DEB_BUILD_OPTIONS))
SCONSPARFLAGS=$(subst parallel=,-j, $(NUMJOBS))
DESTDIR=debian/tmp/
%:
	dh $@ --with python2


build-python%: PYTHON=/usr/bin/python$*

override_dh_auto_configure:
	cp debian/custom.py .

build-python%:	
	scons $(SCONSPARFLAGS) prepare-install prepare-devel \
		PYTHON=$(PYTHON) \
		DOCS=False HAVE_PYDOC=False

override_dh_auto_build-arch: $(PYVERS:%=build-python%)

install-python%: PYTHON=/usr/bin/python$*

install-python%:
	scons $(SCONSPARFLAGS) install devel-install \
		PYTHON=$(PYTHON) \
		DOCS=False  HAVE_PYDOC=False 

override_dh_auto_install-arch: $(PYVERS:%=install-python%)


override_dh_auto_build-indep:
	scons $(SCONSPARFLAGS) docs 

override_dh_auto_install-indep:
	scons $(SCONSPARFLAGS) install-docs 


test-unittests:
	scons $(SCONSPARFLAGS) testsuite/unittests
ifeq ($(DEB_HOST_ARCH_OS),hurd) # problems with rpath on hurd
	LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) testsuite/unittests
else
	testsuite/unittests
endif

test-python%:
	python$* ipbori/ipbori -t 

override_dh_auto_test: test-unittests $(PYVERS:%=test-python%)

# Manually cleaning, since upstream is broken (Called before patches apply, too.)
override_dh_auto_clean:
	scons . --keep-going --clean || true
	dh_auto_clean
	rm -f custom.py

PBORIVER=0.8
PBORIREL=3
override_dh_strip:
	dh_strip -plibpolybori-${PBORIVER}-${PBORIREL} \
	  --dbg-package=libpolybori-${PBORIVER}-${PBORIREL}-dbg
	dh_strip -plibpolybori-groebner-${PBORIVER}-${PBORIREL} \
	  --dbg-package=libpolybori-groebner-${PBORIVER}-${PBORIREL}-dbg
	dh_strip -a -Nlibpolybori-${PBORIVER}-${PBORIREL} \
	  -Nlibpolybori-groebner-${PBORIVER}-${PBORIREL}



