#!/usr/bin/make -f
#DH_VERBOSE = 1

export PYBUILD_NAME = ofxstatement-plugins

%:
	dh $@ --with python3,quilt

PLUGINS=$(wildcard ofxstatement-*) $(wildcard banking.statements.*)

plugin_clean_% : %
	cd $< && python3 setup.py clean
	rm -f $</$<.README.rst

plugin_build_% : %
	cd $< && python3 setup.py build
	test ! -e $</README.rst || cp $</README.rst $</$<.README.rst

plugin_install_% : %
	cd $< && python3 setup.py install \
		--force --root=$(CURDIR)/debian/ofxstatement-plugins \
		--no-compile -O0 --install-layout=deb
	test ! -e $</$<.README.rst || dh_installdocs $</$<.README.rst
	# Don't install tests
	find $(CURDIR)/debian/ofxstatement-plugins -type d -name 'tests' -print0 | \
		xargs -0r rm -rf
	# Don't install nspkg.pth
	find $(CURDIR)/debian/ofxstatement-plugins -name '*-nspkg.pth' -print0 | \
		xargs -0r rm -rf

plugin_test_% : %
	cd $< && python3 setup.py test
	# run pytest with doctest and ignore exit code 5 (no tests found)
	cd $< && PYTEST_ADDOPTS=--doctest-modules python3 setup.py pytest; \
		PYTEST_RESULT=$$?; \
		test $$PYTEST_RESULT -eq 5 && exit 0; \
		exit $$PYTEST_RESULT


override_dh_auto_clean: $(addprefix plugin_clean_,$(PLUGINS))
	rm -rf ofxstatement-postfinance/build \
		ofxstatement-postfinance/src/ofxstatement_postfinance.egg-info
override_dh_auto_build: $(addprefix plugin_build_,$(PLUGINS))
override_dh_auto_install: $(addprefix plugin_install_,$(PLUGINS))

ifdef RUN_AUTOTESTS
override_dh_auto_test: $(addprefix plugin_test_,$(PLUGINS))
endif

sync:
	git submodule update --init
	git submodule foreach git fetch
	git submodule foreach git rebase origin/master
	git commit ofxstatement-* banking.* -s -m "Update submodules" -m "$$(git submodule summary)"