#!/usr/bin/make -f

# FIXME: Is this needed?
#CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
#CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS)
#CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS)
#LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

DEB_BUILD_MULTIARCH ?= $(shell dpkg-architecture -qDEB_BUILD_MULTIARCH)

MSHR_HOME = $(CURDIR)/$(DEB_SRCDIR)
USCAN_DESTDIR := $(CURDIR)
PY3VERS := $(shell py3versions --requested debian/control | tac -s' ')
PYVERS = $(PY3VERS)

DEB_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')
DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -e 's/\+dfsg.*$$//p')

FENICS_RELEASE_VERSION=$(DEB_STRIPPED_UPSTREAM_VERSION)
FENICS_MAJOR_VERSION=$(shell echo $(FENICS_RELEASE_VERSION) | sed "s/^\([^.]*\)\..*$$/\1/")
FENICS_MINOR_VERSION=$(shell echo $(FENICS_RELEASE_VERSION) | sed "s/^\([^.]*\)\.\([^.]*\)\..*$$/\2/")
FENICS_VERSION=$(FENICS_MAJOR_VERSION).$(FENICS_MINOR_VERSION)
FENICS_NEXT_VERSION=$(FENICS_MAJOR_VERSION).$(shell echo $$(( $(FENICS_MINOR_VERSION) + 1 )) )

# dolfin has a strict dependency on the pybind11 version it was built against,
# if pybind11.h will be used in C++ code fragments in python scripts.
# Extract pybind11 version from pybind11-dev
PYBIND11_DEB_VERSION=$(shell dpkg -s pybind11-dev | awk '/Version:/ {print $$2}')
# extract the current pybind11 version X.Y.Z (drop epoch and debian package version)
PYBIND11_UPSTREAM_VERSION=$(shell echo $(PYBIND11_DEB_VERSION) | sed "s/^.[^:]*://; s/-[^-]*$$//")
PYBIND11_X_Y_VERSION=$(shell echo $(PYBIND11_UPSTREAM_VERSION) | sed "s/^\(.*\)\.\([^.]*\)$$/\1/")
PYBIND11_Z_VERSION=$(shell echo $(PYBIND11_UPSTREAM_VERSION) | sed "s/^\(.*\)\.\([^.]*\)$$/\2/")
PYBIND11_NEXT_UPSTREAM_VERSION=$(PYBIND11_X_Y_VERSION).$(shell echo $$(( $(PYBIND11_Z_VERSION) + 1 )) )

# strip -g from dpkg-buildflags to reduce memory requirements of the build (needed to build on Debian 32 bit and on ubuntu)
CMAKE_OPTS := \
	-D CMAKE_BUILD_TYPE:STRING=Release \
	-D BUILD_SHARED_LIBS:BOOL=ON \
	-D USE_SYSTEM_CGAL:BOOL=ON \
	-D USE_SYSTEM_TETGEN:BOOL=ON \
	-D ENABLE_MSHRABLE:BOOL=ON \
	-D CMAKE_SKIP_RPATH:BOOL=ON \
	-D CMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=OFF \
	-D INSTALL_LIB_DIR:PATH=lib/$(DEB_BUILD_MULTIARCH) \
	-D CMAKE_CXX_FLAGS:STRING="-fpermissive $(shell dpkg-buildflags --get CXXFLAGS | sed 's/-g //' )" \
	$(MSHR_HOME)

ifeq (,$(DISTRIBUTION))
	DISTRIBUTION := $(shell dpkg-parsechangelog --format rfc822 | sed -ne "s/^Distribution: //p")
endif

ifneq ($(DISTRIBUTION),$(findstring $(DISTRIBUTION),"squeeze wheezy lucid maverick natty oneiric precise quantal raring saucy trusty"))
	DISTRIBUTION := sid
endif

%:
	dh $@ --buildsystem=cmake \
	      --with python3

override_dh_auto_clean:
	dh_auto_clean
	rm -rf python/build*

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_OPTS) ;\

override_dh_auto_install:
	dh_auto_install
	set -e; \
	for v in $(PYVERS); do \
	   cd python; \
	   CMAKE_PREFIX_PATH=$(CURDIR)/debian/tmp/usr $$v setup.py build --build-base=$(CURDIR)/python/build-$$v install --root=$(CURDIR)/debian/tmp --install-layout=deb; \
	   cd ..; \
	done
	dh_numpy3

override_dh_gencontrol:
	dh_gencontrol -- -Vfenics:Upstream-Version=$(FENICS_VERSION) \
		-Vpybind11:Upstream-Version=$(PYBIND11_UPSTREAM_VERSION) -Vpybind11:Next-Upstream-Version=$(PYBIND11_NEXT_UPSTREAM_VERSION)

# debug symbols (-g) have been switched off in CMAKE_OPTS, so no point building dbgsym packages
override_dh_strip:
	dh_strip --no-automatic-dbgsym

#override_dh_shlibdeps:
#	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

.PHONY: get-orig-source
get-orig-source:
	uscan --force-download --verbose --destdir $(USCAN_DESTDIR)
