#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# Overriding this with "contrib" attempts to link with parmetis (unimplemented).
DEBIAN_DIST=main

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# On Alpha, override this with debian/rules SLEPC_ARCH=linux_alpha_dec binary
# to build PETSc libs with the Compaq compilers, though that requires at least:
# libots, cpml-ev(5|6), cxml-ev(5|6), cfal and ccc.
# Note that only arch-dependent .debs are generated when you do this.
PETSC_ARCH=$(DEB_HOST_MULTIARCH)
SLEPC_ARCH=$(PETSC_ARCH)

# This defines the MPI implementation to use.  The easiest way to override this
# default is to specify while building, e.g. debian/rules SLEPC_MPI=lam binary.
# Again, only arch-dependent .debs are generated when you do this.
# Note that as of PETSc 2.3.0 this only specifies the name of the PETSc
# packages, the implementation is chosen by BuildSystem according to what's
# available at build time.
include /usr/share/mpi-default-dev/debian_defaults
SLEPC_MPI=$(ARCH_DEFAULT_MPI_IMPL)
SLEPC_MPI_DIR=/usr/lib/$(SLEPC_MPI)

# facilitate build-time testing (invoking OpenMPI) when building in a chroot (pbuilder or sbuild)
export OMPI_MCA_orte_rsh_agent=/bin/false

SLEPC_HDF5_FLAGS=$(shell if [ -e /usr/lib/libhdf5.so ]; then echo "--with-hdf5=1 --with-hdf5-dir=/usr"; fi)

SLEPC_BUILD_DIR=$(SLEPC_ARCH)-real
SLEPC_COMPLEX_BUILD_DIR=$(SLEPC_ARCH)-complex

# SLEPC_VERSION is used for the package names, library sonames, and
# /usr/lib/slepcdir subdirectory names.
SLEPC_VERSION=3.7.2$(SLEPC_EXT)
# use only major.minor as general soname (rather than major.minor.patch.build)
SLEPC_SONAME_VERSION=$(shell echo $(SLEPC_VERSION) | awk 'BEGIN { FS="."}; {print $$1"."$$2 }')

# SLEPC_NAME is the install dir under /usr/lib and the base for package names
SLEPC_SONAME=slepc$(SLEPC_SONAME_VERSION)
SLEPC_NAME=slepc$(SLEPC_VERSION)
SLEPC_COMPLEX_SONAME=slepc-complex-$(SLEPC_SONAME_VERSION)
SLEPC_COMPLEX_NAME=slepc-complex-$(SLEPC_VERSION)

# debian binary package names
SLEPC_DEV_PACKAGE=$(SLEPC_NAME)-dev
SLEPC_VERSIONED_DEV_PACKAGE=lib$(SLEPC_NAME)-dev
SLEPC_VERSIONED_PACKAGE=lib$(SLEPC_NAME)
SLEPC_DOC_PACKAGE=$(SLEPC_NAME)-doc
SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE=lib$(SLEPC_COMPLEX_NAME)-dev
SLEPC_COMPLEX_VERSIONED_PACKAGE=lib$(SLEPC_COMPLEX_NAME)

# install into /usr/lib/slepcdir rather than /usr/lib/slepc 
# to allow /usr/lib/slepc to be configured by alternatives
SLEPC_DIR_PREFIX=/usr/lib/slepcdir/$(SLEPC_VERSION)/$(SLEPC_ARCH)-real
SLEPC_COMPLEX_DIR_PREFIX=/usr/lib/slepcdir/$(SLEPC_VERSION)/$(SLEPC_ARCH)-complex

PACKAGE_INSTALL_BASEDIR=debian/tmp/$(SLEPC_NAME)-real
PACKAGE_INSTALL_DIR=$(PACKAGE_INSTALL_BASEDIR)/$(SLEPC_DIR_PREFIX)
PACKAGE_COMPLEX_INSTALL_BASEDIR=debian/tmp/$(SLEPC_NAME)-complex
PACKAGE_COMPLEX_INSTALL_DIR=$(PACKAGE_COMPLEX_INSTALL_BASEDIR)/$(SLEPC_COMPLEX_DIR_PREFIX)

PETSC_SONAME_VERSION=$(SLEPC_SONAME_VERSION)
PETSC_DIR=/usr/lib/petscdir/$(PETSC_SONAME_VERSION)
PETSC_DIR_REAL=$(PETSC_DIR)-real
PETSC_DIR_COMPLEX=$(PETSC_DIR)-complex

export SLEPC_DIR=$(CURDIR)

# main packaging script based on dh7 syntax
%:
	dh $@ --with python2


override_dh_auto_clean:
	if [ -d $(SLEPC_BUILD_DIR) ]; then \
	dh_auto_clean -p$(SLEPC_VERSIONED_DEV_PACKAGE) -p$(SLEPC_DOC_PACKAGE) -- \
	  SLEPC_DIR=$(CURDIR) SLEPC_ARCH=$(SLEPC_BUILD_DIR); \
	fi
	if [ -d $(SLEPC_COMPLEX_BUILD_DIR) ]; then \
	dh_auto_clean -p$(SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE) -- \
	  SLEPC_DIR=$(CURDIR) SLEPC_ARCH=$(SLEPC_COMPLEX_BUILD_DIR); \
	fi


#SLEPc cannot differentiate different configrations at build time 
# i.e. there is no SLEPC_ARCH, unlike PETSC_ARCH.
# So have to configure and build in one step
# Note SLEPc's ./configure refuses to allow additional (unknown) options
# so can't use dh_auto_configure
override_dh_auto_configure:

override_dh_auto_build:


override_dh_auto_test:
#	dh_auto_test -p$(SLEPC_DEBUG_PACKAGE) -- \
#	  SLEPC_DIR=$(CURDIR) PETSC_ARCH=$(SLEPC_DEBUG_BUILD_DIR) \
#	  LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/$(SLEPC_DEBUG_BUILD_DIR)/lib
#	dh_auto_test -p$(SLEPC_VERSIONED_DEV_PACKAGE) -- \
#	  SLEPC_DIR=$(CURDIR) PETSC_ARCH=$(SLEPC_BUILD_DIR) \
#	  LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/$(SLEPC_BUILD_DIR)/lib
#	dh_auto_test -p$(SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE) -- \
#	  SLEPC_DIR=$(CURDIR) PETSC_ARCH=$(SLEPC_COMPLEX_BUILD_DIR) \
#	  LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/$(SLEPC_COMPLEX_BUILD_DIR)/lib
#	dh_auto_test -p$(SLEPC_COMPLEX_DEBUG_PACKAGE) -- \
#	  SLEPC_DIR=$(CURDIR) PETSC_ARCH=$(SLEPC_COMPLEX_DEBUG_BUILD_DIR) \
#	  LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/$(SLEPC_COMPLEX_DEBUG_BUILD_DIR)/lib


override_dh_auto_install:
	PETSC_DIR=$(PETSC_DIR_REAL) \
          ./configure --prefix=$(SLEPC_DIR_PREFIX)  \
            --with-arpack=1 \
            --shared-library-extension=_real
	dh_auto_build -p$(SLEPC_VERSIONED_DEV_PACKAGE) -p$(SLEPC_DOC_PACKAGE) -- \
            PETSC_DIR=$(PETSC_DIR_REAL)
	dh_auto_install -p$(SLEPC_VERSIONED_DEV_PACKAGE) -p$(SLEPC_DOC_PACKAGE) \
	  --destdir=$(PACKAGE_INSTALL_DIR) --  \
          SLEPC_INSTALLDIR=$(SLEPC_DIR_PREFIX) \
          SLEPC_DESTDIR=$(PACKAGE_INSTALL_DIR) \
          PETSC_DIR=$(PETSC_DIR_REAL)
	echo PACKAGE_INSTALL_DIR=$(PACKAGE_INSTALL_DIR)
	echo SLEPC_DIR_PREFIX=$(SLEPC_DIR_PREFIX)
#	make SLEPC_DIR=$(CURDIR)/$(PACKAGE_INSTALL_DIR) PETSC_DIR=$(PETSC_DIR_REAL) PETSC_ARCH="" LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/$(PACKAGE_INSTALL_DIR)/lib test
	
	PETSC_DIR=$(PETSC_DIR_COMPLEX) \
          ./configure --prefix=$(SLEPC_COMPLEX_DIR_PREFIX)  \
          --with-arpack=1 \
          --shared-library-extension=_complex
	dh_auto_build -p$(SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE)  --  \
          PETSC_DIR=$(PETSC_DIR_COMPLEX)
	dh_auto_install -p$(SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE) --destdir=$(PACKAGE_COMPLEX_INSTALL_DIR) --  \
          SLEPC_INSTALLDIR=$(SLEPC_COMPLEX_DIR_PREFIX) \
          SLEPC_DESTDIR=$(PACKAGE_COMPLEX_INSTALL_DIR) \
          PETSC_DIR=$(PETSC_DIR_COMPLEX)
	echo Note: standard SLEPc built-time tests only cover real number configurations, not complex numbers.


override_dh_install:
	dh_install -p$(SLEPC_VERSIONED_PACKAGE) --sourcedir $(PACKAGE_INSTALL_BASEDIR) --exclude=*html  $(SLEPC_DIR_PREFIX)/lib/libslepc_real.so.$(SLEPC_VERSION)  usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(SLEPC_VERSIONED_DEV_PACKAGE) --sourcedir $(PACKAGE_INSTALL_BASEDIR) --autodest --exclude=*html --exclude=libslepc_real.so.$(SLEPC_VERSION)  usr
	dh_link -p$(SLEPC_VERSIONED_DEV_PACKAGE)  usr/lib/$(DEB_HOST_MULTIARCH)/libslepc_real.so.$(SLEPC_VERSION)  $(SLEPC_DIR_PREFIX)/lib/libslepc_real.so.$(SLEPC_VERSION)
	
	dh_install -p$(SLEPC_COMPLEX_VERSIONED_PACKAGE) --sourcedir $(PACKAGE_COMPLEX_INSTALL_BASEDIR) --exclude=*html  $(SLEPC_COMPLEX_DIR_PREFIX)/lib/libslepc_complex.so.$(SLEPC_VERSION)  usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE) --sourcedir $(PACKAGE_COMPLEX_INSTALL_BASEDIR) --autodest --exclude=*html --exclude=libslepc_complex.so.$(SLEPC_VERSION)  usr
	dh_link -p$(SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE)  usr/lib/$(DEB_HOST_MULTIARCH)/libslepc_complex.so.$(SLEPC_VERSION)  $(SLEPC_COMPLEX_DIR_PREFIX)/lib/libslepc_complex.so.$(SLEPC_VERSION)

override_dh_installdocs:
	dh_installdocs --exclude=makefile
	# html docs for header files in include have been installed,
	# but dh_installdocs cannot exclude the .h (-X.h bans the .html files)
	# so remove the excess .h files
	find debian/$(SLEPC_DOC_PACKAGE)/usr/share/doc/$(SLEPC_DOC_PACKAGE)/include -name *.h -o -name *.h90 | xargs -r rm
	for examples in $$( find src -name *example* ); do \
	  dh_install -p$(SLEPC_DOC_PACKAGE) $$examples/* usr/share/doc/$(SLEPC_DOC_PACKAGE)/examples/$$(echo $$examples | sed "s/src\///; s/examples//" ); \
	done

# dh_installchangelogs can't handle a directory of changelog files, so skip them here
override_dh_installchangelogs:
	dh_installchangelogs --exclude=docs/changes

override_dh_python2:
	dh_python2
	dh_python2 -p$(SLEPC_VERSIONED_DEV_PACKAGE) $(SLEPC_DIR_PREFIX)/bin
	dh_python2 -p$(SLEPC_COMPLEX_VERSIONED_DEV_PACKAGE) $(SLEPC_COMPLEX_DIR_PREFIX)/bin

override_dh_installdeb:
	dh_installdeb
	for pkg in $$(dh_listpackages -a); do \
	  sed -i -e 's/__SLEPC_SONAME_VERSION__/$(SLEPC_SONAME_VERSION)/; s/__SLEPC_VERSION__/$(SLEPC_VERSION)/; s/__DEB_HOST_MULTIARCH__/$(DEB_HOST_MULTIARCH)/' debian/$$pkg/DEBIAN/*; \
	done


override_dh_strip:
	dh_strip -p$(SLEPC_VERSIONED_PACKAGE)
	dh_strip -p$(SLEPC_COMPLEX_VERSIONED_PACKAGE)


DEB_SOURCE_PACKAGE = slepc
DEB_UPSTREAM_VERSION = $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')
DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -n -e 's/\.dfsg.*$$//p')
USCAN_DESTDIR := $(CURDIR)

get-orig-source:
	uscan --no-conf --verbose --download-current-version --repack
