#!/bin/sh -e

COMPRESS=xz

NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
DVERSION=`dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed -e 's/^[0-9]*://' -e 's/-.*//'`
MVERSION=`dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed 's/^\([0-9\.]\+\)[+~][-0-9]\+$/\1/'`

mkdir -p ../tarballs
cd ../tarballs
# need to clean up the tarballs dir first because upstream tarball might
# contain a directory with unpredictable name
rm -rf *
git clone --quiet https://github.com/xflouris/libpll
cd $NAME
VERSION=${MVERSION}+`date -d @$(git show --format="%at" | head -n1) +%Y%m%d`

if [ "$DVERSION" = "$VERSION" ] ; then
  echo "No newer upstream version than $DVERSION available."
  cd ..
  rm -rf ${NAME}
  exit
fi

# for esthetical reasons set file timestamps (if git-restore-mtime is installed)
git restore-mtime 2>/dev/null || true
cd ..
TARDIR=${NAME}-${VERSION}
mv ${NAME} ${TARDIR}
rm -rf ${TARDIR}/.git
rm -f ${TARDIR}/jars/*
rm -rf ${TARDIR}/m4

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