#!/bin/sh

COMPRESS=xz

set -e

NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
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/beagle-dev/beagle-lib.git
cd beagle-lib
VERSION=${MVERSION}+`date -d @$(git show --format="%at" | head -n1) +%Y%m%d`
# for esthetical reasons set file timestamps (if git-restore-mtime is installed)
git restore-mtime || true
cd ..
TARDIR=${NAME}-${VERSION}
mv beagle-lib ${TARDIR}
rm -rf ${TARDIR}/.git
# remove binaries
find . -name epochtest -type f -delete
find . -name .dirstamp -type f -delete
find . -name .cproject -type f -delete
find . -name .project -type f -delete
find . -name "*.isl" -type f -delete
find . -name "*.isproj" -type f -delete
find . -name "*.props" -type f -delete
find . -name "*.vcxproj*" -type f -delete
find . -name "*.exe" -type f -delete
find . -name "*.bat" -type f -delete
find . -name "*.sln" -type f -delete
find . -name .dirs -type d | xargs rm -rf
find . -type d -empty | xargs rm -rf
rm -rf ${TARDIR}/project

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

cat <<EOT
+-------------------------------------------------------------+
|   Please check                                              |
|      https://github.com/beagle-dev/beagle-lib/releases      |
|   for new official releases before using this Git commit    |
+-------------------------------------------------------------+
EOT
