TESTS="no_shared_filesystem no_shared_filesystem_with_std_io multicore_scratch"

function test_no_shared_filesystem() {
read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
EOF

read -r -d '' arc_test_configuration <<'EOF'
[arex]
scratchdir=/local/scratch
shared_filesystem=no
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 11:18:43.152122917 +0100
+++ b/basic-script.sh 2014-01-28 11:18:43.144122917 +0100
@@ -22,22 +22,14 @@
 # Setting environment variables as specified by user
 export 'GRID_GLOBAL_JOBID='
 
-RUNTIME_JOB_DIR=@TEST_SESSION_DIR@/@TEST_JOB_ID@
-RUNTIME_JOB_STDIN="/dev/null"
-RUNTIME_JOB_STDOUT="/dev/null"
-RUNTIME_JOB_STDERR="/dev/null"
-RUNTIME_JOB_DIAG=@TEST_SESSION_DIR@/@TEST_JOB_ID@.diag
-if [ ! -z "$RUNTIME_GRIDAREA_DIR" ] ; then
-  RUNTIME_JOB_DIR=$RUNTIME_GRIDAREA_DIR/`basename $RUNTIME_JOB_DIR`
-  RUNTIME_JOB_STDIN=`echo "$RUNTIME_JOB_STDIN" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_STDOUT=`echo "$RUNTIME_JOB_STDOUT" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_STDERR=`echo "$RUNTIME_JOB_STDERR" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_DIAG=`echo "$RUNTIME_JOB_DIAG" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_CONTROL_DIR=`echo "$RUNTIME_CONTROL_DIR" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-fi
-RUNTIME_LOCAL_SCRATCH_DIR=${RUNTIME_LOCAL_SCRATCH_DIR:-}
+RUNTIME_JOB_DIR=/local/scratch/@TEST_JOB_ID@
+RUNTIME_JOB_STDIN="/dev/null"
+RUNTIME_JOB_STDOUT="/dev/null"
+RUNTIME_JOB_STDERR="/dev/null"
+RUNTIME_JOB_DIAG=/local/scratch/@TEST_JOB_ID@.diag
+RUNTIME_LOCAL_SCRATCH_DIR=${RUNTIME_LOCAL_SCRATCH_DIR:-/local/scratch}
 RUNTIME_FRONTEND_SEES_NODE=${RUNTIME_FRONTEND_SEES_NODE:-}
-RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-yes}
+RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-}
   if [ ! -z "$RUNTIME_LOCAL_SCRATCH_DIR" ] && [ ! -z "$RUNTIME_NODE_SEES_FRONTEND" ]; then
     RUNTIME_NODE_JOB_DIR="$RUNTIME_LOCAL_SCRATCH_DIR"/`basename "$RUNTIME_JOB_DIR"`
     rm -rf "$RUNTIME_NODE_JOB_DIR"
EOF
echo "${job_script_patch}" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_no_shared_filesystem_with_std_io() {
read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (stdin = "in")
 (stdout = "out")
 (stderr = "error")
EOF

read -r -d '' arc_test_configuration <<'EOF'
[arex]
scratchdir=/local/scratch
shared_filesystem=no
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 11:18:43.152122917 +0100
+++ b/basic-script.sh 2014-01-28 11:18:43.144122917 +0100
@@ -22,22 +22,14 @@
 # Setting environment variables as specified by user
 export 'GRID_GLOBAL_JOBID='
 
-RUNTIME_JOB_DIR=@TEST_SESSION_DIR@/@TEST_JOB_ID@
-RUNTIME_JOB_STDIN="/dev/null"
-RUNTIME_JOB_STDOUT="/dev/null"
-RUNTIME_JOB_STDERR="/dev/null"
-RUNTIME_JOB_DIAG=@TEST_SESSION_DIR@/@TEST_JOB_ID@.diag
-if [ ! -z "$RUNTIME_GRIDAREA_DIR" ] ; then
-  RUNTIME_JOB_DIR=$RUNTIME_GRIDAREA_DIR/`basename $RUNTIME_JOB_DIR`
-  RUNTIME_JOB_STDIN=`echo "$RUNTIME_JOB_STDIN" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_STDOUT=`echo "$RUNTIME_JOB_STDOUT" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_STDERR=`echo "$RUNTIME_JOB_STDERR" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_DIAG=`echo "$RUNTIME_JOB_DIAG" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_CONTROL_DIR=`echo "$RUNTIME_CONTROL_DIR" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-fi
-RUNTIME_LOCAL_SCRATCH_DIR=${RUNTIME_LOCAL_SCRATCH_DIR:-}
+RUNTIME_JOB_DIR=/local/scratch/@TEST_JOB_ID@
+RUNTIME_JOB_STDIN="in"
+RUNTIME_JOB_STDOUT="/local/scratch/@TEST_JOB_ID@/out"
+RUNTIME_JOB_STDERR="/local/scratch/@TEST_JOB_ID@/error"
+RUNTIME_JOB_DIAG=/local/scratch/@TEST_JOB_ID@.diag
+RUNTIME_LOCAL_SCRATCH_DIR=${RUNTIME_LOCAL_SCRATCH_DIR:-/local/scratch}
 RUNTIME_FRONTEND_SEES_NODE=${RUNTIME_FRONTEND_SEES_NODE:-}
-RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-yes}
+RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-}
   if [ ! -z "$RUNTIME_LOCAL_SCRATCH_DIR" ] && [ ! -z "$RUNTIME_NODE_SEES_FRONTEND" ]; then
     RUNTIME_NODE_JOB_DIR="$RUNTIME_LOCAL_SCRATCH_DIR"/`basename "$RUNTIME_JOB_DIR"`
     rm -rf "$RUNTIME_NODE_JOB_DIR"
@@ -140,9 +140,9 @@
 fi 
 
 if [ -z "$GNU_TIME" ] ; then
-   "/bin/true" <$RUNTIME_JOB_STDIN 1>$RUNTIME_JOB_STDOUT 2>&1
+   "/bin/true" <$RUNTIME_JOB_STDIN 1>$RUNTIME_JOB_STDOUT 2>$RUNTIME_JOB_STDERR
 else
-  $GNU_TIME -o "$RUNTIME_JOB_DIAG" -a -f 'WallTime=%es\nKernelTime=%Ss\nUserTime=%Us\nCPUUsage=%P\nMaxResidentMemory=%MkB\nAverageResidentMemory=%tkB\nAverageTotalMemory=%KkB\nAverageUnsharedMemory=%DkB\nAverageUnsharedStack=%pkB\nAverageSharedMemory=%XkB\nPageSize=%ZB\nMajorPageFaults=%F\nMinorPageFaults=%R\nSwaps=%W\nForcedSwitches=%c\nWaitSwitches=%w\nInputs=%I\nOutputs=%O\nSocketReceived=%r\nSocketSent=%s\nSignals=%k\n'  "/bin/true" <$RUNTIME_JOB_STDIN 1>$RUNTIME_JOB_STDOUT 2>&1
+  $GNU_TIME -o "$RUNTIME_JOB_DIAG" -a -f 'WallTime=%es\nKernelTime=%Ss\nUserTime=%Us\nCPUUsage=%P\nMaxResidentMemory=%MkB\nAverageResidentMemory=%tkB\nAverageTotalMemory=%KkB\nAverageUnsharedMemory=%DkB\nAverageUnsharedStack=%pkB\nAverageSharedMemory=%XkB\nPageSize=%ZB\nMajorPageFaults=%F\nMinorPageFaults=%R\nSwaps=%W\nForcedSwitches=%c\nWaitSwitches=%w\nInputs=%I\nOutputs=%O\nSocketReceived=%r\nSocketSent=%s\nSignals=%k\n'  "/bin/true" <$RUNTIME_JOB_STDIN 1>$RUNTIME_JOB_STDOUT 2>$RUNTIME_JOB_STDERR
 
 fi
 RESULT=$?
EOF
echo "${job_script_patch}" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_multicore_scratch() {
read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (runtimeenvironment = "MULTICORE_SCRATCH")
 (count = 8)
EOF

read -r -d '' arc_test_configuration <<'EOF'
[arex]
scratchdir="/local/scratch"
shared_filesystem=no
EOF

rtes="MULTICORE_SCRATCH"

read -r -d '' MULTICORE_SCRATCH <<'EOF'
export RUNTIME_ENABLE_MULTICORE_SCRATCH="yes"
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 11:18:43.152122917 +0100
+++ b/basic-script.sh 2014-01-28 11:18:43.144122917 +0100
@@ -7,6 +7,6 @@
 #SBATCH --nice=50
 #SBATCH -J 'gridjob'
 #SBATCH --get-user-env=10L
-#SBATCH -n 1
+#SBATCH -n 8
 #SBATCH 
 
@@ -23,22 +23,14 @@
 # Setting environment variables as specified by user
 export 'GRID_GLOBAL_JOBID=@TEST_JOB_ID@'
 
-RUNTIME_JOB_DIR=@TEST_SESSION_DIR@/@TEST_JOB_ID@
+RUNTIME_JOB_DIR=/local/scratch/@TEST_JOB_ID@
 RUNTIME_JOB_STDIN="/dev/null"
 RUNTIME_JOB_STDOUT="/dev/null"
 RUNTIME_JOB_STDERR="/dev/null"
-RUNTIME_JOB_DIAG=@TEST_SESSION_DIR@/@TEST_JOB_ID@.diag
-if [ ! -z "$RUNTIME_GRIDAREA_DIR" ] ; then
-  RUNTIME_JOB_DIR=$RUNTIME_GRIDAREA_DIR/`basename $RUNTIME_JOB_DIR`
-  RUNTIME_JOB_STDIN=`echo "$RUNTIME_JOB_STDIN" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_STDOUT=`echo "$RUNTIME_JOB_STDOUT" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_STDERR=`echo "$RUNTIME_JOB_STDERR" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_JOB_DIAG=`echo "$RUNTIME_JOB_DIAG" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-  RUNTIME_CONTROL_DIR=`echo "$RUNTIME_CONTROL_DIR" | sed "s#^$RUNTIME_JOB_DIR#$RUNTIME_GRIDAREA_DIR#"`
-fi
-RUNTIME_LOCAL_SCRATCH_DIR=${RUNTIME_LOCAL_SCRATCH_DIR:-}
+RUNTIME_JOB_DIAG=/local/scratch/@TEST_JOB_ID@.diag
+RUNTIME_LOCAL_SCRATCH_DIR=${RUNTIME_LOCAL_SCRATCH_DIR:-/local/scratch}
 RUNTIME_FRONTEND_SEES_NODE=${RUNTIME_FRONTEND_SEES_NODE:-}
-RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-yes}
+RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-}
   if [ ! -z "$RUNTIME_LOCAL_SCRATCH_DIR" ] && [ ! -z "$RUNTIME_NODE_SEES_FRONTEND" ]; then
     RUNTIME_NODE_JOB_DIR="$RUNTIME_LOCAL_SCRATCH_DIR"/`basename "$RUNTIME_JOB_DIR"`
     rm -rf "$RUNTIME_NODE_JOB_DIR"
@@ -99,8 +91,21 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for MULTICORE_SCRATCH:
+RTE_function_0 () {
+export RUNTIME_ENABLE_MULTICORE_SCRATCH="yes"
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}MULTICORE_SCRATCH;"
+# Calling MULTICORE_SCRATCH function:
+RTE_function_0 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime MULTICORE_SCRATCH stage 1 execution failed." 1>&2
+    echo "Runtime MULTICORE_SCRATCH stage 1 execution failed." 1>"${RUNTIME_JOB_DIAG}"
+    exit 1
+fi
+
 echo "runtimeenvironments=$runtimeenvironments" >> "$RUNTIME_JOB_DIAG"
 if [ ! "X$SLURM_NODEFILE" = 'X' ] ; then
   if [ -r "$SLURM_NODEFILE" ] ; then
@@ -123,7 +128,7 @@
   fi
 nodename=`/bin/hostname -f`
 echo "nodename=$nodename" >> "$RUNTIME_JOB_DIAG"
-echo "Processors=1" >> "$RUNTIME_JOB_DIAG"
+echo "Processors=8" >> "$RUNTIME_JOB_DIAG"
 executable='/bin/true'
 # Check if executable exists
 if [ ! -f "$executable" ]; 
@@ -159,6 +164,15 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}MULTICORE_SCRATCH;"
+# Calling MULTICORE_SCRATCH function:
+RTE_function_0 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime MULTICORE_SCRATCH stage 2 execution failed." 1>&2
+    echo "Runtime MULTICORE_SCRATCH stage 2 execution failed." 1>"${RUNTIME_JOB_DIAG}"
+    exit 1
+fi
+
 if [ ! -z  "$RUNTIME_LOCAL_SCRATCH_DIR" ] ; then
   find ./ -type l -exec rm -f "{}" ";"
   chmod -R u+w "./"
EOF
echo "${job_script_patch}" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}
