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

	# yoh: For this ad-hoc package which would be available
	#      across multiple releases of Debian and Ubuntu, forget about
	#      not carrying external libraries
	## # 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/ipython1x/usr/share/man/man1/; \
	for pyvers in $(PYVERS); do \
	    if [ "$${firstpyver}" = "" ]; then \
	        install -m 755 debian/ipython1x.sh $(CURDIR)/debian/ipython1x/usr/bin/ipython1x$${pyvers} ;\
	        firstpyver="$$pyvers" ;\
	    else \
	        ln -sf ipython1x$${firstpyver} $(CURDIR)/debian/ipython1x/usr/bin/ipython1x$${pyvers} ;\
	    fi ;\
	    ln -sf ipython1x.1.gz $(CURDIR)/debian/ipython1x/usr/share/man/man1/ipython1x$${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 -pipython1x
	dh_installdocs -pipython1x-parallel --link-doc=ipython1x
	dh_installdocs -pipython1x-notebook --link-doc=ipython1x
	dh_installdocs -pipython1x-qtconsole --link-doc=ipython1x
	dh_installdocs -pipython1x-doc --link-doc=ipython1x
endif

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

override_dh_python2:
	: # and here we are
	: # The Module
	{ /usr/bin/find debian/ipy* -name IPython | \
		while read d; do \
			mkdir $${d}1X; \
			mv $$d $${d}1X; \
			echo $$d | grep -q 'debian/ipython1x/' && \
				cp $(CURDIR)/debian/c__init__.py $${d}1X/__init__.py || :; \
		 done; }
	: # misc dirs
	{ /usr/bin/find debian/ipy* -mindepth 2 -name ipython -type d | \
		while read d; do mv $$d $${d}1x; done; }
	: # man pages
	{ /usr/bin/find debian/ipy* -path *man1/\* | \
		while read d; do mv $$d $${d%.1}1x.1; done; }
	: # desktop files
	{ /usr/bin/find debian/ipy* -path *.desktop | \
		while read d; do \
			sed -i -e 's,Exec=ipython ,Exec=ipython1x ,g' $$d; \
			mv $$d $${d%.desktop}1x.desktop; \
		done; }
	: # emacs files
	{ /usr/bin/find debian/ipy* -path *.el | \
		while read d; do mv $$d $${d%.el}1x.el}; done; }
	: # binaries
	{ /usr/bin/find debian/ipy* -path *bin/\* | \
		grep -v '1x[.\d]*$$' | \
		while read d; do mv $$d $${d}1x; done; }
	: # Adjust all wrappers to import IPython1X first
	find $(CURDIR)/debian/ipy*/usr/bin -type f \
	| xargs sed -i -e 's,\(from IPython[^1].*\),import IPython1X; \1,g'
	: # 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/ipython1x -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=ipython1x-$$uver/ --format=tar origin/master \
	| gzip >| ../tarballs/ipython1x_$$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
