#!/usr/bin/make -f

srcpkg = haxby2001
upstreamver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 | cut -d '-' -f 1,1)
url = http://data.pymvpa.org/datasets/haxby2001
subjs = 1 2 3 4 5 6
extension = tar.gz


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

override_dh_auto_clean:
	# create controlfile
	sed -e "s/###ALLSUBJPKGS###/${subjs:%=$(srcpkg)-faceobject-subject%,}/" \
		< debian/control.in > debian/control
	for s in ${subjs}; do \
		sed -e "s/###SUBJ###/$${s}/" \
			< debian/control-subj.template >> debian/control; \
		echo "subj$${s}/* usr/share/data/${srcpkg}-faceobject/subj$${s}" \
			> debian/${srcpkg}-faceobject-subject$${s}.install; \
	done

# obtain the tarball of a particular subject
origtarball-%:
	[ -f ../$* ] || curl -o ../$* $(url)/$*

# obtain other files for main source tarball
origfile-%:
	[ -f $* ] || curl -o $* $(url)/$*

origcomponent-%:
	cd .. && [ -L $(srcpkg)_$(upstreamver).orig-$*.$(extension) ] \
				|| ln -s $*-$(upstreamver).$(extension) \
					$(srcpkg)_$(upstreamver).orig-$*.$(extension)

get-orig-src: ${subjs:%=origtarball-subj%-$(upstreamver).tar.gz} origfile-MD5SUMS origfile-README.rst
	# check integrity
	cd .. && md5sum -c $(CURDIR)/MD5SUMS
	# link to proper component tarball names
	for comp in ${subjs}; do $(MAKE) -f debian/rules origcomponent-subj$${comp}; done
	# main orig tarball
	tar --exclude='subj*' --exclude='.git*' --exclude=debian \
		-cvzf ../$(srcpkg)_$(upstreamver).orig.tar.gz .
