#!/bin/sh
set -e

git clone https://github.com/neurolabusc/dcm_qa_asl data
find data/ -name '.git*' | xargs rm -rf
cd data

if ! grep -q 'BSD 2-Clause License' LICENSE
then
	cat >&2 <<-END
	error: test data's licensing terms look to have changed.
	       Please examine the situation before going further.
	END
	exit 1
fi

rm -rf $(ls -A | grep -vE "(In)")
cd In/PASL2D/

readonly CHANGELOG="../../../changelog"
if [ ! -r "$CHANGELOG" ]
then
	cat >&2 <<-END
	error: debian/changelog not found.
	       Please run from the debian/ directory.
	END
fi

tarball="$( \
	dpkg-parsechangelog --file "$CHANGELOG" \
	| awk '/^Source:/ {print $2}' \
)_$( \
	dpkg-parsechangelog --file "$CHANGELOG" \
	| awk '/^Version:/ {print $2}' \
	| cut -f1 -d- \
).orig-debian-tests-data.tar.gz"

tar --owner=root --group=root --mode=a+rX -cvzSf "$tarball" "3_pasl_2d"

mv "$tarball" ../../..
cd ../../..
test -r "$tarball"
rm -rf data/
echo "Now move $tarball next to the new upstream tarball and gbp import it as usual"
