#!/usr/bin/make -f

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

FC = $(shell basename $(shell readlink -f /usr/bin/gfortran))
FFLAGS += -fPIC
FFLAGS += -fopenmp
OLD_LAPACK=$(shell [ -f /usr/lib/liblapack.a ] && echo 1 || echo 0)
ifeq ($(OLD_LAPACK), 1)
	LAPACK_PATH=liblapack.a
else
	LAPACK_PATH=$(DEB_HOST_MULTIARCH)/liblapack.a
endif

%:
	dh $@ --with autoreconf --with fortran_mod

override_dh_autoreconf:
	dh_autoreconf --as-needed

override_dh_auto_configure:
	mkdir -p $(CURDIR)/build/spml
	(cd $(CURDIR)/build/spml \
	  && FC=$(FC) FCFLAGS="$(FFLAGS)" LDFLAGS="$(LDFLAGS)" \
	  ../../configure \
	  --prefix=/usr/lib/$(DEB_HOST_MULTIARCH)/spml \
	  --with-ispack=/usr/lib/$(DEB_HOST_MULTIARCH)/libispack.a \
	  --with-netcdf=/usr/lib/$(DEB_HOST_MULTIARCH)/libnetcdf.so \
	  --with-netcdff=/usr/lib/$(DEB_HOST_MULTIARCH)/libnetcdff.so \
	  --with-gtool5=/usr/lib/$(DEB_HOST_MULTIARCH)/gtool5/lib/libgtool5.a \
	  --with-lapack=/usr/lib/$(LAPACK_PATH) \
	  --with-lumatrix=LAPACK )
	mkdir -p $(CURDIR)/build/spml-mpi
	(cd $(CURDIR)/build/spml-mpi \
	  && FC=mpif90 FCFLAGS="$(FFLAGS)" LDFLAGS="$(LDFLAGS)" \
	  ../../configure \
	  --prefix=/usr/lib/$(DEB_HOST_MULTIARCH)/spml-mpi \
	  --with-ispack=/usr/lib/$(DEB_HOST_MULTIARCH)/libispack-mpi.a \
	  --with-netcdf=/usr/lib/$(DEB_HOST_MULTIARCH)/libnetcdf.so \
	  --with-netcdff=/usr/lib/$(DEB_HOST_MULTIARCH)/libnetcdff.so \
	  --with-gtool5=/usr/lib/$(DEB_HOST_MULTIARCH)/gtool5-mpi/lib/libgtool5.a \
	  --with-lapack=/usr/lib/$(LAPACK_PATH) \
	  --with-lumatrix=LAPACK \
	  --with-mpifc=mpif90 \
	  --with-mpiexec=mpiexec )

override_dh_auto_build:
	( cd $(CURDIR)/build/spml && $(MAKE) )
	( cd $(CURDIR)/build/spml-mpi && $(MAKE) )
	grep "^#" $(CURDIR)/build/spml/bin/spmfrt \
	  | sed 's/^#//g' > $(CURDIR)/debian/spmfrt.rd
	grep "^#" $(CURDIR)/build/spml/bin/spmconfig \
	  | sed 's/^#//g' > $(CURDIR)/debian/spmconfig.rd
	grep "^#" $(CURDIR)/build/spml-mpi/bin/mpispmfrt \
	  | sed 's/^#//g' > $(CURDIR)/debian/mpispmfrt.rd
	grep "^#" $(CURDIR)/build/spml-mpi/bin/spmconfig \
	  | sed 's/^#//g' > $(CURDIR)/debian/mpispmconfig.rd
	( cd $(CURDIR)/debian \
	  && rd2 -r rd/rd2man-lib spmfrt.rd > spmfrt.1 )
	( cd $(CURDIR)/debian \
	  && rd2 -r rd/rd2man-lib spmconfig.rd > spmconfig.1 )
	(cd $(CURDIR)/debian \
	  && rd2 -r rd/rd2man-lib mpispmfrt.rd > mpispmfrt.1 )
	(cd $(CURDIR)/debian \
	  && rd2 -r rd/rd2man-lib mpispmconfig.rd > mpispmconfig.1 )

override_dh_auto_test:
# 	( cd $(CURDIR)/build/spml && $(MAKE) test )

override_dh_auto_install:
	(cd $(CURDIR)/build/spml \
	  && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp )
	(cd $(CURDIR)/build/spml-mpi \
	  && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp )

override_dh_install:
	dh_install --sourcedir=debian/tmp
	find $(CURDIR)/debian/spml -name \*.la | xargs rm -f
	sed -i -e 's%-fdebug-prefix-map=$(CURDIR)=.%%g ' \
	  $(CURDIR)/debian/spml/usr/lib/$(DEB_HOST_MULTIARCH)/spml/bin/spmconfig
	sed -i -e 's%-fdebug-prefix-map=$(CURDIR)=.%%g ' \
	  $(CURDIR)/debian/spml/usr/lib/$(DEB_HOST_MULTIARCH)/spml-mpi/bin/spmconfig

override_dh_auto_clean:
	dh_auto_clean
	rm -fr $(CURDIR)/build
	rm -f $(CURDIR)/libsrc/eigmatrix/eigmatrix.s
	rm -f $(CURDIR)/debian/*.rd
	rm -f $(CURDIR)/debian/*.1

override_dh_link:
	dh_link \
	  usr/lib/$(DEB_HOST_MULTIARCH)/spml/bin/spmfrt usr/bin/spmfrt \
	  usr/lib/$(DEB_HOST_MULTIARCH)/spml/bin/spmconfig usr/bin/spmconfig \
	  usr/lib/$(DEB_HOST_MULTIARCH)/spml-mpi/bin/mpispmfrt usr/bin/mpispmfrt \
	  usr/lib/$(DEB_HOST_MULTIARCH)/spml-mpi/bin/spmconfig usr/bin/mpispmconfig

override_dh_installchangelogs:
	dh_installchangelogs --all $(CURDIR)/ChangeLog

override_dh_installdocs:
	dh_installdocs
	find $(CURDIR)/debian/spml-doc -name INSTALL\* | xargs rm -fr
	find $(CURDIR)/debian/spml-doc -name Makefile\* | xargs rm -fr
	find $(CURDIR)/debian/spml-doc -name README\* | xargs rm -fr

override_dh_compress:
	dh_compress -X.tex -X.pdf
