#!/usr/bin/make -f
# -*- makefile -*-

#DH_VERBOSE=1

ifeq ($(dpkg-architecture -qDEB_BUILD_ARCH),ppc64el)
DEB_BUILD_MAINT_OPTIONS = nocheck
endif

%:
	dh $@ --with python3

override_dh_auto_install:
	python3 setup.py install --root=debian/xmds2 --install-layout=deb
	python3 debian/fix-cheetah-build-str.py

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	debian/tests/run-tests -b
endif

override_dh_auto_clean:
	cd xpdeint; waf/waf-light configure
	make clean
	dh_clean

JSDIR = /usr/share/javascript
INSTDIR = $(CURDIR)/debian/xmds2-doc
DOCDIR = $(INSTDIR)/usr/share/doc/xmds2
execute_after_dh_installdocs:
	# Fix Lintian error privacy-breach-may-use-debian-package
	sed -i 's|http://cdn.mathjax.org/mathjax/latest/|file:///usr/share/javascript/mathjax/|g;s|http://www.xmds.org/|file:///usr/share/doc/xmds2/|g' $(DOCDIR)/*.html $(DOCDIR)/_static/*.js
	# Symlink against the system-intalled JS libraries
	ln -fs $$(find $(JSDIR)/underscore -name underscore.js) $$(find $(INSTDIR) -name underscore.js)
	ln -fs $$(find -L $(JSDIR)/jquery -name jquery.js) $$(find $(INSTDIR) -name jquery.js)
	ln -fs $$(find $(JSDIR)/sphinxdoc -name doctools.js) $$(find $(INSTDIR) -name doctools.js)
	ln -fs $$(find $(JSDIR)/sphinxdoc -name searchtools.js) $$(find $(INSTDIR) -name searchtools.js)

CHEETAH_MAJOR_VERSION = $(shell dpkg -s python3-cheetah | grep ^Version: | cut -f2 -d\  | cut -f1 -d.)
override_dh_gencontrol:
	dh_gencontrol -- -VCheetah-Depends="python3-cheetah (>= $(CHEETAH_MAJOR_VERSION)), python3-cheetah (<< $$(($(CHEETAH_MAJOR_VERSION) + 1)))"

execute_before_dh_installdeb:
	# Remove pre-compiled Python source files, as per the Debian
	# Python Policy section 3.7 (Modules Byte-Compilation)
	@echo "*********************************************************"
	@echo "Warning: check whether the following fix is still needed:"
	@echo "*********************************************************"
	find debian/xmds2 -name __pycache__ -exec rm -r '{}' \;
	# Make wscript non executable
	@echo "*********************************************************"
	@echo "Warning: check whether the following fix is still needed:"
	@echo "*********************************************************"
	find debian/xmds2 -name wscript -exec chmod -x '{}' \;