#!/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 master 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 master) | sed -e 's/^v//' -e 's/-/+git/')

export DH_VERBOSE = 1
export PYBUILD_NAME = heudiconv

# for paranoid
export  http_proxy=http://127.0.0.1:9/

# to please datalad and git
export EMAIL=debian@example.com
export GIT_AUTHOR_NAME="The Name"
export GIT_COMMITTER_NAME=$(GIT_AUTHOR_NAME)

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

override_dh_auto_install:
	dh_auto_install
	mkdir -p build 
	PYTHONPATH=./debian/heudiconv/usr/lib/python2.7/dist-packages \
	help2man -n 'DICOM converter for organizing brain imaging data into structured directory layouts' \
		-N --no-discard-stderr \
        ./debian/heudiconv/usr/bin/heudiconv >| build/heudiconv.1
	find debian -iname *.pyc -delete
	# https://github.com/nipy/heudiconv/issues/399
	rm -rf debian/heudiconv/usr/lib/python*/*-packages/Research
	find debian -name __pycache__ | xargs rm -rf


clean::
	dh_clean
	# Ahhhhhhh!
	rm -rf .pybuild build .pytest_cache/
	-rm -rf heudiconv.egg-info/

# 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
