#!/usr/bin/make -f
#export DH_VERBOSE=1
#export DEB_BUILD_OPTIONS=nocheck
export PYBUILD_NAME=theano
export NODE_PATH=$(CURDIR)/debian/missing-source
export PYBUILD_INSTALL_ARGS=--install-scripts=/usr/share/{package}
export THEANO_FLAGS=base_compiledir='.pybuild',device=cpu
export PYTHONPATH=$(CURDIR)
export HOME=$(CURDIR)/jupyter_tmp

%:
	dh $@ --with python2,python3,sphinxdoc --buildsystem=pybuild

override_dh_auto_clean:
	rm -rf theano/d3viz/js/*.min.js doc/library/d3viz/examples/d3viz/js/*.min.js debian/missing-source/*.js debian/missing-source/lodash debian/missing-source/graphlib-dot/bower.json debian/missing-source/graphlib-dot/lib/dot-grammar.js debian/missing-source/graphlib-dot/lib/version.js debian/missing-source/graphlib-dot/dist doc/.pybuild doc/library/d3viz/.pybuild Theano.egg-info theano/scan_module/c_code/scan_perform.c jupyter_tmp
	make -C debian/missing-source/graphlib-dot clean
	dh_auto_clean

override_dh_auto_build:
	ln -s /usr/lib/nodejs/lodash-compat debian/missing-source/lodash
	#graphlib-dot.js
	make -C debian/missing-source/graphlib-dot --always-make dist
	cp debian/missing-source/graphlib-dot/dist/graphlib-dot.min.js theano/d3viz/js
	#dagre-d3.js
	browserify-lite ./debian/missing-source/dagre-d3/index.js --outfile debian/missing-source/dagre-d3.js --standalone dagreD3
	uglifyjs debian/missing-source/dagre-d3.js --comments '@license' -o theano/d3viz/js/dagre-d3.min.js
	#d3 - workaround for #745688
	uglifyjs /usr/share/javascript/d3/d3.js -m -c --comments '@license' -o theano/d3viz/js/d3.v3.min.js
	cp theano/d3viz/js/*.min.js doc/library/d3viz/examples/d3viz/js
	#the cython code is the only part of theano itself that needs building - the C code is runtime compiled
	#the patch is to code Cython copies from itself (Cython/Includes/numpy/__init__.pxd), that only exists in older versions - #918771
	cd $(CURDIR)/theano/scan_module && cython3 scan_perform.pyx
	cd $(CURDIR)/theano/scan_module && dpkg --compare-versions `dpkg-query -f '$${Version}' -W cython3` gt 0.29~ || patch --no-backup-if-mismatch scan_perform.c numpy_api_changes.diff
	cd $(CURDIR)/theano/scan_module && mv scan_perform.c -t c_code
	#documentation
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	sed -i -e "s/!pip/#!pip/g" doc/library/d3viz/index.ipynb
	mkdir jupyter_tmp
	#check that we can run this, but throw away the result because it's unreproducible (profiling output)
	cp $(CURDIR)/doc/library/d3viz/index.ipynb jupyter_tmp && jupyter-nbconvert --to notebook --execute $(CURDIR)/jupyter_tmp/index.ipynb --output $(CURDIR)/jupyter_tmp/test.ipynb
	#...but do use the ipynb-to-html-etc conversion
	jupyter-nbconvert --to html doc/library/d3viz/index.ipynb
	sed -i -e "s/#!pip/!pip/g" doc/library/d3viz/index.ipynb
	python3 doc/scripts/docgen.py
	rst2html README.rst README.html
	cat NEWS.txt HISTORY.txt | gzip -n -9 > NEWS.gz
endif

override_dh_auto_install:
	dh_auto_install
	rm -fv debian/python*-theano/usr/lib/python*/dist-packages/theano/misc/check_blas_many.sh


override_dh_auto_test:
	PYBUILD_SYSTEM=custom PYBUILD_TEST_ARGS='PYTHONPATH=. {interpreter} theano/tests/run_tests_in_batch.py' dh_auto_test

override_dh_installdocs:
	dh_installdocs -A README.html NEWS.gz
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	rdfind -outputname /dev/null -makesymlinks true debian/theano-doc
	symlinks -r -s -c debian/theano-doc
endif

override_dh_installchangelogs:
	#the -X is meant as "don't try to install upstream changelog, only Debian's" - #906827
	dh_installchangelogs -Xtxt

override_dh_sphinxdoc:
	dh_sphinxdoc
	rm -fv debian/theano-doc/usr/share/doc/theano-doc/html/_sources/LICENSE.txt # extra-license-file

override_dh_compress:
	dh_compress -X.py -X.ipynb -X.pdf # save theano-doc/html/_downloads

override_dh_gencontrol:
	dh_gencontrol -- -VjavascriptBU="$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), \n' -W node-lodash-compat)$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), \n' -W libjs-d3)"