#!/bin/sh
# Get and extract upstream tarball. Uses already downloaded tarball
# if it is detected in $DEBTARBALLS.

set -e

# make sane defaults
tarball_dir=$DEBTARBALLS
if [ -z "$tarball_dir" ]; then
	tarball_dir=".."
fi

tarball="fsl-${1}-feeds.tar.gz"
base_url="http://www.fmrib.ox.ac.uk/fsldownloads"
lcl_tarball="${tarball_dir}/${tarball}"
upstream_tarball="${base_url}/${tarball}"

# is local copy available or does it have to be downloaded first
if [ -e "$lcl_tarball" ]; then
	echo "Using local tarball copy from $lcl_tarball"
else
	echo "Trying to download upstream tarball from $upstream_tarball"
	wget -O ${lcl_tarball} ${upstream_tarball}
fi

# extract into the source package
tar xvzf $lcl_tarball
#mv feeds/* .
#rm -rf feeds

# apply patches
if [ -d debpatches ]; then
	for p in debpatches/*; do
		patch -p1 < $p
	done
fi
