#!/bin/sh
# script to download and repack source package of maqview
# It was intended to remove a copy of zlib which is not
# a simple straightforeward because of use of internal features
# Thus this script simply removes the .svn dir for the moment

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

if ! echo $@ | grep -q upstream-version ; then
    VERSION=`dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed 's/\([0-9\.]\+\)-[0-9]\+$/\1/'`
else
    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/"`
    if echo "$VERSION" | grep -q "upstream-version" ; then
        echo "Unable to parse version number"
        exit
    fi
fi

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

UTAR="${PKG}-${VERSION}.tar.gz"
mv ../${UTAR} .
tar -xzf ${UTAR}

cd $PKG
# It is not possible to simply remove the zlib code because it was used
# the wrong way using internal implementation features which are changed
# in current zlib in Debian
# rm -rf zutil.[ch] zlib.h zconf.h uncompr.c trees.[ch] inftrees.[ch] inflate.[ch] inffixed.h inffast.[ch] infback.c gzio.c deflate.[ch] crc32.[ch] compress.c adler32.c
# remove useless SVN dir
rm -rf .svn
cd ..

mv ${PKG} ${PKG}-${VERSION}

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