#!/usr/bin/make -f

# resolve DEB_VERSION_UPSTREAM
include /usr/share/dpkg/pkg-info.mk

# use local fork of dh-cargo and cargo wrapper
PATH := $(CURDIR)/debian/dh-cargo/bin:$(PATH)
PERL5LIB = $(CURDIR)/debian/dh-cargo/lib
export PATH PERL5LIB

# requires env variables HYPOTHESIS_NAME HYPOTHESIS_KEY TEST_GROUP_ID
TEST_ACCOUNT = \
 it_works \
 make \
 sync \
 tag_filter

_help2man = \
 help2man --section 1 --no-info \
 --version-string='$(DEB_VERSION_UPSTREAM)' \
 --name $(shell $(1) $(2) --help | head --lines=1 | xargs -0 shell-quote --) \
 --output 'debian/build/hypothesis-client$(if $2,-$(subst $() ,-,$(strip $(2)))).1' \
 '$(1)$(if $(2), $(2))' \
 || { $(1) $(2) --help; false; }

# set dummy API values to cheat too invasive validation
export HYPOTHESIS_NAME = foo
export HYPOTHESIS_KEY  = bar

%:
	dh $@ --buildsystem cargo

# TODO: check tests when figuring out how to set dotfile
override_dh_auto_test:
#	dh_auto_test -- --no-fail-fast -- \
#	 --test-threads=1 $(addprefix --skip ,$(TEST_ACCOUNT))

# generate shell completion scripts and manual pages
execute_after_dh_auto_install: debian/hypothesis-client/usr/bin/hypothesis-client
	mkdir --parents debian/build
	$< complete bash > debian/build/bash-completion
	$< complete zsh > debian/build/_hypothesis-client
	$(call _help2man,$<)
	$(call _help2man,$<,annotations)
	$(foreach x,create update search fetch delete flag hide show,\
	 $(call _help2man,$<,annotations $x);)
	$(call _help2man,$<,groups)
	$(foreach x,list create fetch update members leave,\
	 $(call _help2man,$<,groups $x);)
	$(call _help2man,$<,profile)
