#!/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 -f install-stamp
	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
	# remove empty files that make dh_sphinxdoc fail
	rm -f ./docs/source/whatsnew/pr/antigravity-feature.rst
	rm -f ./docs/source/whatsnew/pr/incompat-switching-to-perl.rst

ifeq (,$(filter nodocs,$(DEB_BUILD_OPTIONS)))
	HOME=$(CURDIR)/build PYTHONPATH=$(CURDIR) $(MAKE) -C $(CURDIR)/docs html
endif

install-test: install-stamp
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
    	# test_not_writable_ipdir, fails as root
	# test_console_starts, leaks python process
	# test_exit_code_signal sometimes fails with git-buildpackage but not when run manually
	cd build && LC_ALL=C.UTF-8 HOME=$(CURDIR)/build \
	PATH=$(CURDIR)/debian/tmp/usr/bin/:$(PATH) PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages \
	xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" \
	iptest2 -- -e test_not_writable_ipdir -e test_console_starts -e test_exit_code_signal;
else
	:
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
	# fix shebangs
	sed -i '1c#!/usr/bin/env python' $(CURDIR)/debian/tmp/usr/bin/ipython
	sed -i '1c#!/usr/bin/env python' $(CURDIR)/debian/tmp/usr/bin/ipcluster
	sed -i '1c#!/usr/bin/env python' $(CURDIR)/debian/tmp/usr/bin/ipcontroller
	sed -i '1c#!/usr/bin/env python' $(CURDIR)/debian/tmp/usr/bin/ipengine
	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/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

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
	# no space allowed in dh_install
	install -D -m 644 "examples/IPython Kernel/ipython.desktop" debian/ipython/usr/share/applications/ipython.desktop
	install -D -m 644 "examples/IPython Kernel/ipython-qtconsole.desktop" debian/ipython-qtconsole/usr/share/applications/ipython-qtconsole.desktop
	# workaround for broken by space launchpad builds
	sed -i -e "s/gnome-netstatus-idle/ipython/" debian/ipython*/usr/share/applications/*desktop

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

	# font awesome changed location at some point
	if test -e /usr/share/fonts-font-awesome/fonts; then \
	  dh_linktree -pipython-notebook-common replace usr/share/fonts-font-awesome/fonts \
	    usr/share/ipython/notebook/static/components/font-awesome/font; \
	else \
	  dh_linktree -pipython-notebook-common replace usr/share/fonts-font-awesome/font \
	    usr/share/ipython/notebook/static/components/font-awesome/font; \
	fi

	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/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/

	rm -rf $(NB)/components/bootstrap-tour/
	install -D IPython/html/static/components/bootstrap-tour/build/js/bootstrap-tour.js \
	  $(NB)/components/bootstrap-tour/build/js/bootstrap-tour.min.js
	install -D IPython/html/static/components/bootstrap-tour/build/css/bootstrap-tour.min.css \
	  $(NB)/components/bootstrap-tour/build/css/bootstrap-tour.min.css

	rm -rf $(NB)/components/google-caja
	install -D IPython/html/static/components/google-caja/html-css-sanitizer-bundle.js \
	  $(NB)/components/google-caja/html-css-sanitizer-minified.js

	# 0.9 packaged likely too old
	rm -rf $(NB)/components/backbone
	install -D ./IPython/html/static/components/backbone/backbone.js \
	  $(NB)/components/backbone/backbone-min.js

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

	# change permission on scripts
	find $(CURDIR)/debian/ipython-notebook-common -type f | xargs chmod a-x

	touch install-stamp

	
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: install-test
	dh_python2 -pipython
	dh_python2 -pipython-qtconsole
	dh_python2 -pipython-notebook

override_dh_python3:
	dh_python3 -pipython3 --no-shebang-rewrite || dh_python3 -pipython3
	dh_python3 -pipython3-qtconsole --no-shebang-rewrite || dh_python3 -pipython3-qtconsole
	dh_python3 -pipython3-notebook --no-shebang-rewrite || 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/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

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

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