#!/usr/bin/make -f
#export DH_VERBOSE=1
PYVERS = $(shell pyversions -rv)
PY3VERS = $(shell py3versions -r)
NB = debian/ipython-notebook-common/usr/share/ipython/notebook/static

%:
	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
	cd IPython/html && fab css

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)))
    	# test_not_writable_ipdir, fails as root
	# test_console_starts, leaks python process
	# test_constructor, test_notebook_help, as-installed only (static-path change)
	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 -e test_console_starts \
			-e test_constructor -e test_notebook_help;\
	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/qt/" \
		--exclude="IPython/html/" \
		--exclude=tests
	dh_install -pipython-notebook --exclude="html/static/" --exclude=tests
	dh_install -pipython-notebook-common -XCOPYING -XLICENSE -XLICENSE.txt
	dh_install -pipython-qtconsole --exclude=tests
	dh_install -pipython3 \
		--exclude="IPython/qt/" \
		--exclude="IPython/html/" \
		--exclude=tests
	dh_install -pipython3-notebook --exclude="html/static/" --exclude=tests
	dh_install -pipython3-qtconsole --exclude=tests

	rm -rf $(NB)/components/marked/

	dh_linktree -pipython-notebook-common replace usr/share/fonts-font-awesome usr/share/ipython/notebook/static/components/font-awesome/build/assets/font-awesome

	dh_linktree -pipython-notebook-common replace usr/share/javascript/jquery-ui usr/share/ipython/notebook/static/components/jquery-ui
	install -D IPython/html/static/components/jquery-ui/ui/jquery-ui.js $(NB)/components/jquery-ui/ui/minified/jquery-ui.min.js
	install -D IPython/html/static/components/jquery-ui/ui/i18n/jquery-ui-i18n.js $(NB)/components/jquery-ui/ui/minified/i18n/jquery-ui-i18n.min.js
	install -D IPython/html/static/components/jquery-ui/themes/smoothness/jquery-ui.css $(NB)/components/jquery-ui/themes/smoothness/jquery-ui.min.css

	rm -rf $(NB)/components/jquery/
	install -D IPython/html/static/components/jquery/jquery.js $(NB)/components/jquery/jquery.min.js

	rm -rf $(NB)/components/less.js/

	rm -rf $(NB)/components/highlight.js/

	rm -rf $(NB)/components/requirejs/
	install -D IPython/html/static/components/requirejs/require.js $(NB)/components/requirejs/require.js

	rm -rf $(NB)/components/codemirror/bin/

	rm -rf $(NB)/components/bootstrap/
	make -C IPython/html/static/components/bootstrap/ clean
	make -C IPython/html/static/components/bootstrap/ bootstrap-img
	make -C IPython/html/static/components/bootstrap/ bootstrap-js
	# no bootstrap-css, recess used by makefile is broken #734505
	mkdir -p $(NB)/components/bootstrap/bootstrap/css/
	lessc  IPython/html/static/components/bootstrap/less/bootstrap.less > $(NB)/components/bootstrap/bootstrap/css/bootstrap.min.css
	cp -r IPython/html/static/components/bootstrap/bootstrap/ $(NB)/components/bootstrap/

	mkdir -p debian/ipython3/usr/share/applications/
	sed -e "s/ipython/ipython3/" -e "s/IPython/IPython3/" \
	    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/" \
	    examples/core/ipython-qtconsole.desktop > debian/ipython3-qtconsole/usr/share/applications/ipython3-qtconsole.desktop

	mkdir -p $(CURDIR)/debian/ipython/usr/share/icons/hicolor/scalable/apps/
	cp IPython/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/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
	find $(CURDIR)/debian/ipython-notebook-common -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
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
