#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

BINDIR=$(CURDIR)/debian/tmp/usr/bin/

# Setting MPLCONFIGDIR to please matplotlib demanding writable HOME
# on older systems (e.g. 12.04)
export MPLCONFIGDIR=$(CURDIR)/build

%:
	dh $@ --buildsystem=pybuild --builddirectory=build --with=python3

override_dh_auto_build:
	dh_auto_build
	PYTHONPATH=$(CURDIR) $(MAKE) -C doc html SPHINXBUILD=/usr/share/sphinx/scripts/python3/sphinx-build PYTHON=python3
	# but remove jquery copy (later on link to Debian's version)
	-rm build/html/_static/jquery.js
	# objects inventory is of no use for the package
	-rm build/html/objects.inv
	# also doc source files only consume space
	-rm -r build/html/_sources

# enable when we believe that the tests should pass
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	PYBUILD_SYSTEM=custom PYBUILD_TEST_ARGS="PYTHONPATH={build_dir} LC_ALL=C.UTF-8 {interpreter} -m pytest --verbose" dh_auto_test
endif

## immediately useable documentation
## and exemplar data (they are small excerpts anyway)
override_dh_compress:
	dh_compress -X.py -X.html -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -X.par -X.bin

override_dh_installman:
	PYTHONPATH=build/lib:$(PYTHONPATH) help2man -N \
		-n 'convert PARREC image to NIfTI' $(BINDIR)/parrec2nii > build/parrec2nii.1
	PYTHONPATH=build/lib:$(PYTHONPATH) help2man -N \
		-n 'FUSE filesystem on top of a directory with DICOMs' $(BINDIR)/nib-dicomfs > build/nib-dicomfs.1
	PYTHONPATH=build/lib:$(PYTHONPATH) help2man -N \
		-n "'ls' for neuroimaging files" $(BINDIR)/nib-ls > build/nib-ls.1
	PYTHONPATH=build/lib:$(PYTHONPATH) help2man -N \
		-n "header diagnostic tool" $(BINDIR)/nib-nifti-dx > build/nib-nifti-dx.1

	dh_installman build/*.1

override_dh_fixperms:
	find debian/ -name 'gzipbase64.gii' | xargs chmod -x
	find debian/ -name 'umass_anonymized.PAR' | xargs chmod -x
	dh_fixperms

override_dh_clean:
	$(MAKE) clean
	dh_clean
