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

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

%:
	dh $@ --with python2,python3,sphinxdoc

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

override_dh_auto_build:
	dh_auto_build -- --executable '/usr/bin/env python'
	set -e && for pyvers in $(PY3VERS); do \
	  $$pyvers setup.py build --executable '/usr/bin/env python3'; \
	done
	# so dh_python2 symlinks the missing stuff
	touch IPython/frontend/html/notebook/static/jquery/js/jquery-1.6.2.min.js
	touch IPython/frontend/html/notebook/static/jquery/js/jquery-ui-1.8.14.custom.min.js
ifeq (,$(filter nodocs,$(DEB_BUILD_OPTIONS)))
	HOME=$(CURDIR)/build PYTHONPATH=$(CURDIR) $(MAKE) -C $(CURDIR)/docs html
endif

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

override_dh_auto_install:
	dh_auto_install
	set -e && for pyvers in $(PY3VERS); do \
	  $$pyvers setup.py install --install-layout=deb \
		--force --no-compile -O0 --root=$(CURDIR)/debian/tmp; \
	done
	# py3 uses entrypoints where --excutable in build has no effect
	sed -i '1c#!/usr/bin/env python3' $(CURDIR)/debian/tmp/usr/bin/ipython3
	sed -i '1c#!/usr/bin/env python3' $(CURDIR)/debian/tmp/usr/bin/irunner3
	sed -i '1c#!/usr/bin/env python3' $(CURDIR)/debian/tmp/usr/bin/ipcluster3
	sed -i '1c#!/usr/bin/env python3' $(CURDIR)/debian/tmp/usr/bin/ipcontroller3
	sed -i '1c#!/usr/bin/env python3' $(CURDIR)/debian/tmp/usr/bin/ipengine3
	sed -i '1c#!/usr/bin/env python3' $(CURDIR)/debian/tmp/usr/bin/iplogger3

override_dh_install:
	dh_install -pipython \
		--exclude="IPython/frontend/qt/" \
		--exclude="IPython/frontend/html/" \
		--exclude="IPython/deathrow/" \
		--exclude="IPython/quarantine/" \
		--exclude=tests
	dh_install -pipython-notebook -XCOPYING -XLICENSE -XLICENSE.txt
	dh_install -pipython-qtconsole
	dh_install -pipython3 \
		--exclude="IPython/frontend/qt/" \
		--exclude="IPython/frontend/html/" \
		--exclude="IPython/deathrow/" \
		--exclude="IPython/quarantine/" \
		--exclude=tests
	dh_install -pipython3-qtconsole

	mkdir -p $(CURDIR)/debian/ipython/usr/share/icons/hicolor/scalable/apps/
	cp IPython/frontend/qt/console/resources/icon/IPythonConsole.svg \
	  $(CURDIR)/debian/ipython/usr/share/icons/hicolor/scalable/apps/ipython.svg

	# remove embedded arparse
	find $(CURDIR)/debian/ipython/usr/ -name _argparse.py -delete
	find $(CURDIR)/debian/ipython3/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
	find $(CURDIR)/debian/ipython3/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 -pipython /usr/bin/ipython$${firstpyver} /usr/bin/ipython$$pyvers ;\
	    fi ;\
	    dh_link -pipython /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 -pipython3
	dh_installdocs -pipython-qtconsole --link-doc=ipython
	dh_installdocs -pipython3-qtconsole --link-doc=ipython3
	dh_installdocs -pipython-notebook --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
	rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython/html/_images/qtconsole1.png
	dh_link -pipython-doc usr/share/doc/ipython/html/_static/qtconsole.png \
	  usr/share/doc/ipython/html/_images/qtconsole1.png
	rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython/html/_images/qtconsole_tabbed.png
	dh_link -pipython-doc usr/share/doc/ipython/html/_static/qtconsole_tabbed.png \
	  usr/share/doc/ipython/html/_images/qtconsole_tabbed.png
	rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython/html/_images/notebook_specgram.png
	dh_link -pipython-doc usr/share/doc/ipython/html/_static/notebook_specgram.png \
	  usr/share/doc/ipython/html/_images/notebook_specgram.png
endif

override_dh_installman:
	dh_installman
	mv debian/ipython3/usr/share/man/man1/ipython.1 \
	  debian/ipython3/usr/share/man/man1/ipython3.1
	mv debian/ipython3/usr/share/man/man1/irunner.1 \
	  debian/ipython3/usr/share/man/man1/irunner3.1
	mv debian/ipython3/usr/share/man/man1/ipcluster.1 \
	  debian/ipython3/usr/share/man/man1/ipcluster3.1
	mv debian/ipython3/usr/share/man/man1/ipcontroller.1 \
	  debian/ipython3/usr/share/man/man1/ipcontroller3.1
	mv debian/ipython3/usr/share/man/man1/ipengine.1 \
	  debian/ipython3/usr/share/man/man1/ipengine3.1
	mv debian/ipython3/usr/share/man/man1/iplogger.1 \
	  debian/ipython3/usr/share/man/man1/iplogger3.1

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

VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-\+]+)+(\+dfsg)*.*,\1,p')
COMMIT = $(shell dpkg-parsechangelog | grep Commit | sed -e "s/^\s*Commit //")
get-orig-source:
	#git clone --depth 1 git://github.com/ipython/ipython ipython.git
	#cd ipython.git && git archive --format=tar --prefix=ipython/ $(COMMIT) | gzip -9 -c > ../ipython_$(VERSION).orig.tar.gz
	#rm -rf ipython.git
	#tar xfz ipython_$(VERSION).orig.tar.gz
	mkdir ipython
	uscan --force-download --destdir=. --download-version=$(VERSION)
	tar -C ipython -xz -f rel-$(VERSION).tar.gz --strip 1
	rm -f ipython/IPython/frontend/html/notebook/static/jquery/js/jquery-1.6.2.min.js
	rm -f ipython/IPython/frontend/html/notebook/static/jquery/js/jquery-ui-1.8.14.custom.min.js
	mv ipython/IPython/frontend/html/notebook/static/unminified/prettify.js \
	   ipython/IPython/frontend/html/notebook/static/prettify/prettify.js
	tar cfz ipython_$(VERSION)+dfsg.orig.tar.gz ipython
	rm -rf ipython
