#!/bin/sh
# script to download and repack source package
# Repackaging is needed because original archive is in zip format
# and moreover gPLINK.jar has to be stripped from the tarball because
# it is a binary without source

BUILDTARBALLDEPENDS="unzip"
missingdepends=`dpkg -l ${BUILDTARBALLDEPENDS} ${BUILDDEPS} | \
    grep -v -e "^ii" -e "^|" -e "^++" -e "^ " -e "^..[^[:space:]]" | \
    sed 's/^[a-z][a-z][[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+.*/\1/'`

if [ "$missingdepends" != "" ] ; then
    echo "Please install the following packages to rebuild the upstream source tarball:"
    echo $missingdepends
    exit -1
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}-src"
unzip ../"$UPSTREAMDIR".zip
rm -f "$UPSTREAMDIR"/gPLINK.jar

GZIP="--best --no-name" tar --owner=root --group=root --mode=a+rX -czf "$PKG"_"$VERSION".orig.tar.gz "$UPSTREAMDIR"
rm -rf "$UPSTREAMDIR"
