#!/bin/bash

P=ebizzy-install
DEFAULT_VERSION=0.3
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
WEB_LOCATION="https://10gbps-io.dl.sourceforge.net/project/ebizzy/ebizzy"
MIRROR_LOCATION="$WEBROOT/ebizzy"

# 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
		;;
	*)
		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=ebizzy-${VERSION}.tar.gz
sources_fetch $WEB_LOCATION/${VERSION}/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $WEB_LOCATION_ALT/${VERSION}/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf ebizzy-${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 ebizzy-$VERSION
pushd ebizzy-$VERSION > /dev/null || die Failed to rename tar

echo 'diff --git a/configure b/configure
index 49f3652..3618780 100755
--- a/configure
+++ b/configure
@@ -31,7 +31,7 @@ cat <<EOF > Makefile
 all: ebizzy

 ebizzy: ebizzy.c
-	gcc -Wall -Wshadow ${LIBS} ${FLAGS} -o ebizzy ebizzy.c
+	gcc -Wall -Wshadow ${FLAGS} -o ebizzy ebizzy.c ${LIBS}

 clean:
 	rm -f ebizzy Makefile
diff --git a/ebizzy.c b/ebizzy.c
index 76c7492..3e7644f 100644
--- a/ebizzy.c
+++ b/ebizzy.c
@@ -83,7 +83,7 @@ static char **hole_mem;
 static unsigned int page_size;
 static time_t start_time;
 static volatile int threads_go;
-static unsigned int records_read;
+static unsigned int *thread_records_read;
 
 static void
 usage(void)
@@ -436,6 +436,7 @@ search_mem(void)
 static void *
 thread_run(void *arg)
 {
+	unsigned int *records = (unsigned int *)arg;
 
 	if (verbose > 1)
 		printf("Thread started\n");
@@ -444,7 +445,7 @@ thread_run(void *arg)
 
 	while (threads_go == 0);
 
-	records_read += search_mem();
+	*records = search_mem();
 
 	if (verbose > 1)
 		printf("Thread finished, %f seconds\n",
@@ -471,12 +472,19 @@ start_threads(void)
 	struct rusage start_ru, end_ru;
 	struct timeval usr_time, sys_time;
 	int err;
+	unsigned int total_records = 0;
 
 	if (verbose)
 		printf("Threads starting\n");
 
+	thread_records_read = calloc(threads, sizeof(unsigned int));
+	if (!thread_records_read) {
+		fprintf(stderr, "Error allocating thread_records_read\n");
+		exit(1);
+	}
+
 	for (i = 0; i < threads; i++) {
-		err = pthread_create(&thread_array[i], NULL, thread_run, NULL);
+		err = pthread_create(&thread_array[i], NULL, thread_run, &thread_records_read[i]);
 		if (err) {
 			fprintf(stderr, "Error creating thread %d\n", i);
 			exit(1);
@@ -505,13 +513,21 @@ start_threads(void)
 			fprintf(stderr, "Error joining thread %d\n", i);
 			exit(1);
 		}
+		total_records += thread_records_read[i];
 	}
 
 	if (verbose)
 		printf("Threads finished\n");
 
-	printf("%u records/s\n",
-	       (unsigned int) (((double) records_read)/elapsed));
+	printf("%u records/s",
+	       (unsigned int) (((double) total_records)/elapsed));
+
+	for (i = 0; i < threads; i++) {
+		printf(" %u", (unsigned int) (((double) thread_records_read[i])/elapsed));
+	}
+	printf("\n");
+
+	free(thread_records_read);
 
 	usr_time = difftimeval(&end_ru.ru_utime, &start_ru.ru_utime);
 	sys_time = difftimeval(&end_ru.ru_stime, &start_ru.ru_stime);' | patch -p1 || exit $SHELLPACK_ERROR

pushd $SHELLPACK_SOURCES/ebizzy-$VERSION || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
export CFLAGS="-O2 $CFLAGS_MMTESTS_EXTRA"
eval ./configure --prefix=$SHELLPACK_SOURCES/-installed 
if [ $? -ne 0 ]; then
	cp /usr/share/automake*/config.guess .
	cp /usr/share/automake*/config.sub .
	eval ./configure --prefix=$SHELLPACK_SOURCES/-installed 
	if [ $? -ne 0 ]; then
		error "$P: configure failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi
fi
unset CFLAGS
make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
mkdir -p ../ebizzy-$VERSION-installed/bin
cp ebizzy ../ebizzy-$VERSION-installed/bin || exit $SHELLPACK_ERROR

exit $SHELLPACK_SUCCESS
#### Description ebizzy
#### Details ebizzy 22
