#!/bin/bash

P=parsecbuild-install
DEFAULT_VERSION=0
. $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/cirosantilli/parsec-benchmark"
MIRROR_LOCATION="$WEBROOT/parsec/"

for PACKAGE in gsl-devel libgsl23 libgslcblas0 libjpeg8-devel libjpeg8 xt cmake libXt-devel libXmu-devel libXi-devel; do
	install-depends $PACKAGE
done

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
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
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	*)
		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=parsecbuild-${VERSION}.tar.gz
git_fetch $GIT_LOCATION parsecbuild-${VERSION}-installed $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf parsecbuild-${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 parsecbuild-${VERSION}-installed
pushd parsecbuild-${VERSION}-installed > /dev/null || die Failed to rename tar
cd $SHELLPACK_SOURCES
mv parsecbuild-${VERSION}-installed $SHELLPACK_DATA || die "Failed to move sources to data directory"
ln -s $SHELLPACK_DATA/parsecbuild-${VERSION}-installed parsecbuild-${VERSION}-installed
cd $SHELLPACK_SOURCES/parsecbuild-${VERSION}-installed || die "Failed to cd to sources"

echo Fetching native input data
file_fetch http://parsec.cs.princeton.edu/download/3.0/parsec-3.0-input-native.tar.gz \
	$MIRROR_LOCATION/parsec-3.0-input-native.tar.gz \
	parsec-input.tar.gz
echo Extracting native input data
tar -xf parsec-input.tar.gz --strip-components=1 || die "Failed to download and extract parsec-input.tar.gz"

# Build fixes
for FILE in `find -name "*.cpp"`; do
	sed -i -e 's/"__PARSEC_XSTRING/" __PARSEC_XSTRING/' $FILE
done

if [ "`which man`" != "" ]; then
	man g++ 2>&1 | grep -q no-pie
	if [ $? -ne 0 ]; then
		sed -i -e 's/-no-pie//g' ./pkgs/apps/x264/parsec/gcc-pthreads.bldconf
	fi
fi

LINESTART=`grep -n "==== BEGIN x264.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END x264.patch" $0 | tail -1 | awk -F : '{print $1}'`
if [ "$LINEEND" = "" ]; then
	LINECOUNT=`wc -l $0 | awk '{print $1}'`
fi
if [ "$LINESTART" = "" ]; then
	die Failed to find start of file x264.patch
fi
echo Extracting $SHELLPACK_TEMP/x264.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/x264.patch
cat $SHELLPACK_TEMP/x264.patch | patch -p1 || exit $SHELLPACK_FAILURE

echo parsecbuild preliminary download complete
exit $SHELLPACK_SUCCESS

==== BEGIN x264.patch ====
diff --git a/pkgs/apps/x264/src/common/set.c b/pkgs/apps/x264/src/common/set.c
index 6c7ddc468ff2..795e7d528bf2 100644
--- a/pkgs/apps/x264/src/common/set.c
+++ b/pkgs/apps/x264/src/common/set.c
@@ -176,7 +176,7 @@
 void x264_cqm_delete( x264_t *h )
 {
     int i, j;
-    for( i = 0; i < 6; i++ )
+    for( i = 0; i < 5; i++ )
     {
         for( j = 0; j < i; j++ )
             if( h->quant4_mf[i] == h->quant4_mf[j] )
==== END x264.patch ====
#### Description parsecbuild
#### Details parsecbuild 9
