#!/bin/sh

# bail out with the first problem
set -e

# Remark: A new uscan that enables easier handling of removing files can be
#         obtained via
#   git clone git://tille@git.debian.org/git/users/tille/devscripts.git
#         and then copy scripts/uscan.pl as uscan at the beginning of your PATH
if uscan --help | grep -q -- --repack-compression ; then
    echo "Use new enhanced uscan"
    uscan --verbose --force-download --repack-compression xz
    exit
fi

# script to download and repack source package of TMalign which strips the binaries from upstream tarball

PKG=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
VERSION=`uscan --verbose --force-download | \
    grep "Newest version on remote site is .* local version is .*" | \
    head -n 1 | \
    sed "s/Newest version on remote site is \([-0-9.]\+\),.*/\1/"`

# mkdir -p does not fail when directories exist already
mkdir -p ../tarballs
cd ../tarballs
TARBALLDIR=`pwd`

UPSTREAMDIR=${PKG}-${VERSION}

mkdir -p ${UPSTREAMDIR}
cd ${UPSTREAMDIR}

tar -xzf ../../TMtools${VERSION}.tar.gz

rm -rf TMscore TMalign
cd ..

GZIP="--best --no-name" tar -czf "$PKG"_"$VERSION".orig.tar.gz "${UPSTREAMDIR}"
rm -rf "${UPSTREAMDIR}"

