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

%:
	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
	# remove 0.13 generated files
	rm -f $(CURDIR)/docs/source/_static/hist_simple.png
	rm -f $(CURDIR)/docs/source/_static/hist_with_text.png
	rm -f $(CURDIR)/docs/source/_static/plot_simple.png
	rm -f $(CURDIR)/docs/source/_static/plot_simple_python.png

override_dh_auto_build:
	dh_auto_build -- --executable '/usr/bin/env python'
	# utf-8 due to https://github.com/ipython/ipython/issues/2057
	set -e && for pyvers in $(PY3VERS); do \
	  $$pyvers setup.py build --executable '/usr/bin/env python3'; \
	done

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 -a -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="IPython/external/js/" \
		--exclude=tests
	dh_install -pipython-notebook --exclude="html/notebook/static/" --exclude=tests
	dh_install -pipython-notebook-common -XCOPYING -XLICENSE -XLICENSE.txt
	dh_install -pipython-qtconsole --exclude=tests
	dh_install -pipython3 \
		--exclude="IPython/frontend/qt/" \
		--exclude="IPython/frontend/html/" \
		--exclude="IPython/deathrow/" \
		--exclude="IPython/quarantine/" \
		--exclude=tests
	dh_install -pipython3-notebook --exclude="html/notebook/static/" --exclude=tests
	dh_install -pipython3-qtconsole --exclude=tests

	mkdir -p debian/ipython3/usr/share/applications/
	sed -e "s/ipython/ipython3/" -e "s/IPython/IPython3/" \
	    docs/examples/core/ipython.desktop > debian/ipython3/usr/share/applications/ipython3.desktop
	mkdir -p debian/ipython3-qtconsole/usr/share/applications/
	sed -e "s/ipython/ipython3/" -e "s/IPython/IPython3/" \
	    docs/examples/core/ipython-qtconsole.desktop > debian/ipython3-qtconsole/usr/share/applications/ipython3-qtconsole.desktop

	# jquery-ui-themes provides more in javascript/jquery-ui-themes (not available in 12.04)
	# disabled as long as the ui dev branch is used
	#dh_linktree -v -pipython-notebook-common replace usr/share/javascript/jquery-ui/themes \
	#  usr/share/ipython/notebook/static/jquery/css/themes

	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
	mkdir -p $(CURDIR)/debian/ipython3/usr/share/icons/hicolor/scalable/apps/
	cp IPython/frontend/qt/console/resources/icon/IPythonConsole.svg \
	  $(CURDIR)/debian/ipython3/usr/share/icons/hicolor/scalable/apps/ipython3.svg

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

	# remove embedded simplegeneric
	find $(CURDIR)/debian/ipython/usr/ -name _simplegeneric.py -delete
	find $(CURDIR)/debian/ipython3/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

	# 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/*
	find $(CURDIR)/debian/ipython-notebook-common/usr/share/ipython/notebook/static/codemirror/ \
	  -type f | xargs chmod a-x
	
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 -pipython3-notebook --link-doc=ipython3
	dh_installdocs -pipython-notebook-common
	dh_installdocs -pipython-doc
	find $(CURDIR)/debian/ipython -type d -empty -delete

	# remove all the duplicates to keep -doc small
	set -e && \
	for f in hist_simple.png hist_with_text.png ipy_013_dashboard_cluster.png ipy_013_dashboard.png \
	ipy_013_notebook_cythonmagic.png ipy_013_notebook_long_out.png ipy_013_notebook_octavemagic.png \
	ipy_013_notebook_rmagic.png ipy_013_notebook_script_cells.png ipy_013_notebook_spectrogram.png \
	ipy_013_notebook_tooltip.png ipy_013_par_tb.png ipy_013_qtconsole_completer.png plot_simple.png \
	plot_simple_python.png ms_visual_studio.png notebook_specgram.png \
	qtconsole.png qtconsole_tabbed.png; \
	do \
	  rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython-doc/html/_images/$$f; \
	  dh_link -pipython-doc usr/share/doc/ipython-doc/html/_static/$$f usr/share/doc/ipython-doc/html/_images/$$f; \
	done
	rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython-doc/html/_static/plot_simple.png
	dh_link -pipython-doc usr/share/doc/ipython-doc/html/_static/plot_simple_python.png \
	  usr/share/doc/ipython-doc/html/_static/plot_simple.png
	rm -f $(CURDIR)/debian/ipython-doc/usr/share/doc/ipython-doc/html/_images/qtconsole1.png
	dh_link -pipython-doc usr/share/doc/ipython-doc/html/_static/qtconsole.png \
	  usr/share/doc/ipython-doc/html/_images/qtconsole1.png
endif

# work around #683108
override_dh_python2:
	dh_python2 -pipython
	dh_python2 -pipython-qtconsole
	dh_python2 -pipython-notebook

override_dh_python3:
	dh_python3 -pipython3
	dh_python3 -pipython3-qtconsole
	dh_python3 -pipython3-notebook

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

override_dh_compress:
	dh_compress -Xhtml/ -X.ipynb -X.py

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
