#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

export PYBUILD_AFTER_BUILD=chmod +x {build_dir}/beancount/tools/treeify.py

# Failing tests, need to be solved later
export PYBUILD_TEST_ARGS=-v -k "not test_export_basic"
export PYBUILD_BEFORE_TEST=cp -r $(CURDIR)/beancount {build_dir}/beancount;
export PYBUILD_AFTER_TEST=:;

%:
	dh $@ --buildsystem pybuild

override_dh_python3:
	dh_python3 --recommends bottle --suggests google-api-python-client --suggests beautifulsoup4 --suggests requests

# Skip generation of specific tools for now, need to be solved later
execute_after_dh_auto_build:
ifeq ($(filter nodoc,$(DEB_BUILD_PROFILES)),)
	cd $(CURDIR)/bin; \
	  for binary in *; do \
	    if [ "X$$binary" = "XBUILD" ]; then continue; fi ; \
	    if [ "X$$binary" = "Xbean-check" ]; then continue; fi ; \
	    if [ "X$$binary" = "Xbean-doctor" ]; then continue; fi ; \
	    if [ "X$$binary" = "Xbean-example" ]; then continue; fi ; \
	    if [ "X$$binary" = "Xbean-format" ]; then continue; fi ; \
	    PYTHONPATH=$$(cd $(CURDIR); pybuild --print build_dir --interpreter python3) \
	      help2man \
	        --name "$$(grep $$binary $(CURDIR)/debian/manpages/whatis.txt | cut -f 2)" \
	        --no-info \
	        --help-option=-h \
	        --version-string=$(DEB_VERSION_UPSTREAM) \
	        ./$$binary > $(CURDIR)/debian/manpages/$$binary.1 ; \
	  done
endif

execute_after_dh_auto_clean:
	rm -f debian/manpages/*.1

override_dh_installexamples:
	dh_installexamples -X __pycache__ -X .picklecache

execute_after_dh_fixperms:
	find debian/*/usr/lib/python*/*-packages/beancount/* -type f -name '*.py' -exec chmod -x {} +
