#!/bin/sh
# get source for SAINT because upstream archive is bloated by macosx specific
# stuff which breaks dpkg-buildpackage
# -> http://lists.debian.org/debian-qa/2012/04/msg00057.html

set -e
NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
UCNAME=`echo $NAME | tr [a-z] [A-Z]`

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

mkdir -p ../tarballs
cd ../tarballs
upstream="${UCNAME}_v${VERSION}"
unzip -q ../${upstream}.zip
rm -rf __MACOSX

# once we are changing the tarball anyway we could fix some other nasty things
# this will be suggested upstream for the future
find ${upstream} -name .DS_Store -delete
find ${upstream} -name "*~" -type f -delete
find ${upstream} -name "*.c" -type f -exec chmod a-x \{\} \;
find ${upstream} -name "*.h" -type f -exec chmod a-x \{\} \;

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