#!/bin/sh
# get source for snappy-java to enable stripping binaries without source

COMPRESSION=xz

set -e
NAME=`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=`echo $@ | sed "s?^.*--upstream-version \([0-9.]\+\) .*${NAME}.*?\1?"`
    if echo "$VERSION" | grep -q "upstream-version" ; then
        echo "Unable to parse version number"
        exit
    fi
fi

mkdir -p ../tarballs
cd ../tarballs
#TARNAME=`echo ${NAME} | sed 's/[.0-9]\+-java/-java/'`-${VERSION}
TARNAME=snappy-java

REVISION=`echo $VERSION | sed 's/\(~dfsg-[0-9\.]\)$//'`
echo "Cloning revision "$REVISION
hg clone -r snappy-java-$REVISION https://code.google.com/p/snappy-java/

# Remove hg dir files
rm -rf ${TARNAME}/.hg*

# Remove native libraries for different OSes which are not needed because we link
# against Debian packaged libraries
rm -rf ${TARNAME}/src/main/resources/org/xerial/snappy/native

# Remove lib/ directory which was also stripped by Charles Plessy before and seems to be unneeded
rm -rf ${TARNAME}/lib

mv ${TARNAME} "${NAME}-${VERSION}"
GZIP="--best --no-name" tar --owner=root --group=root --mode=a+rX -caf "$NAME"_"$REVISION"~dfsg.orig.tar.${COMPRESSION} "${NAME}-${VERSION}"
rm -rf "${NAME}-$VERSION"
