#!/bin/bash
# This script installs the THP memory scaling benchmark
P=thpmemscale-install
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
TARFILE=thp_memscale.tar.gz
WEB_LOCATION="ftp://shell.sgi.com/collect/memscale"
MIRROR_LOCATION="$WEBROOT/thpmemscale/"

# Basic argument parser
while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

# Unconditionally fetch the tar to find out the real version number
TARFILE=$TARFILE
sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
        error "$P: tar xf $TARFILE failed"
        popd > /dev/null
        exit $SHELLPACK_ERROR
fi

# Rename directory to something we expect.
DST_DIR=`tar tf $TARFILE | head -n 1 | awk -F / '{print $1}'`
mv $DST_DIR thpmemscale-${VERSION}
pushd thpmemscale-${VERSION} > /dev/null || die Failed to rename tar


# Patch it if necessary
pushd $SHELLPACK_SOURCES/thpmemscale-${VERSION}/thp_memscale
make -j$NUMCPUS
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
mkdir $SHELLPACK_SOURCES/thpmemscale-${VERSION}-installed
cp thp_memscale $SHELLPACK_SOURCES/thpmemscale-${VERSION}-installed || exit $SHELLPACK_ERROR

echo thp-memscale installed successfully
#### Description THP memory scaling benchmark from SGI
#### Details thpmemscale 6
