#!/usr/bin/make -f
export DH_VERBOSE=1
PYVERS = $(shell pyversions -rv)

override_dh_compress:
	# make sure the documentation stays usable.
	dh_compress -Xhtml/

%:
	dh $@ --with python2,sphinxdoc

override_dh_auto_clean:
	rm -f test_hist.sqlite
	if cd $(CURDIR)/docs; then $(MAKE) clean; fi
	dh_auto_clean

override_dh_auto_build:
	dh_auto_build -- --executable '/usr/bin/env python'
ifeq (,$(filter nodocs,$(DEB_BUILD_OPTIONS)))
	PYTHONPATH=$(CURDIR) $(MAKE) -C $(CURDIR)/docs html
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# IPython.frontend requires X
	# temporary ignore failure in experimental, parallel testsuite fails randomly
	-set -e && for pyvers in $(PYVERS); do \
	  LC_ALL=C.UTF-8 PATH=$(CURDIR)/IPython/scripts/:$(PATH) PYTHONPATH=$(CURDIR) xvfb-run -s "-screen 0 1280x1024x24 -noreset" \
	  python$$pyvers $(CURDIR)/IPython/scripts/iptest -v; \
	done
endif

override_dh_install:
	dh_install -pipython --exclude="IPython/parallel/" \
		--exclude="IPython/frontend/qt/" \
		--exclude="IPython/deathrow/" \
		--exclude="IPython/quarantine/"
	dh_install -pipython-parallel IPython/parallel/ usr/share/pyshared/IPython
	dh_install -pipython-qtconsole IPython/frontend/qt/ usr/share/pyshared/IPython/frontend

	# remove embedded arparse
	find $(CURDIR)/debian/ipython/usr/ -name _argparse.py -delete
	
	# remove embedded configobj
	find $(CURDIR)/debian/ipython/usr/ -name _configobj.py -delete
	find $(CURDIR)/debian/ipython/usr/ -name _validate.py -delete

	# remove embedded simplegeneric
	find $(CURDIR)/debian/ipython/usr/ -name _simplegeneric.py -delete

	# remove embedded decorator
	find $(CURDIR)/debian/ipython/usr/ -name _decorator.py -delete

	# remove embedded pexpect
	find $(CURDIR)/debian/ipython/usr/ -name _pexpect.py -delete

	# remove embedded pyparsing
	find $(CURDIR)/debian/ipython/usr/ -name _pyparsing.py -delete

	# add wrapper scripts for all python versions
	set -e ;\
	firstpyver="" ;\
	for pyvers in $(PYVERS); do \
	    if [ "$${firstpyver}" = "" ]; then \
	        install -m 755 debian/ipython.sh $(CURDIR)/debian/ipython/usr/bin/ipython$$pyvers ;\
	        firstpyver="$$pyvers" ;\
	    else \
	        dh_link /usr/bin/ipython$${firstpyver} /usr/bin/ipython$$pyvers ;\
	    fi ;\
	    dh_link /usr/share/man/man1/ipython.1.gz /usr/share/man/man1/ipython$$pyvers.1.gz ;\
	done

	# change permission on scripts
	chmod a-x $(CURDIR)/debian/ipython/usr/share/doc/ipython/examples/*
	
override_dh_installdocs:
ifeq (,$(filter nodocs,$(DEB_BUILD_OPTIONS)))
	dh_installdocs -pipython
	dh_installdocs -pipython-parallel --link-doc=ipython
	dh_installdocs -pipython-qtconsole --link-doc=ipython
	dh_installdocs -pipython-doc --link-doc=ipython
	find $(CURDIR)/debian/ipython -type d -empty -delete

	rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython/html/_images/ms_visual_studio.png
	dh_link -pipython-doc usr/share/doc/ipython/html/_static/ms_visual_studio.png \
	  usr/share/doc/ipython/html/_images/ms_visual_studio.png
	rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython/html/_images/qtconsole.png
	dh_link -pipython-doc usr/share/doc/ipython/html/_static/qtconsole.png \
	  usr/share/doc/ipython/html/_images/qtconsole.png
endif

override_dh_installexamples:
	dh_installexamples -pipython --exclude="examples/newparallel/"
	dh_installexamples -pipython-parallel

override_dh_sphinxdoc:
ifeq (,$(filter nodocs,$(DEB_BUILD_OPTIONS)))
	dh_sphinxdoc
endif
