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

# Allow test programs that uses OpenMPI to run
export OMPI_MCA_plm_rsh_agent=/bin/false

# Disable MPI on lam architectures
ifeq ($(DEB_BUILD_ARCH), armel)
ENABLE_MPI=OFF
else ifeq ($(DEB_BUILD_ARCH), mips)
ENABLE_MPI=OFF
else ifeq ($(DEB_BUILD_ARCH), mipsel)
ENABLE_MPI=OFF
else ifeq ($(DEB_BUILD_ARCH), s390)
ENABLE_MPI=OFF
else ifeq ($(DEB_BUILD_ARCH), sparc)
ENABLE_MPI=OFF
else
ENABLE_MPI=ON
endif

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

BUILDDIR = $(CURDIR)/debian/build
DOLFIN_HOME = $(CURDIR)/$(DEB_SRCDIR)
USCAN_DESTDIR := $(CURDIR)
PYVERS := $(shell pyversions --requested debian/control | tac -s' ')
DEFAULT_PYVER := $(shell pyversions -d)

CMAKE_OPTS := \
	-D CMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
	-D BUILD_SHARED_LIBS:BOOL=ON \
	-D CMAKE_SKIP_RPATH:BOOL=ON \
	-D CMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=OFF \
	-D DOLFIN_ENABLE_TRILINOS:BOOL=OFF \
	-D DOLFIN_ENABLE_CGAL:BOOL=$(ENABLE_CGAL) \
	-D DOLFIN_ENABLE_HDF5:BOOL=ON \
	-D DOLFIN_ENABLE_VTK:BOOL=ON \
	-D DOLFIN_ENABLE_QT:BOOL=ON \
	-D DOLFIN_ENABLE_PARMETIS:BOOL=OFF \
	-D DOLFIN_ENABLE_PASTIX:BOOL=OFF \
	-D DOLFIN_ENABLE_UNIT_TESTS:BOOL=OFF \
	-D SWIG_EXECUTABLE:FILEPATH=/usr/bin/swig2.0 \
        -D DOLFIN_ENABLE_MPI:BOOL=$(ENABLE_MPI) \
	-D PETSC_DIR:PATH=/usr/lib/petscdir/3.4.2 \
	-D SLEPC_DIR:PATH=/usr/lib/slepcdir/3.4.2 \
	-D DOLFIN_LIB_DIR:PATH=lib/$(DEB_BUILD_MULTIARCH) \
	-D DOLFIN_PKGCONFIG_DIR:PATH=lib/$(DEB_BUILD_MULTIARCH)/pkgconfig \
	$(DOLFIN_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"))
        DISTRIBUTION := sid
endif

ifneq (,$(findstring $(DISTRIBUTION),"raring saucy sid"))
        PYTHON_INCLUDE_DIR2 = /usr/include/$(DEB_BUILD_MULTIARCH)
        PYTHON_LIBRARY_DIR = /usr/lib/$(DEB_BUILD_MULTIARCH)
else
        PYTHON_INCLUDE_DIR2 = /usr/include
        PYTHON_LIBRARY_DIR = /usr/lib
endif

%:
	dh $@ --buildsystem=cmake \
	      --builddirectory=$(BUILDDIR)-$(DEFAULT_PYVER) \
	      --with python2

override_dh_compress:
	dh_compress -X.py -X.cpp -X.h -X.pdf -X.ufl

override_dh_auto_clean:
	for v in $(PYVERS); do \
	   rm -rf $(BUILDDIR)-$$v; \
	done

override_dh_auto_configure:
	for v in $(PYVERS); do \
	dh_auto_configure --builddirectory=$(BUILDDIR)-$$v -- \
	   -D PYTHON_EXECUTABLE:FILEPATH=/usr/bin/$$v \
	   -D PYTHON_LIBRARY:FILEPATH=$(PYTHON_LIBRARY_DIR)/lib$$v.so \
	   -D PYTHON_INCLUDE_DIR:PATH=/usr/include/$$v \
	   -D PYTHON_INCLUDE_DIR2:PATH=$(PYTHON_INCLUDE_DIR2)/$$v \
	   -- $(CMAKE_OPTS) ;\
	done

override_dh_auto_build:
	for v in $(PYVERS); do \
	   dh_auto_build --builddirectory=$(BUILDDIR)-$$v; \
	done

override_dh_auto_install:
	for v in $(PYVERS); do \
	   dh_auto_install --builddirectory=$(BUILDDIR)-$$v; \
	done
	dh_numpy

override_dh_gencontrol:
	# We have a tight dependency on swig2.0 - #675207
	set -e; \
	swig_version=$$(dpkg-query -W swig2.0 | cut -f 2 | cut -d- -f 1); \
	next_swig_version="$${swig_version%.*}.$$(($${swig_version##*.} + 1))"; \
	echo "swig:Depends=swig2.0 (>= $$swig_version), swig2.0 (<< $$next_swig_version~)" >> debian/python-dolfin.substvars
	dh_gencontrol

override_dh_strip:
	dh_strip -a --package=libdolfin1.3 --dbg-package=libdolfin1.3-dbg -Xpython
	dh_strip -a --package=python-dolfin --dbg-package=python-dolfin-dbg

override_dh_installman:
	dh_installman


DEB_SOURCE_PACKAGE := $(strip $(shell egrep '^Source: ' debian/control | cut -f 2 -d ':'))
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 -n -e 's/\+dfsg.*$$//p')

.PHONY: get-orig-source override_dh_strip
get-orig-source:
	set -ex ; \
	TMPDIR=`mktemp -d $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig.XXXXXX` ; \
	uscan --force-download --check-dirname-level 0 --destdir "$$TMPDIR" --download-version $(DEB_STRIPPED_UPSTREAM_VERSION) --upstream-version $(DEB_STRIPPED_UPSTREAM_VERSION); \
	tar -C "$$TMPDIR" --strip-components 1 -xzf "$$TMPDIR"/$(DEB_SOURCE_PACKAGE)_$(DEB_STRIPPED_UPSTREAM_VERSION).orig.tar.gz ; \
	rm -rf "$$TMPDIR"/demo/undocumented/waveguide/waveguide_TM_cutoff_modes.pdf ; \
	find "$$TMPDIR" -name "*.pyc" | xargs rm ; \
	rm -f "$$TMPDIR"/$(DEB_SOURCE_PACKAGE)*.tar.gz ; \
	mv "$$TMPDIR" $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig ; \
	GZIP=-9 tar -czf $(CURDIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig ; \
	rm -rf $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION).orig
