#!/usr/bin/make -f

DH_VERBOSE := 1

include /usr/share/dpkg/default.mk

docpkg:=$(DEB_SOURCE)-doc

export PYBUILD_NAME=ruffus

%:
	dh $@ --with python2,python3,sphinxdoc --buildsystem=pybuild

override_dh_auto_build:
	dh_auto_build
	make --directory=doc html

override_dh_installdocs:
	dh_installdocs -ppython-ruffus-doc doc/_build/html
	dh_installdocs -A

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test -- --test --system=custom \
		--test-args='set -e; \
			    cd {build_dir}/$(PYBUILD_NAME)/test ; \
			    if [ "{version.major}" = 2 ] ; then \
				ln -s /build/python-ruffus-*/ruffus/test/Makefile ; \
			        cp -a run_all_unit_tests.cmd /tmp/run_all_unit_tests.cmd ; \
			    else \
				sed "s/^python3/python{version}/" run_all_unit_tests3.cmd > /tmp/run_all_unit_tests.cmd ; \
			    fi ;\
                            PYTHONPATH={build_dir} HOME=`mktemp -d` bash /tmp/run_all_unit_tests.cmd'
	find . -name .ruffus_history.sqlite -delete
	find . -name "*.pyc" -delete
	find . -name __pycache__ | xargs rm -rf
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -rf ruffus/test/temp_branching_dir \
	       ruffus/test/temp_filesre_combine \
	       ruffus/test/test_pausing_dir \
	       ruffus/test/tmp_test_job_history_with_exceptions \
	       ruffus/test/.ruffus_history.sqlite \
	       ruffus/__pycache__ \
	       doc/_build/html \
	       doc/_build/doctrees \
	       ruffus/test/*.log \
	       .ruffus_history.sqlite

override_dh_install:
	dh_install
	# test files have all #!/usr/bin/python which looks wrong in python3 package
	find debian/python3-$(PYBUILD_NAME)/usr/lib/python3*/dist-packages/ruffus/test -name "*.py" -exec sed -i 's+^\(#!/usr/bin/.*python\)[[:space:]]*$$+\13+' \{\} \;
	# leave only the test starter that fits Python version
	find debian/python3-$(PYBUILD_NAME) -name run_all_unit_tests.cmd -delete
	find debian/python-$(PYBUILD_NAME) -name run_all_unit_tests3.cmd -delete
	sed -i '1s?^#! */usr/bin/env *python *$$?#!/usr/bin/python3?' `grep -Rl '^#! */usr/bin/env *python *$$' debian/python3-$(PYBUILD_NAME)`

override_dh_fixperms:
	dh_fixperms
	find debian -name "run_all_unit_tests*.cmd" -executable -exec chmod -x \{\} \;
