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

export PYBUILD_NAME = slepc4py

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

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

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 -e 's/[\.\+]dfsg.*$$//p' )

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

SLEPC_MAJOR_VERSION=$(shell echo $(SLEPC_BASE_VERSION) | sed "s/\..*//")
SLEPC_MINOR_VERSION=$(shell echo $(SLEPC_BASE_VERSION) | sed "s/.*\.//")
SLEPC_NEXT_VERSION=$(shell echo $(SLEPC_MAJOR_VERSION).$$(( $(SLEPC_MINOR_VERSION) + 1 )) )

export PETSC_DIR=/usr/lib/petscdir/petsc$(SLEPC_BASE_VERSION)/$(DEB_HOST_MULTIARCH)-real
export SLEPC_DIR=/usr/lib/slepcdir/slepc$(SLEPC_BASE_VERSION)/$(DEB_HOST_MULTIARCH)-real

# 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


# main packaging script based on dh7 syntax
%:
	dh $@ --with python2,python3 --buildsystem=pybuild

override_dh_clean:
	dh_clean
	rm -rf .eggs

override_dh_auto_build:
	SLEPC_DIR=$(SLEPC_DIR)  PETSC_DIR=$(PETSC_DIR)  dh_auto_build
	# slepc4py embeds the deep PETSC_DIR path as RPATH in its library.
	# Swap it out for a standard path, or else dh_shlibdeps gets confused
	chrpath -d .pybuild/*python*/build/slepc4py/lib/SLEPc*.so

override_dh_auto_install:
	SLEPC_DIR=$(SLEPC_DIR)  PETSC_DIR=$(PETSC_DIR)  dh_auto_install

override_dh_installdocs-indep:
	dh_installdocs
	rm debian/python-slepc4py-docs/usr/share/doc/python-slepc4py-docs/usrman/_static/jquery*.js
	ln -sf /usr/share/javascript/jquery/jquery.js debian/python-slepc4py-docs/usr/share/doc/python-slepc4py-docs/usrman/_static/jquery.js
	rm debian/python-slepc4py-docs/usr/share/doc/python-slepc4py-docs/usrman/_static/underscore*.js
	ln -sf /usr/share/javascript/underscore/underscore.js debian/python-slepc4py-docs/usr/share/doc/python-slepc4py-docs/usrman/_static/underscore.js

override_dh_python2:
	dh_python2
	dh_numpy

override_dh_compress:
	dh_compress -Xpdf

override_dh_gencontrol:
	dh_gencontrol -- -Vpetsc4py:Depends="python-petsc4py (>= $(SLEPC_BASE_VERSION)), python-petsc4py (<< $(SLEPC_NEXT_VERSION))" \
                           -Vpetsc4py:Depends3="python3-petsc4py (>= $(SLEPC_BASE_VERSION)), python3-petsc4py (<< $(SLEPC_NEXT_VERSION))"
