#!/bin/sh
# get source for libbeagle-java from SVN because there is no source tarball distribution

set -e
NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
UPSTREAMNAME=beagle
UPSTREAMLOC=${UPSTREAMNAME}-lib

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

SVNTAG=`echo ${VERSION} | sed 's/\./_/g'`
rm -f ../${UPSTREAMNAME}_release_${SVNTAG}
RELEASE=${UPSTREAMNAME}_release_${SVNTAG}
TARDIR=${NAME}-${VERSION}

mkdir -p ../tarballs
cd ../tarballs
# svn export conserves time stamps of the files, checkout does not
LC_ALL=C svn export http://${UPSTREAMLOC}.googlecode.com/svn/tags/${RELEASE} ${TARDIR}

rm -f "${TARDIR}"/.gitignore

# Directory contains code for MSC including binary dlls which need to be deleted anyway
# Once we are about to remove something just kick the whole dir ...
rm -rf "${TARDIR}"/project

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