#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

DH_OPTIONS = -O--buildsystem=pybuild

cmd = ontospy
_ENV = $(strip \
 PATH="$(CURDIR)/debian/tmp/usr/bin:$(PATH)" \
 PYTHONPATH="$(CURDIR)/debian/tmp/usr/lib/$(shell py3versions -d)/dist-packages")

# generate manpage with help2man from --help option of python script
_mkman = $(_ENV) \
 help2man $(if $3,--name "$(strip $3)") --no-info --version-string $(DEB_VERSION_UPSTREAM) --output $2 $1 \
 || { $(_ENV) $1 --help; false; }

# check shell script
# * try but distrust upstream testsuite
override_dh_auto_test:
	dh_auto_test || true

# * generate manpage based on --help option of script itself
# * generate shell completion scripts
# TODO: run package testsuite during build
# TODO: generate files during build
override_dh_auto_install:
	dh_auto_install
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(_ENV) prove debian/tests/*.t
endif
	$(_ENV) _ONTOSPY_COMPLETE=source $(cmd) \
		> debian/bash-completion || true
	test -s debian/bash-completion
	$(_ENV) _ONTOSPY_COMPLETE=source_zsh $(cmd) \
		> debian/_$(cmd) || true
	test -s debian/_$(cmd)
	$(call _mkman, $(cmd), debian/$(cmd).1, \
		command-line tool to inspect and visualize RDF models)
	find debian/tmp/usr/lib -name '*.pyc' -delete
	find debian/tmp/usr/lib -type d -empty -delete

# unset bogus executable bits
override_dh_fixperms:
	dh_fixperms
	chmod --recursive a-x,a+X -- debian/*/usr/lib

%:
	dh $@ --with python3,bash-completion $(DH_OPTIONS:-O%=%)
