#!/bin/bash
# A shellpack consists of an install script, a benchmark script, a version, a
# name and a description.
#
DIRNAME=`dirname $0`
export SCRIPTDIR=`cd "$DIRNAME" && pwd`
SRC=.
INSTALL=install
BENCH=bench
OUTPUT=$SRC/testpack.tar.gz
VERSION=
NAME=
TEMP=

usage() {
	echo "build-shellpack"
	echo
	echo "   -s <directory>     Location of install and bench files"
	echo "   -i <install-file>  Install script name (default: install)"
	echo "   -b <bench-file>    Benchmark script name (default: bench)"
	echo "   -n <name>          Pack name"
	echo "   -d \"<description>\" Short description of the pack"
	echo "   -o <filename>      Output pack name (default: testpack.tar.gz)"
	echo "   -h, --help         Print this help message"
	exit 1
}

die() {
	echo ERROR: $@
	if [ "$TEMP" != "" ]; then
		cd /
	fi
	exit -1
}

# Parse command line args
ARGS=`getopt -o hs:i:b:n:d:o:h --long help -n build-shellpack -- "$@"`
eval set -- "$ARGS"
while true; do
	case "$1" in 
	-s)
		SRC="$2"
		shift 2
		;;
	-i)
		INSTALL="$2"
		shift 2
		;;
	-b)
		BENCH="$2"
		shift 2
		;;
	-n)
		NAME="$2"
		shift 2
		;;
	-d)
		DESCRIPTION="$2"
		shift 2
		;;
	-v)
		VERSION="$2"
		shift 2
		;;
	-o)
		OUTPUT="$2"
		shift 2
		;;
	-h|--help)
		usage
		;;
	--)
		break
		;;
	*)
		echo "Unrecognised arg: $1"
		usage
		;;
	esac
done

# Check source directory
if [ "$SRC" = "" -o ! -d "$SRC" ]; then
	echo Invalid source directory specified
	exit -1
fi
pushd "$SRC" > /dev/null
SRC=`pwd`
popd > /dev/null
echo Source directory: $SRC

# Check script names
INSTALL="$SRC/$INSTALL"
BENCH="$SRC/$BENCH"
if [ ! -f "$INSTALL" ]; then
	die "Install script $INSTALL does not exist. Use -i"
fi
if [ ! -f "$BENCH" ]; then
	die "Benchmark script $BENCH does not exist. Use -b"
fi

# Get the version number and update
if [ "$VERSION" != "" ]; then
	echo Using specified version: $VERSION
else
	if [ -f "$SRC/version" ]; then
		VERSION=`cat "$SRC/version"`
		VERSION=$(($VERSION+1))
		if [ "$VERSION" = "" ]; then
			die Unable to read version file or version file corrupt
		fi
		echo Updating version number: $VERSION
	else
		VERSION=1
		echo Starting at version: $VERSION
	fi
fi

if [ "$NAME" = "" ]; then
	if [ -f "$SRC/name" ]; then
		NAME=`cat "$SRC/name"`
		echo Name: $NAME
	else
		die Specify a name with -n
	fi
fi

# Get the description of the benchmark
if [ "$DESCRIPTION" = "" ]; then
	if [ -f "$SRC/description" ]; then
		DESCRIPTION=`cat "$SRC/description"`
		echo Description: $DESCRIPTION
	else
		die Specify a description with -n
	 fi
fi

##### Start building the pack
TEMP=`mktemp 2> /dev/null`
if [ "$TEMP" = "" ]; then
	TEMP=`mktemp -t testpack.XXXXXX 2> /dev/null`
	if [ "$TEMP" = "" ]; then
		die Cannot figure out how to use mktemp
	fi
fi
mkdir $TEMP.dir
rm $TEMP
TEMP=$TEMP.dir

echo "$VERSION" > "$SRC/version"
echo "$NAME" > "$SRC/name"
echo "$DESCRIPTION" > "$SRC/description"

pushd $TEMP > /dev/null
cat "$BENCH" | $SCRIPTDIR/rewrite-shellpack > ./bench
cat "$INSTALL" | $SCRIPTDIR/rewrite-shellpack > ./install
cp "$SRC/version" .
cp "$SRC/name" .
cp "$SRC/description" .
echo "#### Description $DESCRIPTION" >> ./bench
echo "#### Description $DESCRIPTION" >> ./install
echo "#### Details `basename $BENCH | sed -e 's/ /_/g'` $VERSION" >> ./bench
echo "#### Details `basename $NAME	| sed -e 's/ /_/g'` $VERSION" >> ./install

echo Creating pack
tar -cz --exclude testpack.tar.gz -f testpack.tar.gz .
popd > /dev/null
mv $TEMP/testpack.tar.gz "$OUTPUT"
if [ ! -f "$OUTPUT" ]; then
	die Failed to create testpack
fi

echo Pack: $OUTPUT
cd /
rm -rf $TEMP
exit 0
