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

Vsuf=2x
VSUF=2X

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
uver = $(shell echo $(debver) | cut -d '-' -f -2 )

modulesfile = debian/ipython.environment-modules

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

%:
	dh $@ --with python2

override_dh_auto_clean:
	rm -f test_hist.sqlite ${modulesfile}
	if cd $(CURDIR)/docs; then $(MAKE) clean; fi
	dh_auto_clean

%modules: %modules.in
	sed -e 's,$$UVERSION,$(uver),g' $< >| $@

override_dh_auto_build: $(modulesfile)
	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_installdirs:
	dh_installdirs /usr/share/ipython$(Vsuf)/bin /usr/share/ipython$(Vsuf)/man

override_dh_install:
	dh_install -pipython$(Vsuf) \
		--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/ipython$(Vsuf)/usr/share/man/man1/; \
	for pyvers in $(PYVERS); do \
	    if [ "$${firstpyver}" = "" ]; then \
	        install -m 755 debian/ipython$(Vsuf).sh $(CURDIR)/debian/ipython$(Vsuf)/usr/bin/ipython$(Vsuf)$${pyvers} ;\
	        firstpyver="$$pyvers" ;\
	    else \
	        ln -sf ipython$(Vsuf)$${firstpyver} $(CURDIR)/debian/ipython$(Vsuf)/usr/bin/ipython$(Vsuf)$${pyvers} ;\
	    fi ;\
	    ln -sf ipython$(Vsuf).1.gz $(CURDIR)/debian/ipython$(Vsuf)/usr/share/man/man1/ipython$(Vsuf)$${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 -pipython$(Vsuf)
	dh_installdocs -pipython$(Vsuf)-doc --link-doc=ipython$(Vsuf)
endif

override_dh_installexamples:
	dh_installexamples -pipython$(Vsuf)

override_dh_python2:
	: # and here we are
	: # The Module
	{ /usr/bin/find debian/ipy* -name IPython | \
		while read d; do \
			mkdir $${d}$(VSUF); \
			mv $$d $${d}$(VSUF); \
			echo $$d | grep -q 'debian/ipython$(Vsuf)/' && \
				cp $(CURDIR)/debian/c__init__.py $${d}$(VSUF)/__init__.py || :; \
		 done; }
	: # misc dirs
	{ /usr/bin/find debian/ipy* -mindepth 2 -name ipython -type d | \
		while read d; do mv $$d $${d}$(Vsuf); done; }
	: # man pages -- move under share/ipythonVx/man and symlink back with suffix
	{ /usr/bin/find debian/ipy* -path *man1/\* | \
		while read d; do \
			mv $$d debian/ipython$(Vsuf)/usr/share/ipython$(Vsuf)/man/; \
			ln -sf /usr/share/ipython$(Vsuf)/man/$$(basename $$d) $${d%.1}$(Vsuf).1; \
			done; }
	: # desktop files
	{ /usr/bin/find debian/ipy* -path *.desktop | \
		while read d; do \
			sed -i -e 's,Exec=ipython ,Exec=ipython$(Vsuf) ,g' "$$d"; \
			mv "$$d" "$${d%.desktop}$(Vsuf).desktop"; \
		done; }
	: # emacs files
	{ /usr/bin/find debian/ipy* -path *.el | \
		while read d; do mv $$d $${d%.el}$(Vsuf).el}; done; }
	: # binaries -- two copies -- under share/ipythonVx/bin unadjusted and under /usr/bin adjusted
	{ /usr/bin/find debian/ipy* -path *bin/\* | \
		grep -v '$(Vsuf)[.\d]*$$' | \
		while read d; do \
			cp -p $$d debian/ipython$(Vsuf)/usr/share/ipython$(Vsuf)/bin; \
			mv $$d $${d}$(Vsuf); \
		done; }
	: # Adjust all wrappers to import IPython$(VSUF) first
	find $(CURDIR)/debian/ipy*/usr/bin -type f \
	| xargs sed -i -e 's,\(from IPython[^1].*\),import IPython$(VSUF); \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/ipython$(Vsuf) -iname *.egg-info -delete

	: # Finally install modules file -- if before, directory gets renamed
	install -m 664 -D $(modulesfile) debian/ipython$(Vsuf)/usr/share/modules/modulefiles/ipython/$(uver)


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=ipython$(Vsuf)-$$uver/ --format=tar origin/master \
	| gzip >| ../tarballs/ipython$(Vsuf)_$$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
