#!/usr/bin/make -f

ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
    DH_FLAGS += --with sphinxdoc
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    TEST_FLAGS += --threads=$(NUMJOBS)
endif

# Use the --fast option to allow tests to finish as soon as the output matches
# the expected output. This turns the --wait option into a timeout rather than
# a time that has to be waited on each test.
TEST_FLAGS += --wait=20 --fast

ifndef PYBUILD_AUTOPKGTEST
    PYBUILD_BEFORE_TEST=ln -s {dir}/crazy-complete {build_dir}
else
    PYBUILD_BEFORE_TEST=ln -s /usr/bin/crazy-complete {build_dir}
endif

export PYBUILD_TEST_CUSTOM=1
export PYBUILD_TEST_ARGS=test/run.sh --driver=tmux $(TEST_FLAGS) || (diff -u test/tests/tests.yaml test/tests/tests.new.yaml; exit 1)
export PYBUILD_BEFORE_TEST
export PYBUILD_AFTER_TEST=rm {build_dir}/crazy-complete

%:
	dh $@ --with python3 --with shell_completions --buildsystem=pybuild $(DH_FLAGS)

execute_before_dh_installman:
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	PYTHONPATH=. argparse-manpage \
		--description="Generate auto completion files for all major shells" \
		--author="Benjamin Abendroth" \
		--pyfile=crazy-complete \
		--object=p \
		> crazy-complete.1
endif

execute_before_dh_installdocs:
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	PYTHONPATH=. python3 -m sphinx -N -bhtml docs/ build/html
endif
