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

#DH_VERBOSE=1

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

%:
	dh $@ --with python3

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
execute_after_dh_auto_build:
	make -C userdoc/src html latex
	make -C documentation/latex all-pdf

DOCDIR = $(CURDIR)/debian/xmds2-doc/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
	dh_sphinxdoc
endif

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_compress:
	dh_compress -X.ico

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:
	# Make wscript non executable
	@wscript_path=$$(find debian/xmds2 -name wscript) ;	\
	if [ "$$(stat -c %a $$wscript_path)" = 644 ] ; then	\
	    echo "************************************" ;	\
	    echo "chmod wscript hack is no more needed" ;	\
	    echo "************************************" ;	\
	else 							\
	    chmod -x $$wscript_path ;				\
	fi

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

get-userdoc-tarball:
	( CURDIR=$$(pwd) ; 						\
	  TMPDIR=$$(mktemp -d) ;					\
	  cd $$TMPDIR ;							\
	  svn checkout https://svn.code.sf.net/p/xmds/code/trunk xmds ;	\
	  cd xmds/xpdeint/admin/ ; 					\
	  mkdir userdoc ;						\
	  mv userdoc-source userdoc/src ;				\
	  tar cfvJ xmds2-userdoc.tar.xz userdoc ;			\
	  mv xmds2-userdoc.tar.xz $$CURDIR ;				\
	  echo rm -rf $$TMPDIR )