#!/usr/bin/make -f

BUILDDIR = $(CURDIR)/debian/build
DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -n -e 's/\.dfsg.*$$//p')

# PETSC_DIR is the location of PETSc.
export SLEPC_DIR=/usr/lib/slepc
export PETSC_DIR=/usr/lib/petsc

# Fix FTBS on armhf
export PETSC_ARCH=linux-gnu-c-opt
ifeq ("armhf", "$(DEB_HOST_ARCH)")
export PETSC_ARCH=linux-gnueabihf-c-opt
endif
ifeq ("armel", "$(DEB_HOST_ARCH)")
export PETSC_ARCH=linux-gnueabi-c-opt
endif

%:
	dh $@ --buildsystem=cmake --builddirectory=$(BUILDDIR) --parallel
export OMPI_MCA_plm_rsh_agent=/bin/false                #workaround to start MPI-applications in chroot

disable_mpi_archs = armel kfreebsd-amd64 kfreebsd-i386 mips mipsel
export LD_LIBRARY_PATH :=  $(CURDIR)/debian/tmp/usr/lib:$(LD_LIBRARY_PATH); 

ifeq (,$(filter $(DEB_HOST_ARCH),$(disable_mpi_archs)))
	extra_flags += \
	-DCMAKE_CXX_FLAGS="-DMPICH_SKIP_MPICXX -DOMPI_SKIP_MPICXX -fopenmp -lmpi -fPIC \
	                   -Wall $(shell dpkg-buildflags --get CXXFLAGS)" \
	-DENABLE_MPI:BOOL=ON
else
	extra_flags += \
	-DCMAKE_CXX_FLAGS="-DMPICH_SKIP_MPICXX -DOMPI_SKIP_MPICXX -fopenmp -fPIC \
	                   -Wall $(shell dpkg-buildflags --get CXXFLAGS)" \
	-DENABLE_MPI:BOOL=OFF
endif

override_dh_auto_configure:
	dh_auto_configure --buildsystem=cmake --builddirectory=$(BUILDDIR)/petsc --parallel -- $(extra_flags)
	dh_auto_configure --buildsystem=cmake --builddirectory=$(BUILDDIR)/sparskit --parallel -- $(extra_flags) -DENABLE_PETSC=0 -DENABLE_SPARSKIT=1
	sed -i 's/-lpthread/-lgfortran -lpthread/' debian/build/*/CMakeFiles/getdp.dir/link.txt

override_dh_auto_build:	
	echo "installing getdp"
	dh_auto_build --parallel --builddirectory=$(BUILDDIR)/petsc
	echo "installing getdp-sparskit"
	dh_auto_build --parallel --builddirectory=$(BUILDDIR)/sparskit
	mv $(BUILDDIR)/sparskit/getdp $(BUILDDIR)/sparskit/getdp-sparskit

override_dh_auto_install:
	echo "installing getdp"
	dh_auto_install --paralell  --builddirectory=$(BUILDDIR)/petsc

override_dh_auto_clean:
	rm -rf $(BUILDDIR)/petsc $(BUILDDIR)/sparskit

# Grab the version before +dfsg
DEB_SOURCE_PACKAGE = $(shell dpkg-parsechangelog | grep '^Source:' | cut -f 2 -d ' ')
DEB_UPSTREAM_VERSION = $(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^+]+).*,\1,p' | cut -f 1 -d '-')
DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -n -e 's/\.dfsg.*$$//p')

get-orig-source:
	set -ex ; \
        mkdir -p tmp ; \
        uscan --force-download --symlink --check-dirname-level 0 --destdir tmp ; \
        tar -C tmp -xzf tmp/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)-source.tgz ; \
	mv tmp/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)-source tmp/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION); \
        rm -rf tmp/$(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION)/contrib/Arpack ; \
        GZIP=-9 tar -C tmp -czf \
            $(CURDIR)/$(DEB_SOURCE_PACKAGE)_$(DEB_UPSTREAM_VERSION).orig.tar.gz \
            $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_VERSION) ; \
        rm -rf tmp
