#!/bin/sh
# Obtaining the source tarball of Denoiser is a bit tricky because here is no
# explicite link to a downloadable tarball on the web page and the source is
# only in SVN

set -e

UNAME=Denoiser
UURL=http://www.microbio.me/denoiser/

PKG=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`

if ! echo $@ | grep -q upstream-version ; then
    DVERSION=`dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed 's/\([0-9\.]\+\)-[0-9]\+$/\1/'`
else
    DVERSION=`echo $@ | sed 's?^.*--upstream-version \([0-9.]\+\) .*download.html.*?\1?'`
    if echo "$DVERSION" | grep -q "upstream-version" ; then
	echo "Unable to parse version number"
        exit
    fi
fi

VERSION=`echo $DVERSION | sed -e 's/\.//g' -e 's/^\([0-9]\)/\1./'`

origDir=../tarballs
if [ -r .svn/deb-layout ]; then
	. .svn/deb-layout
fi

mkdir -p "$origDir"
cd "$origDir"

UDIR="$UNAME"_"$VERSION"
UTAR="$UDIR".tgz
wget "$UURL""$UTAR"

echo "Expecting file '$UTAR' in `pwd`."
tar -xzf "$UTAR"

find "$UDIR"/FlowgramAlignment -name "FlowgramAli_4frame_linux_*" | xargs -r rm

GZIP="--best --no-name" tar -czf "$PKG"_"$DVERSION".orig.tar.gz "$UDIR"
rm -rf "$UDIR"
