#!/usr/bin/make -f

srcpkg = fsldata
upstreamver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 | cut -d '-' -f 1,1)
upstreamtarball = fsl-$(upstreamver)-sources.tar.gz
upstreamurl = http://www.fmrib.ox.ac.uk/fsldownloads/$(upstreamtarball)

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

$(upstreamtarball):
	[ -f ../$@ ] || curl -o ../$@ $(upstreamurl)

maint-clean:
	rm -rf data COPYING

maint-update-md5sum:
	md5sum $$(find . -type f -path './data*' -o -name 'COPYING') > debian/origdata.md5sum

maint-check-md5sum:
	# if the check fails it means upstream changed something in the
	# data -- in that case visit upstream webpage, figure out date of
	# change and use it as a new upstream version (simply update Debian
	# changelog)
	md5sum -c debian/origdata.md5sum

maint-populate-srcpkg:
	# extract the pieces we are interested in (data and license information)
	tar --wildcards --strip 1 -xvzf ../$(srcpkg)_$(upstreamver).orig.tar.gz

get-orig-source: $(upstreamtarball)
	# extract the pieces we are interested in (data and license information)
	tar --wildcards --strip 1 -xvzf ../$(upstreamtarball) fsl/data 'fsl/doc/fsl/licence.*'
	# convert the license into plain text file, strip parts that are irrelevant
	# the subset of stuff that we extracted
	html2text doc/fsl/licence.html | head -n73 | tail -n70 > COPYING
	rm -rf doc
	# fix permissions
	-find data -type f -name '*.nii.gz' -exec chmod -x \{\} \;
	-find data -type f -name '*.xml' -exec chmod -x \{\} \;
	# make fake-gzip file actually gzipped
	mv data/atlases/JHU/JHU-ICBM-WhiteMatter-labels-2mm.nii.gz data/atlases/JHU/JHU-ICBM-WhiteMatter-labels-2mm.nii
	gzip data/atlases/JHU/JHU-ICBM-WhiteMatter-labels-2mm.nii
	# check integrity
	md5sum -c debian/origdata.md5sum
	# orig tarball
	tar --exclude='.git*' --exclude=debian \
		-cvzf ../$(srcpkg)_$(upstreamver).orig.tar.gz .
