#!/bin/sh
# get source for libmtj-java and remove unneeded jars and binaries

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

# unmangle version
UARCHIVEDIR=${NAME}-${VERSION}
UPSTREAMARCHIVE=${UARCHIVEDIR}.zip

mkdir -p ../tarballs
cd ../tarballs
mv ../${UPSTREAMARCHIVE} .
unzip -q ${UPSTREAMARCHIVE}

# Remove all *.jar files
# rm -rf ${UARCHIVEDIR}/lib/*
find ${UARCHIVEDIR} -name "*.jar" -delete
find ${UARCHIVEDIR} -type d -empty -delete

# Build process deletes javadoc first and rebuilds it afterwards
# So it is reasonable to save space in the upstream tarball once
# we are rebuilding it anyway
rm -rf ${UARCHIVEDIR}/javadoc

GZIP="--best --no-name" tar --owner=root --group=root --mode=a+rX -czf "$NAME"_"$VERSION"~dfsg.orig.tar.gz "${UARCHIVEDIR}"
rm -rf ${UARCHIVEDIR}
