#!/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 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 | egrep '^Version:' | cut -f 2 -d ' ')
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')

VERSION := $(DEB_STRIPPED_UPSTREAM_VERSION)
MAJOR_VERSION := $(shell echo $(DEB_STRIPPED_UPSTREAM_VERSION) | sed "s/^\([^.]*\)\..*$$/\1/")
SOVERSION := $(MAJOR_VERSION)

PY3VER_DEFAULT := $(shell py3versions -dv)

export PYBUILD_NAME=basix

export PYBUILD_TEST_ARGS=-k "not test_include"

%:
	dh $@ --with python3 --buildsystem=pybuild


override_dh_auto_clean:
	dh_auto_clean
	rm -rf doc/html doc/latex
	rm -rf cpp/fenics_basix.egg-info

override_dh_auto_build-indep: export http_proxy=127.0.0.1:9
override_dh_auto_build-indep: export https_proxy=127.0.0.1:9
override_dh_auto_build-indep:
	dh_auto_build -i
	cd doc; doxygen

override_dh_auto_install:
	dh_auto_install
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	cp -a _skbuild/*${PY3VER_DEFAULT}/cmake-install/cpp/basix/lib/* debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	mv debian/python3-basix/usr/lib/python${PY3VER_DEFAULT}/dist-packages/basix/include debian/tmp/usr
	rm -rf debian/python3-basix/usr/lib/python*/dist-packages/basix/lib
	chrpath -d debian/python3-basix/usr/lib/python*/dist-packages/basix/_basixcpp.*.so

override_dh_installdocs-indep:
	dh_installdocs -i
	grep "http://cdn.mathjax.org/mathjax/latest/MathJax.js" debian/basix-doc/usr/share/doc/basix-doc/* -r --files-with-matches | xargs sed "s|src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js|src=\"file://usr/share/javascript/mathjax/MathJax.js|g" -i
