#!/usr/bin/make -f

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

# resolve if release is experimental
EXP_RELEASE = $(filter experimental% UNRELEASED,$(DEB_DISTRIBUTION))

destdir = debian/tmp
cmd = ontospy
python3path = $(destdir)/usr/lib/$(shell py3versions -d)/dist-packages
python3path_build = .pybuild/cpython3_$(shell py3versions -dv)/build
path_build = build/scripts-$(shell py3versions -dv):$(PATH)

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

# generate shell completion scripts
override_dh_auto_build:
	dh_auto_build
	PATH="$(path_build)" PYTHONPATH="$(python3path_build)" \
	  _ONTOSPY_COMPLETE=source $(cmd) \
		> debian/bash-completion || true
	PATH="$(path_build)" PYTHONPATH="$(python3path_build)" \
	  _ONTOSPY_COMPLETE=source_zsh $(cmd) \
		> debian/_$(cmd) || true

# check shell script
override_dh_auto_test:
	dh_auto_test $(if $(EXP_RELEASE), || true)
	shellcheck --shell bash --exclude=SC2086,SC2206,SC2207,SC2235 \
		debian/bash-completion

# generate manpage based on --help option of script itself
override_dh_auto_install:
	dh_auto_install
	$(call _mkman, $(destdir)/usr/bin/$(cmd), debian/$(cmd).1, \
		command-line tool to inspect and visualize RDF models)
	find $(destdir)/usr/lib -name '*.pyc' -delete
	find $(destdir)/usr/lib -type d -empty -delete

%:
	dh $@ --with python3,bash-completion --buildsystem=pybuild

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