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

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

%:
	dh $@ --with python2

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 -pipython01x \
		--exclude="IPython/deathrow/" \
		--exclude="IPython/quarantine/"

	# 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="" ;\
	mkdir -p $(CURDIR)/debian/ipython01x/usr/share/man/man1/; \
	for pyvers in $(PYVERS); do \
	    if [ "$${firstpyver}" = "" ]; then \
	        install -m 755 debian/ipython01x.sh $(CURDIR)/debian/ipython01x/usr/bin/ipython01x$${pyvers} ;\
	        firstpyver="$$pyvers" ;\
	    else \
	        ln -sf ipython01x$${firstpyver} $(CURDIR)/debian/ipython01x/usr/bin/ipython01x$${pyvers} ;\
	    fi ;\
	    ln -sf ipython01x.1.gz $(CURDIR)/debian/ipython01x/usr/share/man/man1/ipython01x$${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 -pipython01x
	dh_installdocs -pipython01x-parallel --link-doc=ipython01x
	dh_installdocs -pipython01x-notebook --link-doc=ipython01x
	dh_installdocs -pipython01x-qtconsole --link-doc=ipython01x
	dh_installdocs -pipython01x-doc --link-doc=ipython01x
endif

override_dh_installexamples:
	dh_installexamples -pipython01x
	# yoh: disabled for now since at this point link is dangling and do not want more mess with renamings
	# dh_installexamples -pipython01x-parallel

override_dh_python2:
	: # and here we are
	: # The Module
	{ /usr/bin/find debian/ipy* -name IPython | \
		while read d; do \
			mkdir $${d}01X; \
			mv $$d $${d}01X; \
			echo $$d | grep -q 'debian/ipython01x/' && \
				cp $(CURDIR)/debian/c__init__.py $${d}01X/__init__.py || :; \
		 done; }
	: # misc dirs
	{ /usr/bin/find debian/ipy* -mindepth 2 -name ipython -type d | \
		while read d; do mv $$d $${d}01x; done; }
	: # man pages
	{ /usr/bin/find debian/ipy* -path *man1/\* | \
		while read d; do mv $$d $${d%.1}01x.1; done; }
	: # desktop files
	{ /usr/bin/find debian/ipy* -path *.desktop | \
		while read d; do \
			sed -i -e 's,Exec=ipython ,Exec=ipython01x ,g' $$d; \
			mv $$d $${d%.desktop}01x.desktop; \
		done; }
	: # emacs files
	{ /usr/bin/find debian/ipy* -path *.el | \
		while read d; do mv $$d $${d%.el}01x.el}; done; }
	: # binaries
	{ /usr/bin/find debian/ipy* -path *bin/\* | \
		grep -v 'bin/ipython01x' | \
		while read d; do mv $$d $${d}01x; done; }
	: # Adjust all wrappers to import IPython01X first
	sed -i -e 's,\(from IPython\..*\),import IPython01X; \1,g' $(CURDIR)/debian/ipy*/usr/bin/*
	: # call actual dh_python2
	dh_python2
	: # and remove .egg-info since they are anyways somewhat incorrect now and I do not think they are mandatory here fro ipython
	find ./debian/ipython01x -iname *.egg-info -delete

get-orig-source-git:
	git fetch origin
	uver=$$(git describe origin/master | sed -e 's,rel-\([.0-9]*\)-\([0-9]*-g.*\),\1+git\2,g') && \
	git archive --prefix=ipython01x-$$uver/ --format=tar origin/master \
	| gzip >| ../tarballs/ipython01x_$$uver.orig.tar.gz && \
	dch --noconf --newversion $$uver-1 -D neurodebian --force-distribution "Fresh upstream snapshot" && \
	git commit -m "Changelog for a new snapshot revision $$uver" debian/changelog
