#!/bin/bash
#
#  $Id: makedistribution,v 1.8.30.1 2007/03/25 22:02:49 oliver Exp $
#

# check whether the revision number is given as the first argument
# (git tag!)
if test $# -lt 1 ; then
	echo "makdistribution <git tag> <(optional) directories and files to include into the checkout>"
	exit
else
	TAG=$1
	shift 1
	KEEP_DIRS=$@
fi

BALL_PATH=$(pwd)

# find out if we are in the right directory
if test ! -d source -o ! -d data -o ! -d include -o ! -d doc -o ! -d debian-upstream ; then
	echo "Please call this script in the root of a BALL-checkout (the directory containing source, data, include, ...)!"
	exit
fi

# create a dummy directory and extract the current git version of BALL
echo "extracting paths ${KEEP_DIRS} of BALL revision ${TAG} from git..."

BALL_ARCHIVE=$(mktemp -p${BALL_PATH} ball-archive.XXXXXX) || exit 1

git archive --format=tar --prefix=ball-${TAG}/ ${TAG} ${KEEP_DIRS} 2>/dev/null > ${BALL_ARCHIVE} || exit 1

# determine the version number contained in the checkout
version="$(tar xOf ${BALL_ARCHIVE} ball-${TAG}/include/BALL/COMMON/version.h | grep BALL_RELEASE_STRING | head -1 | awk '{print $3}' | tr -d \")"
echo "BALL checkout has version " $version
FILE=ball-${version}.tar
# mv config/Makefile.dummy .

echo "creating archive ${FILE}..."
mv ${BALL_ARCHIVE} ${FILE}

echo "compressing archive ${FILE}..."
#gzip -9 $FILE
gzip -2 $FILE
echo "Successfully created ${FILE}.gz"
