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

set -e
set -x
NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
UPSTREAMNAME=cofoja
COMPRESSION=xz

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.r]\+\) .*${UPSTREAMNAME}.*?\1?"`
    if echo "$VERSION" | grep -q "upstream-version" ; then
        echo "Unable to parse version number"
        exit
    fi
fi

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://${UPSTREAMNAME}.googlecode.com/svn/trunk ${TARDIR}

GZIP="--best --no-name" tar --owner=root --group=root --mode=a+rX --exclude-vcs -caf "$NAME"_"$VERSION".orig.tar.${COMPRESSION} "${TARDIR}"
rm -rf "${TARDIR}"
