#!/usr/bin/make -f

DH_VERBOSE := 1
include /usr/share/dpkg/default.mk
export PYBUILD_NAME=cwltool
export PYBUILD_TEST_ARGS=-n=auto
export PYBUILD_AFTER_TEST=rm -Rf {build_dir}/tmp/ {build_dir}/main.cwl {build_dir}/output.txt*
export HOME=$(shell echo $$PWD"/fakehome")

%:
	dh $@ --with python3 --buildsystem=pybuild

debian/cwltool.1: debian/rules debian/cwltool.help2man
	python3 setup.py develop --user
	help2man --version-string $(DEB_VERSION_EPOCH_UPSTREAM) -I debian/cwltool.help2man -N \
		-o debian/cwltool.1  'python3 -m cwltool'
	sed -i 's/__main__.py/cwltool/g' debian/cwltool.1

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
override_dh_installman: debian/cwltool.1
	dh_installman
else
override_dh_installman:
endif

override_dh_auto_install:
	dh_auto_install
	find debian -name '*.cwl' -exec chmod a+x {} \;
	find debian -name '*.json' -exec chmod a-x {} \;

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_PROFILES)))
	dh_auto_install
	PYBUILD_SYSTEM=custom \
	PYBUILD_TEST_ARGS="cd {dir}; export PATH={destdir}/usr/bin:$$PATH ; \
	cd {build_dir}; export PYTHONPATH=$$(pwd); {interpreter} -m pytest \
		-k 'not (test_http_path_mapping or test_get_subgraph or test_udocker_usage_should_not_write_cid_file or test_udocker_should_display_memory_usage or test_content_types or test_get_step)' \
		-n auto --ignore cwltool/schemas/ -rs --pyargs cwltool" dh_auto_test
	find .pybuild -name "out" -type f -delete
endif
# test_http_path_mapping requires internet access (could be mocked?)
# test_get_{subgraph,step} embedded local path isn't being cleaned properly
