#!/bin/sh -e
# upstream does not provide versioned tarballs
# since we do repack *.Z compressed tarballs anyway some useless
# files are removed via Files-Excluded in debian/copyright

COMPRESS=xz

NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
VERSION=$(dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed 's/\([0-9\.fixdfsg+-]\+\)-[0-9]\+$/\1/')

UPSTREAMTAR="$NAME".tar.Z
MAINVERSION="4.1P"
curdir=`pwd`

mkdir -p ../tarballs
cd ../tarballs

rm -f "$UPSTREAMTAR"
wget ftp://ftp.ncbi.nih.gov/pub/fastlink/$UPSTREAMTAR
tar -xzf "$UPSTREAMTAR"

PATCHVERSION=`grep -v "^ " fastlink/README.updates | tail -n 1 | sed 's/^\([0-9a-z]\+\)\..*/\1/'`
UPSTREAMVERSION="${MAINVERSION}-fix${PATCHVERSION}+dfsg"

if [ "$UPSTREAMVERSION" = "$VERSION" ] ; then
  echo "No new version available"
  exit 1
fi

UPSTREAMDIR="${NAME}-${UPSTREAMVERSION}"

mv "$NAME" "$UPSTREAMDIR"
cd $UPSTREAMDIR
mkdir debian
ln -s $curdir/debian/changelog debian/changelog
ln -s $curdir/debian/copyright debian/copyright
mk-origtargz --repack --compress $COMPRESS --version $UPSTREAMVERSION ../$UPSTREAMTAR

cd ..
rm -rf "$UPSTREAMDIR"
