#!/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_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
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 depends on the pybind11 version it was built against,
# if pybind11.h will be used in C++ code fragments in python scripts.
# But pybind11 follows semantic versioning, so should have backwards compatibility
# with minor and patch version updates.
# 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_VERSION=$(shell echo $(PYBIND11_UPSTREAM_VERSION) | sed "s/^\([^.]*\).*/\1/")
PYBIND11_X_Y_VERSION=$(shell echo $(PYBIND11_UPSTREAM_VERSION) | sed "s/^\(.*\)\.\([^.]*\)$$/\1/")
PYBIND11_VERSION=$(PYBIND11_X_Y_VERSION)
PYBIND11_NEXT_VERSION=$(shell echo $$(( $(PYBIND11_X_VERSION) + 1 )) )

# some arches (mostly 32 bit) don't provide enough memory to build with debug symbols
ARCH_NO_DEBUG_SYMBOLS := armhf i386 mips64el mipsel hppa m68k powerpc x32
ifeq ($(findstring $(DEB_BUILD_ARCH),$(ARCH_NO_DEBUG_SYMBOLS)),)
  MSHR_BUILDFLAGS := $(shell dpkg-buildflags --get CXXFLAGS )
else
# remove -g from build flags on weak arches
  MSHR_BUILDFLAGS := $(shell dpkg-buildflags --get CXXFLAGS | sed 's/-g //' )
endif

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 $(MSHR_BUILDFLAGS)" \
	$(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 \
	      --no-parallel

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_VERSION) -Vpybind11:Next-Upstream-Version=$(PYBIND11_NEXT_VERSION)

# debug symbols (-g) have been switched off in CMAKE_OPTS for some arches, so no point building dbgsym packages
override_dh_strip:
	case " $(ARCH_NO_DEBUG_SYMBOLS) " in \
	    *\ $(DEB_HOST_ARCH)\ *) dh_strip --no-automatic-dbgsym;; \
	    *) dh_strip;; \
	esac

override_dh_dwz:
	case " $(ARCH_NO_DEBUG_SYMBOLS) " in \
	    *\ $(DEB_HOST_ARCH)\ *) echo "skipping dh_dwz since debug symbols (-g) have been switched off for $(DEB_HOST_ARCH)";; \
	    *) dh_dwz;; \
	esac

#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)
