#!/usr/bin/make -f

#export DH_VERBOSE=1 # can also set this on each debian/rules command line
export LC_ALL=C.UTF-8

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

override_dh_auto_configure:
	dh_auto_configure
	dh_auto_configure -- -d ./widgetsnbextension

override_dh_auto_build:
# fakewebpack
	$(MAKE) -C debian -f fakewebpack.mk all
	touch debian/fakewebpack/widgetsnbextension/extension.js.map
	# See that patch for a description of why this is needed
	#patch -l -F400 debian/fakewebpack/docs/index.js debian/webpack-compat/jquery.patch
# ipywidgets
	dh_auto_build
# widgetsnbextension
	# The below dance is only to satisfy widgetsnbextension/setup.py being
	# extra-picky and is otherwise useless, since we replace all these files
	# with symlinks later in override_dh_auto_install
	mkdir -p widgetsnbextension/widgetsnbextension/static
	cp debian/fakewebpack/widgetsnbextension/* \
	  widgetsnbextension/widgetsnbextension/static/
	dh_auto_build -Dwidgetsnbextension
#ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
# ipywidgets-doc
#	dh_auto_build -Ddocs -Smakefile -- html
#	cp -a debian/fakewebpack/docs docs/build/html/_static/built
#	find docs/build/html/_static/built -type f -empty -delete
#endif

override_dh_auto_install:
	dh_auto_install
	dh_auto_install -- -d ./widgetsnbextension
	set -e; for i in debian/tmp/usr/lib/*/dist-packages/widgetsnbextension/static; do \
	  rm -rf $$i && ln -sfT /usr/share/jupyter/nbextensions/jupyter-js-widgets $$i; \
	done

override_dh_auto_clean:
#ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
# ipywidgets-doc
#	dh_auto_clean -Ddocs -Smakefile -- clean
#endif
# widgetsnbextension
	dh_auto_clean -Dwidgetsnbextension
	rm -rf widgetsnbextension/widgetsnbextension/static
# ipywidgets
	dh_auto_clean
# fakewebpack
	$(MAKE) -C debian -f fakewebpack.mk clean

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# from docs/source/dev_testing.md
	nosetests --with-coverage --cover-package=ipywidgets ipywidgets
	# no tests in widgetsnbextension
endif

override_dh_compress:
	dh_compress -X python-ipywidgets-doc/html

gen_built_using = \
	$(MAKE) -C debian -f fakewebpack.mk fakewebpack/$(1).packages && \
	echo "fakewebpack:Built-Using=$$(cat debian/fakewebpack/$(1).packages)" >> debian/$(2).substvars
override_dh_gencontrol:
#ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
#	$(call gen_built_using,docs,python-ipywidgets-doc)
#endif
	$(call gen_built_using,widgetsnbextension,jupyter-nbextension-jupyter-js-widgets)
	dh_gencontrol

override_dh_sphinxdoc:
	dh_sphinxdoc -X_static/mathjax
	# Replace embedded jupyter-sphinx-theme files with symlinks
	set -e; for i in debian/python-ipywidgets-doc/usr/share/doc/python-ipywidgets-doc/html/_static/*; do \
	  if [ -e "/usr/share/jupyter_sphinx_theme/jupyter/static/$$(basename "$$i")" ]; then \
	    rm -rf "$$i"; \
	    ln -sfT "/usr/share/jupyter_sphinx_theme/jupyter/static/$$(basename "$$i")" "$$i"; \
	  fi; \
	done
