#!/bin/bash
P=dedup-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/davidlohr/ezdedup.git
MIRROR_LOCATION="$WEBROOT/ezdedup/"

install-depends libopenssl-devel

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

pushd $SHELLPACK_SOURCES/dedup-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done

LINESTART=`grep -n "==== BEGIN signed-fix.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END signed-fix.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 signed-fix.patch
fi
echo Extracting $SHELLPACK_TEMP/signed-fix.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/signed-fix.patch
LINESTART=`grep -n "==== BEGIN timeout-fix.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END timeout-fix.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 timeout-fix.patch
fi
echo Extracting $SHELLPACK_TEMP/timeout-fix.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/timeout-fix.patch

for FILE in signed-fix.patch timeout-fix.patch; do
	cat $SHELLPACK_TEMP/${FILE} | patch -p1 || exit $SHELLPACK_FAILURE
done

make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

echo dedup installed successfully
exit $SHELLPACK_SUCCESS

==== BEGIN signed-fix.patch ====
diff --git a/encoder.c b/encoder.c
index 3b86743e8f81..58314d6d3936 100644
--- a/encoder.c
+++ b/encoder.c
@@ -163,7 +163,7 @@ static void print_stats(stats_t *s) {
 
   //determine most suitable unit to use
   for(unit_idx=0; unit_idx<unit_str_size; unit_idx++) {
-    unsigned int unit_div_next = unit_div * 1024;
+    size_t unit_div_next = unit_div * 1024;
 
     if(s->total_input / unit_div_next <= 0) break;
     if(s->total_dedup / unit_div_next <= 0) break;
==== END signed-fix.patch ====

==== BEGIN timeout-fix.patch ====
diff --git a/queue.c b/queue.c
index 823fdc000d05..2d08c1d174b8 100644
--- a/queue.c
+++ b/queue.c
@@ -1,4 +1,5 @@
 #include <assert.h>
+#include <sys/time.h>
 
 #include "util.h"
 #include "queue.h"
@@ -6,6 +7,8 @@
 
 #ifdef ENABLE_PTHREADS
 #include <pthread.h>
+#include <errno.h>
+#include <stdio.h>
 #endif //ENABLE_PTHREADS
 
 void queue_init(queue_t * que, size_t size, int nProducers) {
@@ -86,8 +89,16 @@ int queue_enqueue(queue_t *que, ringbuffer_t *buf, int limit) {
 #ifdef ENABLE_PTHREADS
   pthread_mutex_lock(&que->mutex);
   assert(!queue_isTerminated(que));
-  while (ringbuffer_isFull(&que->buf))
-    pthread_cond_wait(&que->notFull, &que->mutex);
+  while (ringbuffer_isFull(&que->buf)) {
+    struct timeval currentTime;
+    struct timespec timeout;
+
+    gettimeofday(&currentTime, NULL);
+    timeout.tv_sec = currentTime.tv_sec + 10;
+    timeout.tv_nsec = currentTime.tv_usec * 1000UL;
+
+    pthread_cond_timedwait(&que->notFull, &que->mutex, &timeout);
+  }
 #else
   assert(!queue_isTerminated(que));
 #endif
diff --git a/queue.h b/queue.h
index f1c43751f2bf..c5dad71eb2b9 100644
--- a/queue.h
+++ b/queue.h
@@ -42,6 +42,7 @@ typedef struct _queue_t queue_t;
 //Initialize a ring buffer
 static inline int ringbuffer_init(ringbuffer_t *buf, size_t size) {
   //NOTE: We have to allocate one extra element because one element will be unusable (we need to distinguish between full and empty).
+  size *= 2;
   buf->data = (void **)malloc(sizeof(void*) * (size+1));
   buf->size = (size+1);
   buf->head = 0;
==== END timeout-fix.patch ====
#### Description dedup
#### Details dedup 23
