#!/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_SOURCE_PACKAGE := $(strip $(shell egrep '^Source: ' debian/control | cut -f 2 -d ':'))
DEB_VERSION := $(shell dpkg-parsechangelog -S Version)
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' )

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

SLEPC_REAL_ARCH=$(DEB_HOST_MULTIARCH)-real
SLEPC_COMPLEX_ARCH=$(DEB_HOST_MULTIARCH)-complex

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

# need correct build dir to perform tests, since SLEPc uses
# PETSC_ARCH rather SLEPC_ARCH in lib/slepc/conf/slepc_variables
# and at the same time the build dir is set in configure.py
# using python2's sys.platform to a specific string.
BUILDDIR_ARCH := $(shell python2 -c "import sys; print(sys.platform.replace('cygwin','mswin'))" )
SLEPC_REAL_BUILD_DIR=installed-arch-$(BUILDDIR_ARCH)-c-opt
SLEPC_COMPLEX_BUILD_DIR=installed-arch-$(BUILDDIR_ARCH)-c-opt-complex

# SLEPC_VERSION is used for the package names, library sonames, and
# /usr/lib/slepcdir subdirectory names.
export SLEPC_VERSION=$(DEB_STRIPPED_UPSTREAM_VERSION)$(SLEPC_EXT)
# use only major.minor as general soname (rather than major.minor.patch.build)
export 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_NAME=slepc$(SLEPC_SONAME_VERSION)
SLEPC_REAL_NAME=slepc-real$(SLEPC_SONAME_VERSION)
SLEPC_COMPLEX_NAME=slepc-complex$(SLEPC_SONAME_VERSION)

# debian binary package names
SLEPC_REAL_DEV_PACKAGE=lib$(SLEPC_REAL_NAME)-dev
SLEPC_REAL_PACKAGE=lib$(SLEPC_REAL_NAME)
SLEPC_COMPLEX_DEV_PACKAGE=lib$(SLEPC_COMPLEX_NAME)-dev
SLEPC_COMPLEX_PACKAGE=lib$(SLEPC_COMPLEX_NAME)
export SLEPC_DOC_PACKAGE=$(SLEPC_NAME)-doc
export SLEPC_DEV_EXAMPLE_PACKAGE=lib$(SLEPC_NAME)-dev-examples

# install into /usr/lib/slepcdir rather than /usr/lib/slepc 
# to allow /usr/lib/slepc to be configured by alternatives
export SLEPC_REAL_DIR_PREFIX=/usr/lib/slepcdir/$(SLEPC_NAME)/$(SLEPC_REAL_ARCH)
export SLEPC_COMPLEX_DIR_PREFIX=/usr/lib/slepcdir/$(SLEPC_NAME)/$(SLEPC_COMPLEX_ARCH)

export PACKAGE_REAL_INSTALL_DIR=debian/tmp/$(SLEPC_NAME)-real
PACKAGE_COMPLEX_INSTALL_DIR=debian/tmp/$(SLEPC_NAME)-complex

PETSC_SONAME_VERSION=$(SLEPC_SONAME_VERSION)
PETSC_DIR_BASE=/usr/lib/petscdir/petsc$(PETSC_SONAME_VERSION)
PETSC_DIR_REAL=$(PETSC_DIR_BASE)/$(SLEPC_REAL_ARCH)
PETSC_DIR_COMPLEX=$(PETSC_DIR_BASE)/$(SLEPC_COMPLEX_ARCH)

export SLEPC_DIR=$(CURDIR)

MPIEXEC_ALLOW_ROOT := mpiexec --oversubscribe --allow-run-as-root


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


override_dh_auto_clean:
	if [ -d $(SLEPC_REAL_BUILD_DIR) ]; then \
	dh_auto_clean -p$(SLEPC_REAL_DEV_PACKAGE) -p$(SLEPC_DOC_PACKAGE) -- \
	  SLEPC_DIR=$(CURDIR) PETSC_DIR=$(PETSC_DIR_REAL); \
	fi
	if [ -d $(SLEPC_COMPLEX_BUILD_DIR) ]; then \
	dh_auto_clean -p$(SLEPC_COMPLEX_DEV_PACKAGE) -- \
	  SLEPC_DIR=$(CURDIR) PETSC_DIR=$(PETSC_DIR_COMPLEX); \
	fi

override_dh_clean: override_dh_auto_clean
	dh_clean
	rm -rf installed-*
	rm -f lib/slepc/conf/slepcvariables
	find config -name *.pyc | xargs rm -f

# SLEPc differentiates different configurations (real, complex, etc)
# at build time from values in petscconf.h given by PETSC_DIR
override_dh_auto_configure:
	if PETSC_DIR=$(PETSC_DIR_REAL) \
          ./configure --prefix=$(SLEPC_REAL_DIR_PREFIX)  \
            --with-arpack=1 \
            --shared-library-extension=_real ; then \
	  : ; \
	else \
	  err=$$?; \
	  echo "real configure failed with exit value $$err"; \
	  echo "===== show real configure.log ====="; \
	  cat $(SLEPC_REAL_BUILD_DIR)/lib/slepc/conf/configure.log; \
	  echo "===== end real configure.log ====="; \
	  (exit $$err); \
	fi
	if PETSC_DIR=$(PETSC_DIR_COMPLEX) \
          ./configure --prefix=$(SLEPC_COMPLEX_DIR_PREFIX)  \
          --with-arpack=1 \
          --shared-library-extension=_complex ; then \
	  : ; \
	else \
	  err=$$?; \
	  echo "complex configure failed with exit value $$err"; \
	  echo "===== show complex configure.log ====="; \
	  cat $(SLEPC_COMPLEX_BUILD_DIR)/lib/slepc/conf/configure.log; \
	  echo "===== end complex configure.log ====="; \
	  (exit $$err); \
	fi

override_dh_auto_build:
	dh_auto_build -p$(SLEPC_REAL_DEV_PACKAGE) -p$(SLEPC_DOC_PACKAGE) -- V=1 \
	  SLEPC_DIR=$(CURDIR) \
	  PETSC_DIR=$(PETSC_DIR_REAL) PETSC_ARCH=$(SLEPC_REAL_BUILD_DIR)
	dh_auto_build -p$(SLEPC_COMPLEX_DEV_PACKAGE)  -- V=1 \
	  SLEPC_DIR=$(CURDIR) \
	  PETSC_DIR=$(PETSC_DIR_COMPLEX) PETSC_ARCH=$(SLEPC_COMPLEX_BUILD_DIR)


# ia64 consistently times out during tests, so skip
ARCH_NO_TEST_LIST = ia64

empty :=
space := $(empty)$(empty)

# run tests, or not
RUNTEST=yes
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_NO_TEST_LIST)$(space)))
  RUNTEST=no
endif

override_dh_auto_test:
	set -e; \
	if [ "$(RUNTEST)" = "no" ]; then \
	   echo Tests have been disabled on $(DEB_HOST_ARCH); \
	else \
	   dh_auto_test -p$(SLEPC_REAL_DEV_PACKAGE) -- \
		SLEPC_DIR=$(CURDIR) \
	  	PETSC_DIR=$(PETSC_DIR_REAL) PETSC_ARCH=$(SLEPC_REAL_BUILD_DIR) \
	  	LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/$(SLEPC_REAL_BUILD_DIR)/lib \
	  	MPIEXEC="$(MPIEXEC_ALLOW_ROOT)"; \
	   dh_auto_test -p$(SLEPC_COMPLEX_DEV_PACKAGE) -- \
		SLEPC_DIR=$(CURDIR) \
		PETSC_DIR=$(PETSC_DIR_COMPLEX) PETSC_ARCH=$(SLEPC_COMPLEX_BUILD_DIR) \
	  	LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/$(SLEPC_COMPLEX_BUILD_DIR)/lib \
	  	MPIEXEC="$(MPIEXEC_ALLOW_ROOT)" ; \
	fi

override_dh_auto_install:
	dh_auto_install -p$(SLEPC_REAL_DEV_PACKAGE) -p$(SLEPC_DOC_PACKAGE) \
	  --destdir=$(PACKAGE_REAL_INSTALL_DIR) --  \
	  SLEPC_DIR=$(CURDIR) \
	  PETSC_DIR=$(PETSC_DIR_REAL) PETSC_ARCH=$(SLEPC_REAL_BUILD_DIR)
	dh_auto_install -p$(SLEPC_COMPLEX_DEV_PACKAGE) --destdir=$(PACKAGE_COMPLEX_INSTALL_DIR) --  \
	  SLEPC_DIR=$(CURDIR) \
	  PETSC_DIR=$(PETSC_DIR_COMPLEX) PETSC_ARCH=$(SLEPC_COMPLEX_BUILD_DIR)

override_dh_install:
	dh_install -p$(SLEPC_REAL_PACKAGE) --sourcedir $(PACKAGE_REAL_INSTALL_DIR) --exclude=*html  $(SLEPC_REAL_DIR_PREFIX)/lib/libslepc_real.so.$(SLEPC_VERSION)  usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(SLEPC_REAL_DEV_PACKAGE) --sourcedir $(PACKAGE_REAL_INSTALL_DIR) --autodest --exclude=*html --exclude=libslepc_real.so.$(SLEPC_VERSION) --exclude=conf/uninstall.py --exclude=share/slepc/examples --exclude=share/slepc/datafiles  usr
	
	dh_install -p$(SLEPC_COMPLEX_PACKAGE) --sourcedir $(PACKAGE_COMPLEX_INSTALL_DIR) --exclude=*html  $(SLEPC_COMPLEX_DIR_PREFIX)/lib/libslepc_complex.so.$(SLEPC_VERSION)  usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(SLEPC_COMPLEX_DEV_PACKAGE) --sourcedir $(PACKAGE_COMPLEX_INSTALL_DIR) --autodest --exclude=*html --exclude=libslepc_complex.so.$(SLEPC_VERSION) --exclude=conf/uninstall.py --exclude=share/slepc/examples --exclude=share/slepc/datafiles  usr
	
	dh_install -p$(SLEPC_DEV_EXAMPLE_PACKAGE)

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
        # test if header docs were installed (in a binary-only build they won't be)
	if [ -d ./debian/$(SLEPC_DOC_PACKAGE)/usr/share/doc/$(SLEPC_DOC_PACKAGE)/include ]; then  \
	  find debian/$(SLEPC_DOC_PACKAGE)/usr/share/doc/$(SLEPC_DOC_PACKAGE)/include -name *.h -o -name *.h90 | xargs -r rm; \
	# make header docs "generic" (pointing to headers in preferred alternative /usr/include/slepc)  \
	  for hdoc in `find ./debian/$(SLEPC_DOC_PACKAGE)/usr/share/doc/$(SLEPC_DOC_PACKAGE)/include/ -name "*.html"`; do  \
	    h=`echo $${hdoc} | xargs basename | sed "s/.html//"`; \
	    hdir=`echo $${hdoc} | xargs dirname | sed "s|^.*/include||"`; \
	    sed "s|href=\"$${h}\"|href=\"/usr/include/slepc$${hdir}/$${h}\"|" -i $${hdoc};  \
	  done  \
	fi
	for exampledoc in `find src -name *.html`; do \
	  dh_install -p$(SLEPC_DEV_EXAMPLE_PACKAGE) $$exampledoc usr/share/doc/$(SLEPC_DEV_EXAMPLE_PACKAGE)/examples/`dirname $$exampledoc`; \
	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_REAL_DEV_PACKAGE) $(SLEPC_REAL_DIR_PREFIX)/bin
	dh_python2 -p$(SLEPC_COMPLEX_DEV_PACKAGE) $(SLEPC_COMPLEX_DIR_PREFIX)/bin
	dh_python2 -p$(SLEPC_DEV_EXAMPLE_PACKAGE) /usr/share/doc/$(SLEPC_DEV_EXAMPLE_PACKAGE)

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_REAL_PACKAGE)
	dh_strip -p$(SLEPC_COMPLEX_PACKAGE)

override_dh_fixperms-indep:
	dh_fixperms
	chmod a-x debian/$(SLEPC_DEV_EXAMPLE_PACKAGE)/usr/share/doc/$(SLEPC_DEV_EXAMPLE_PACKAGE)/examples/config/*

override_dh_gencontrol:
	# SLEPc.pc depends on specific patch version of PETSc (as defined in PETSc.pc)
	samplemod=debian/$(SLEPC_REAL_DEV_PACKAGE)/$(SLEPC_REAL_DIR_PREFIX)/include/slepcsys.mod; \
	samplemodheader=$$(zcat $${samplemod} | head -n 1); \
	fcompiler=$$(echo $${samplemodheader} | awk '{print $$1}' | tr '[:upper:]' '[:lower:]'); \
	modversion=$$(echo $${samplemodheader} | awk '{print $$4}' | sed "s/'//g"); \
	dh_gencontrol -- -Vpetsc:dev="libpetsc-real$(PETSC_SONAME_VERSION)-dev" -Vpetsc:complex-dev="libpetsc-complex$(PETSC_SONAME_VERSION)-dev" -Vfortran-mod-version=$${fcompiler}-mod-$${modversion}

USCAN_DESTDIR := $(CURDIR)

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