#!/usr/bin/make -f

export DH_VERBOSE = 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all future=+lfs

# The following workaround is necessary for hardening, since
# the project's SConstruct file doesn't make use of CPPFLAGS.
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
export DEB_CFLAGS_MAINT_APPEND = $(CPPFLAGS)

# Ensure that we use the python3 version of sphinx.
export PATH := /usr/share/sphinx/scripts/python3:$(PATH)

# Prevent sphinx from trying to automatically import themes.
export http_proxy := http://127.0.0.1:9/

DEB_BUILD_ROOT := $(CURDIR)/debian/tmp
DBO := $(DEB_BUILD_OPTIONS) $(DEB_BUILD_MAINT_OPTIONS)
NUM_JOBS := $(lastword $(patsubst parallel=%,%,$(filter parallel=%,$(DBO))))
COMMON_SCONS_OPTS := \
	--prefix=$(DEB_BUILD_ROOT)/usr --actual-prefix=/usr --without-gui \
	$(if $(NUM_JOBS),-j$(NUM_JOBS)) \
	$(if $(filter $(DH_VERBOSE),1),VERBOSE=1) \
	$(if $(filter debug,$(DBO)),DEBUG=1) \
	SYMBOLS=1

# Fudge build date using SOURCE_DATE_EPOCH for build reproducibility.
include /usr/share/dpkg/pkg-info.mk
DATE := @$(SOURCE_DATE_EPOCH)
export BUILD_DATE := $(shell date -u -d '$(DATE)' +'%b %d %Y')
export BUILD_TIME := $(shell date -u -d '$(DATE)' +%T)

# Sensible defaults for test suite.
export RM_TS_PRINT_CMD = 0
export RM_TS_PEDANTIC = 0

# Set locale so that python3 nose tests run with UTF-8 support
# in all build environments.
export LC_ALL = C.UTF-8

%:
	dh $@ --with python3,sphinxdoc

override_dh_auto_clean:
	scons -c $(COMMON_SCONS_OPTS) install

override_dh_auto_configure-arch:
	scons $(COMMON_SCONS_OPTS) config

override_dh_auto_build-arch:
	scons $(COMMON_SCONS_OPTS)

override_dh_auto_install-arch:
	scons $(COMMON_SCONS_OPTS) install

override_dh_auto_configure-indep:
	@echo "*** No-op. ***"

override_dh_auto_build-indep:
	scons $(COMMON_SCONS_OPTS) docs
	bash -O extglob \
		-c "rm -rf docs/_build/html/_static/{bootstrap,bootswatch}-+([[:digit:]]).+([[:digit:]]).+([[:digit:]]) \
		docs/_build/html/_static/js/jquery-+([[:digit:]]).+([[:digit:]]).+([[:digit:]]).min.js"
	sed -i \
		-e 's@http[s]\?://.\+/mathjax/[^/]\+/\([Mm]ath[Jj]ax\|latest\)\.js@file:///usr/share/javascript/mathjax/MathJax.js@g' \
		-e 's@\(_static/\(bootstrap\|bootswatch\)\)-[[:digit:]]\+\(\.[[:digit:]]\+\)\{2\}@\1@g' \
		-e 's@\(_static/js/jquery\)-[[:digit:]]\+\(\.[[:digit:]]\+\)\{2\}\(\.min\.js\)@\1\3@g' \
		docs/_build/html/*.html

override_dh_auto_install-indep:
	cd gui && \
	python3 setup.py install --prefix=$(DEB_BUILD_ROOT)/usr --install-layout=deb

override_dh_install:
	dh_install -prmlint-gui -X.compiled -X.gz -X.mo
	dh_install --remaining-packages

override_dh_installdocs:
	dh_installdocs -prmlint-doc -X.inv
	dh_installdocs --remaining-packages

override_dh_sphinxdoc:
	dh_sphinxdoc -prmlint-doc -Xbootstrap -Xbootswatch -Xjquery.min.js
	dh_sphinxdoc --remaining-packages

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DBO)))
	# Ignore result of the test suite for now
	-scons $(COMMON_SCONS_OPTS) test
else
	@echo "*** Tests disabled via DEB_BUILD(_MAINT)?_OPTIONS. ***"
endif
