#!/usr/bin/make -f

DH_VERBOSE := 1

include /usr/share/dpkg/default.mk

docpkg:=$(DEB_SOURCE)-doc

export PYBUILD_NAME=ruffus

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_auto_build-indep:
ifeq (,$(filter nodoc, $(DEB_BUILD_OPTIONS)))
	make --directory=doc html
endif

override_dh_installdocs:
	dh_installdocs -ppython-ruffus-doc doc/_build/html
	dh_installdocs -A
	find debian -name run_all_unit_tests.cmd -delete

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 \
			        if [ -e $(CURDIR)/ruffus/test/Makefile ] ; then ln -s $(CURDIR)/ruffus/test/Makefile ; else \
			            echo "Expected file $(CURDIR)/ruffus/test/Makefile not found" ; \
			            echo "Use `find $(CURDIR) -name Makefile | grep test` instead" ; \
			            ln -s `find $(CURDIR) -name Makefile | grep test | head -n1` ; \
			        fi ; \
			        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
	find .pybuild -name tmp | xargs rm -rf
endif

execute_after_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
	rm -f  ruffus.egg-info/PKG-INFO ruffus.egg-info/SOURCES.txt ruffus.egg-info/dependency_links.txt ruffus.egg-info/top_level.txt


execute_after_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
	sed -i '1s?^#! */usr/bin/env *python *$$?#!/usr/bin/python3?' `grep -Rl '^#! */usr/bin/env *python *$$' debian/python3-$(PYBUILD_NAME)`

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