#!/usr/bin/make -f

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
upstreamver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 | cut -d '-' -f 1,1)
# upstream uses non-standard, not numerical comperable versioning
mangleduver = $(shell echo $(upstreamver) | sed -e 's/~.*//' -e 's/\./X/' -e 's/\.//g' -e 's/X/\./')
upstreamtarball = caret_distribution_Linux64.v$(mangleduver).zip
upstreamurl = http://brainmap.wustl.edu/pub/caret/$(upstreamtarball)

# one ring to rule them all ...
%:
	dh $@

#
# everything below are maintainer targets for source package handling/processing
#
$(upstreamtarball):
	[ -f ../$@ ] || curl -o ../$@ $(upstreamurl)

maint-clean:
	rm -rf caret

maint-update-md5sum:
	find . -type f ! -wholename './debian*' ! -wholename './.git*' -print0 |\
		xargs -0 md5sum > debian/origdata.md5sum

maint-check-md5sum:
	# if the check fails it means upstream changed something in the
	# zip file -- in that case check the package, figure out what changed,
	# update the version and use it as a new upstream version (simply update
	# Debian changelog)
	md5sum -c debian/origdata.md5sum

maint-populate-srcpkg:
	tar --wildcards --strip 1 -xvjf ../$(srcpkg)_$(upstreamver).orig.tar.bz2

get-orig-source: $(upstreamtarball)
	# extract orig archiv and strip trash
	unzip ../$(upstreamtarball) -x '*Thumbs.db' '*.DS_Store' '*._*' \
		'*/bin_linux64/*' '*~' -d .
	# fix permissions
	find caret -type f -exec chmod -x \{\} \;
	# make orig tarball
	tar --exclude='.git*' --exclude=debian --transform "s,^./,$(srcpkg)-$(upstreamver)/,"\
            -cjf ../$(srcpkg)_$(upstreamver).orig.tar.bz2 .
	# check integrity to notify a version change
	md5sum -c debian/origdata.md5sum
