#!/bin/bash

# shellpack bound to commit b4d0dd3 ("S benchmark suite 2.5")
P=S-startup-install
DEFAULT_VERSION=b4d0dd3
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
GIT_LOCATION="https://github.com/Algodev-github/S.git"
MIRROR_LOCATION="$WEBROOT/S"

install-depends gawk gnuplot coreutils fio time sysstat gcc-c++
install-depends xterm libreoffice-writer gnome-terminal sysstat

# Done separately as openSUSE requires packman repo for MPlayer
install-depends MPlayer

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
TASKSET_ALL=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

# Unconditionally fetch the tar to find out the real version number
TARFILE=S-startup-${VERSION}.tar.gz
git_fetch $GIT_LOCATION S-startup-${VERSION}-installed $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $VERSION
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf S-startup-${VERSION}.tar.gz 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 S-startup-${VERSION}-installed
pushd S-startup-${VERSION}-installed > /dev/null || die Failed to rename tar

# Specify standard required to build some tools
echo Patching S-startup for C++ standards
sed -i -e 's/g++ -pthread/g++ -std=c++11 -pthread/' comm_startup_lat/comm_startup_lat.sh

# Put kernel devel tree in place that S expects
if [ "$S_STARTUP_REQUIRES_KERNELTREE" = "yes" ]; then
	install-depends git-core
	pushd $SHELLPACK_DATA || exit $SHELLPACK_ERROR
	echo Attempting fetch from $MIRROR_LOCATION/../gitcheckout/linux-2.6.tar.gz
	wget -q $MIRROR_LOCATION/../gitcheckout/linux-2.6.tar.gz
	if [ $? -eq 0 ]; then
		echo Using mirrored tarball
		tar -xf linux-2.6.tar.gz || die "Failed to extract mirrored linux-2.6 tarball"
		DST_DIR=`tar tf linux-2.6.tar.gz | head -n 1 | awk -F / '{print $1}'`
		mv $DST_DIR linux-2.6
	else
		echo Cloning git repository
		git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6 || die "Failed to clone Linus git tree"
	fi
	mv linux-2.6 linux.git-for_kern_dev_benchmarks
	cd linux.git-for_kern_dev_benchmarks

	echo Updating git repository
	git remote update

	echo Checking out v4.15 for a consistent kernel build environment
	git checkout v4.15 || die "Failed to checkout v4.15"
fi
sync

echo S-startup installed successfully
#### Description Application start-up time benchmark
#### Details S-startup 15
