#!/bin/sh
# script to download and repack source package of velvet
# this is needed to remove a copy of zlib

if uscan --help | grep -q -- --repack-compression ; then
    echo "Use new enhanced uscan"
    uscan --verbose --force-download --repack-compression xz
    exit
fi

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 ../tarballs
cd ../tarballs

UPSTREAMDIR="${PKG}_${VERSION}"
mv ../${UPSTREAMDIR}.tgz .
tar -xzf ${UPSTREAMDIR}.tgz
rm -rf ${UPSTREAMDIR}/third-party

## Delete useless binaries once we are changing the original tarball anyway
# delete precompiled object files
# rm -f ${UPSTREAMDIR}/obj/*.o
find ${UPSTREAMDIR} -name "*.o" -exec rm -f \{\} \;
# delete .DS_Store files
find ${UPSTREAMDIR} -name .DS_Store -exec rm -f \{\} \;
rm -rf ${UPSTREAMDIR}/contrib/MetaVelvet-1.2.01/ISGraph/*.a

## remove separate project which is basically a duplication of velvet code
## this should be packaged separately with an up to date version
#rm -rf ${UPSTREAMDIR}/contrib/MetaVelvet*
#  ... don't do this any more

SUFFIX="+dfsg"
mv ${UPSTREAMDIR} ${UPSTREAMDIR}${SUFFIX}

rm -f ../"$PKG"_"$VERSION".orig.tar.gz
tar -cJf ../"$PKG"_"$VERSION~nozlibcopy".orig.tar.xz "${UPSTREAMDIR}${SUFFIX}"
rm -rf "${UPSTREAMDIR}${SUFFIX}"
