#!/bin/bash
P=cmattach-test-install
DEFAULT_VERSION=20110718
. $SHELLPACK_INCLUDE/common.sh
WEB_LOCATION=http://ozlabs.org/~cyeoh/cma
MIRROR_LOCATION="$WEBROOT/cmattach/"

# Basic argument parser
while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	*)
		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=cma-test-${VERSION}.tgz
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 cma-test-${VERSION}.tgz 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 cmattach-test-${VERSION}-installed
pushd cmattach-test-${VERSION}-installed > /dev/null || die Failed to rename tar

# Build
pushd $SHELLPACK_SOURCES/cmattach-test-${VERSION}-installed || die Failed to change to source directory
echo "diff --git a/process_vm.h b/process_vm.h
index bc69e45..d80a245 100755
--- a/process_vm.h
+++ b/process_vm.h
@@ -7,10 +7,13 @@
 #include <sys/syscall.h>
 #include <sys/types.h>
 
-#if defined(i386)
+#if defined(__i386__)
 #define __NR_process_vm_readv 347
 #define __NR_process_vm_writev 348
 
+#elif defined(__x86_64__)
+#define __NR_process_vm_readv 310
+#define __NR_process_vm_writev 311
 
 #elif defined(_ARCH_PPC)
 #define __NR_process_vm_readv 351
diff --git a/setup_process_vm_readv_iovec.c b/setup_process_vm_readv_iovec.c
index 162934f..ec7d1ed 100644
--- a/setup_process_vm_readv_iovec.c
+++ b/setup_process_vm_readv_iovec.c
@@ -82,6 +82,7 @@ int main(int argc, char *argv[])
     printf(\" %p %i\", foo[i], buf_sizes[i]);
   }
   printf(\"\\n\");
+  fflush(NULL);
 
 
 
diff --git a/setup_process_vm_readv_simple.c b/setup_process_vm_readv_simple.c
index 1de1422..b16a331 100644
--- a/setup_process_vm_readv_simple.c
+++ b/setup_process_vm_readv_simple.c
@@ -21,6 +21,7 @@ int main(int argc, char *argv[])
   strcpy(foo, tst_string);
 
   printf(\"Target process is setup\\nRun the following to test:\\n./t_process_vm_readv_simple %i %p %i\\n\", getpid(), foo, strlen(foo));
+  fflush(NULL);
   while (1)
     {
       sleep(100);
diff --git a/setup_process_vm_writev.c b/setup_process_vm_writev.c
index 408e373..16b8e47 100644
--- a/setup_process_vm_writev.c
+++ b/setup_process_vm_writev.c
@@ -42,6 +42,7 @@ int main(int argc, char *argv[])
 
   printf(\"Target process is setup\\nRun the following to test:\\n./t_process_vm_writev \");
   printf(\"%i %p %i\\n\", getpid(), foo, buffer_size);
+  fflush(NULL);
   while (foo[buffer_size-1]==DEFAULT_CHAR)
     {
       sleep(1);
" | patch -p1 || die Failed to patch cma-test

make -j$NUMCPUS
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
#### Description This is a basic functional test of cross-memory-attach
#### Details cmattach-test 2
