#!/bin/bash
# A shellpack consists of an install script, a benchmark script, a
# name and a description.
#
DIRNAME=`dirname $0`
export SCRIPTDIR=`cd "$DIRNAME" && pwd`
SRC=.
INSTALL=install
BENCH=bench
OUTPUT=$SRC/testpack.tar.gz
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
		;;
	-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

# 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

if [ ! -f "$SRC/description" ]; then
	die "Specify a description with -d"
fi
DESCRIPTION=`cat "$SRC/description"`

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

# Check required hooks are available
HOOK_AVAILABLE=0
for HOOK in init_complete init_only_end; do
	grep -q $HOOK $BENCH
	if [ $? -eq 0 ]; then
		HOOK_AVAILABLE=1
	fi
done
if [ $HOOK_AVAILABLE -eq 0 ]; then
	echo SHELLPACK hook init_complete or init_only_end is required
	exit -1
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 "$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/name" .
cp "$SRC/description" .
echo "#### Description $DESCRIPTION" >> ./bench
echo "#### Description $DESCRIPTION" >> ./install
echo "#### Details `basename $BENCH | sed -e 's/ /_/g'`" >> ./bench
echo "#### Details `basename $NAME	| sed -e 's/ /_/g'`" >> ./install

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

cd /
rm -rf $TEMP
exit 0
