#!/bin/sh
# get source for libmpj-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
UVERSION=`echo ${VERSION} | sed 's/\./_/'`
UARCHIVEDIR=${NAME}-v${UVERSION}
UPSTREAMARCHIVE=${UARCHIVEDIR}.zip

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

# Remove unneeded binaries
rm -rf bin/daemon*
rm -rf bin/*.bat
rm -rf bin/*.exe
find . -name "*.dll" -delete
find . -name "*macosx*" -delete
find . -name "*solaris*" -delete

# Unfortunately there is no source for the PDFs
find . -name "*.pdf" -delete

# Remove binaries which are provided by Debian packages anyway
find . -name "libwrapper*" -delete   # libservice-wrapper-jni
rm -f lib/log4j*.jar                 # liblog4j1.2-java
rm -f lib/wrapper.jar                # libservice-wrapper-java

# The test suite contains code with unspecified license
# because it is not used in the build process it will
# be simply removed to enable upload to Debian main.
# Volunteers to do further investigation are really welcome
rm -rf test

# finally all precompiled JARs are either builded in the build process
# or not needed for the build process of mpj.jar which is in our main interest
rm -f lib/*

# After removing the subject of this license file it can be removed as well
rm -f THIRDPARTYLICENSES.txt

# ... and once we are talking about deleting useless stuff
rm -f README-win.txt

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