#!/usr/bin/make -f

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
upstreamver = $(shell echo $(debver) | cut -d '-' -f 1,1 )

# this figures out the last merge point from 'master' into the Debian branch and
# then described this commit relative to the last release tag (V...)
# If this should make any sense the local upstream branch must track upstream's
# master or whatever other source branch.
gitver = $(shell [ -x /usr/bin/git ] && git describe --tags --match 'v[0-9].*' $$(git merge-base -a HEAD upstream) | sed -e 's/^v//' -e 's/-/+git/').1

export DH_VERBOSE = 1
export PYBUILD_NAME = fsl
# disable tests for now, needs X, needs non-free data
# tests are not PY3 compatible
export PYBUILD_DISABLE = test/python3 test/python2
# atlas tests need non-free data
export PYBUILD_TEST_ARGS = --niter 1 --ignore tests/test_image.py --ignore tests/test_atlases

# one ring to rule them all ...
%:
	dh $@ --with python2,python3 --buildsystem=pybuild

clean::
	dh_clean
	# Ahhhhhhh!
	rm -rf .pybuild build
	-rm -rf fslpy.egg-info/
	-rm -rf assets
	-rm -rf .eggs/
	-rm -rf tests/__pycache__
	-find . -name '*.pyc' -delete

override_dh_install:
	dh_install

override_dh_auto_test:
	# all tests disabled, need external software, data and X

# make orig tarball from repository content
get-orig-source:
	# orig tarball, turn directory into something nicer
#	git archive --format=tar --prefix=$(srcpkg)-$(gitver)/ HEAD | \
#		gzip -9 > $(srcpkg)_$(gitver).orig.tar.gz
	git archive --format=tar --prefix=$(srcpkg)-$(upstreamver)/ HEAD | \
		gzip -9 > $(srcpkg)_$(upstreamver).orig.tar.gz

# check that DSC patches still apply
maint-check-dsc-patches:
	@for p in debian/patches/*-dsc-patch; \
		do echo "check $$p"; \
		patch -p1 --dry-run < $$p || exit 1 ; \
	done

