TESTS="rte0_change_memory rte0_nodeproperty rte0_env rte0_runtime rte0_arg rte rte_with_options"

# NOTE: Backslashes in environment variables is not handled consistent in bash and dash (RedHat vs Debian).
# TEST DISABLED!
#TESTS="${TESTS} rte0_env_backslashes" # Test passes with bash but not in dash.

function test_rte0_change_memory() {
rtes="INCREASEMEMORY"

read -r -d '' INCREASEMEMORY <<'EOF'
joboption_memory=2000
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (memory=500)
 (runtimeenvironment = "INCREASEMEMORY")
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -9,6 +9,7 @@
 #SBATCH --get-user-env=10L
 #SBATCH -n 1
 #SBATCH 
+#SBATCH --mem-per-cpu=2000
 
 # Overide umask of execution node (sometime values are really strange)
 umask 077
@@ -99,8 +99,21 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for INCREASEMEMORY:
+RTE_function_0 () {
+joboption_memory=2000
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}INCREASEMEMORY;"
+# Calling INCREASEMEMORY function:
+RTE_function_0 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime INCREASEMEMORY stage 1 execution failed." 1>&2
+    echo "Runtime INCREASEMEMORY 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
@@ -159,6 +172,15 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}INCREASEMEMORY;"
+# Calling INCREASEMEMORY function:
+RTE_function_0 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime INCREASEMEMORY stage 2 execution failed." 1>&2
+    echo "Runtime INCREASEMEMORY 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
}

function test_rte0_nodeproperty() {
rtes="NODEPROPERTY"

read -r -d '' NODEPROPERTY <<'EOF'
export joboption_nodeproperty_0="TEST"
export joboption_nodeproperty_1="TESTING"
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (runtimeenvironment = "NODEPROPERTY")
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -8,6 +8,6 @@
 #SBATCH -J 'gridjob'
 #SBATCH --get-user-env=10L
 #SBATCH -n 1
-#SBATCH 
+#SBATCH  TEST TESTING
 
 # Overide umask of execution node (sometime values are really strange)
@@ -99,8 +99,22 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for NODEPROPERTY:
+RTE_function_0 () {
+export joboption_nodeproperty_0="TEST"
+export joboption_nodeproperty_1="TESTING"
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}NODEPROPERTY;"
+# Calling NODEPROPERTY function:
+RTE_function_0 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime NODEPROPERTY stage 1 execution failed." 1>&2
+    echo "Runtime NODEPROPERTY 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
@@ -159,6 +173,15 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}NODEPROPERTY;"
+# Calling NODEPROPERTY function:
+RTE_function_0 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime NODEPROPERTY stage 2 execution failed." 1>&2
+    echo "Runtime NODEPROPERTY 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
}

function test_rte0_env() {
rtes="ENV"

read -r -d '' ENV <<'EOF'
export joboption_env_1="test=\"TEST\""
export joboption_env_2="rte_foo=\"foobar\""
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (environment = ("job_foo" "foobar"))
 (runtimeenvironment = "ENV")
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -21,11 +21,13 @@
 . $script
 }

 # Setting environment variables as specified by user
-export 'GRID_GLOBAL_JOBID=@TEST_JOB_ID@'
-export 'GRID_GLOBAL_JOBURL='
-export 'GRID_GLOBAL_JOBINTERFACE='
-export 'GRID_GLOBAL_JOBHOST='
+export 'job_foo=foobar'
+export 'test="TEST"'
+export 'rte_foo="foobar"'
+export 'GRID_GLOBAL_JOBINTERFACE='
+export 'GRID_GLOBAL_JOBHOST='
+export 'GRID_GLOBAL_JOBID=gsiftp://@TEST_HOSTNAME@:2811/jobs/rte0_env'
 
 RUNTIME_JOB_DIR=@TEST_SESSION_DIR@/@TEST_JOB_ID@
 RUNTIME_JOB_STDIN=/dev/null
@@ -103,8 +106,22 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for ENV:
+RTE_function_0 () {
+export joboption_env_1="test=\"TEST\""
+export joboption_env_2="rte_foo=\"foobar\""
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}ENV;"
+# Calling ENV function:
+RTE_function_0 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime ENV stage 1 execution failed." 1>&2
+    echo "Runtime ENV 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
@@ -163,6 +180,15 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}ENV;"
+# Calling ENV function:
+RTE_function_0 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime ENV stage 2 execution failed." 1>&2
+    echo "Runtime ENV 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
}

# Test passes with bash but not in dash.
function test_rte0_env_backslashes() {
rtes="ENV"

read -r -d '' ENV <<'EOF'
export joboption_env_1="test=\"TEST\""
export joboption_env_2="rte_foo=\"foo\\\\bar\""
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (environment = ("job_foo" "foo\\bar"))
 (runtimeenvironment = "ENV")
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -21,10 +21,13 @@
 }

 # Setting environment variables as specified by user
-export 'GRID_GLOBAL_JOBID=@TEST_JOB_ID@'
-export 'GRID_GLOBAL_JOBURL='
-export 'GRID_GLOBAL_JOBINTERFACE='
-export 'GRID_GLOBAL_JOBHOST='
+export 'job_foo=foo\\bar'
+export 'test="TEST"'
+export 'rte_foo="foo\\bar"'
+export GRID_GLOBAL_JOBID='gsiftp://@TEST_HOSTNAME@:2811/jobs/rte0_env_backslashes'
+export 'GRID_GLOBAL_JOBURL='
+export 'GRID_GLOBAL_JOBINTERFACE='
+export 'GRID_GLOBAL_JOBHOST='
 
 RUNTIME_JOB_DIR=@TEST_SESSION_DIR@/@TEST_JOB_ID@
 RUNTIME_JOB_STDIN=/dev/null
@@ -105,8 +106,20 @@
 
 if [ "$RESULT" = '0' ] ; then
 # Running runtime scripts
-export RUNTIME_CONFIG_DIR=${RUNTIME_CONFIG_DIR:-}
+export RUNTIME_CONFIG_DIR=${RUNTIME_CONFIG_DIR:-@TEST_SESSION_DIR@/rtes}
 runtimeenvironments=
+if [ ! -z "$RUNTIME_CONFIG_DIR" ] ; then
+  if [ -r "${RUNTIME_CONFIG_DIR}/ENV" ] ; then
+    runtimeenvironments="${runtimeenvironments}ENV;"
+    cmdl=${RUNTIME_CONFIG_DIR}/ENV 
+    sourcewithargs $cmdl 1  
+    if [ $? -ne '0' ] ; then 
+      echo "Runtime ENV script failed " 1>&2 
+      echo "Runtime ENV script failed " 1>"$RUNTIME_JOB_DIAG" 
+      exit 1
+    fi 
+  fi
+fi
 
 echo "runtimeenvironments=$runtimeenvironments" >> "$RUNTIME_JOB_DIAG"
 if [ ! "X$SLURM_NODEFILE" = 'X' ] ; then
@@ -163,6 +176,12 @@
 
 fi
 fi
+if [ ! -z "$RUNTIME_CONFIG_DIR" ] ; then
+  if [ -r "${RUNTIME_CONFIG_DIR}/ENV" ] ; then
+    cmdl=${RUNTIME_CONFIG_DIR}/ENV
+    sourcewithargs $cmdl 2  
+  fi
+fi
 
 if [ ! -z  "$RUNTIME_LOCAL_SCRATCH_DIR" ] ; then
   find ./ -type l -exec rm -f "{}" ";"
EOF
echo "${job_script_patch}" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_rte0_runtime() {
rtes="RTE EXTRA_RTE"

read -r -d '' RTE <<'EOF'
export joboption_runtime_1='EXTRA_RTE'
# TODO: Maybe set options?
EOF

read -r -d '' EXTRA_RTE <<'EOF'
export joboption_count=6
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (runtimeenvironment = "RTE")
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -7,6 +7,6 @@
 #SBATCH --nice=50
 #SBATCH -J 'gridjob'
 #SBATCH --get-user-env=10L
-#SBATCH -n 1
+#SBATCH -n 6
 #SBATCH 
 
@@ -99,8 +99,35 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for RTE:
+RTE_function_0 () {
+export joboption_runtime_1='EXTRA_RTE'
+# TODO: Maybe set options?
+}
+# RunTimeEnvironment function for EXTRA_RTE:
+RTE_function_1 () {
+export joboption_count=6
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}RTE;"
+# Calling RTE function:
+RTE_function_0 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime RTE stage 1 execution failed." 1>&2
+    echo "Runtime RTE stage 1 execution failed." 1>"${RUNTIME_JOB_DIAG}"
+    exit 1
+fi
+
+runtimeenvironments="${runtimeenvironments}EXTRA_RTE;"
+# Calling EXTRA_RTE function:
+RTE_function_1 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime EXTRA_RTE stage 1 execution failed." 1>&2
+    echo "Runtime EXTRA_RTE 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 +150,7 @@
   fi
 nodename=`/bin/hostname -f`
 echo "nodename=$nodename" >> "$RUNTIME_JOB_DIAG"
-echo "Processors=1" >> "$RUNTIME_JOB_DIAG"
+echo "Processors=6" >> "$RUNTIME_JOB_DIAG"
 executable='/bin/true'
 # Check if executable exists
 if [ ! -f "$executable" ]; 
@@ -159,6 +186,24 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}RTE;"
+# Calling RTE function:
+RTE_function_0 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime RTE stage 2 execution failed." 1>&2
+    echo "Runtime RTE stage 2 execution failed." 1>"${RUNTIME_JOB_DIAG}"
+    exit 1
+fi
+
+runtimeenvironments="${runtimeenvironments}EXTRA_RTE;"
+# Calling EXTRA_RTE function:
+RTE_function_1 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime EXTRA_RTE stage 2 execution failed." 1>&2
+    echo "Runtime EXTRA_RTE 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
}

function test_rte0_arg() {
rtes="ARG"

# TODO: Also set the joboption_arg_code variable.

read -r -d '' ARG <<'EOF'
joboption_arg_0="${joboption_arg_0}/echo"
export joboption_arg_1="Hello"
export joboption_arg_2="World"
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin")
 (runtimeenvironment = "ARG")
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -99,8 +99,23 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for ARG:
+RTE_function_0 () {
+joboption_arg_0="${joboption_arg_0}/echo"
+export joboption_arg_1="Hello"
+export joboption_arg_2="World"
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}ARG;"
+# Calling ARG function:
+RTE_function_0 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime ARG stage 1 execution failed." 1>&2
+    echo "Runtime ARG 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
@@ -124,7 +139,7 @@
 nodename=`/bin/hostname -f`
 echo "nodename=$nodename" >> "$RUNTIME_JOB_DIAG"
 echo "Processors=1" >> "$RUNTIME_JOB_DIAG"
-executable='/bin/true'
+executable='/bin/echo'
 # Check if executable exists
 if [ ! -f "$executable" ]; 
 then 
@@ -148,9 +163,9 @@
 fi 
 
 if [ -z "$GNU_TIME" ] ; then
-   "/bin/true" <$RUNTIME_JOB_STDIN 1>$RUNTIME_JOB_STDOUT 2>&1
+  "/bin/echo" "Hello" "World" <$RUNTIME_JOB_STDIN 1>$RUNTIME_JOB_STDOUT 2>&1
 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/echo" "Hello" "World" <$RUNTIME_JOB_STDIN 1>$RUNTIME_JOB_STDOUT 2>&1
 
 fi
 RESULT=$?
@@ -159,6 +174,15 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}ARG;"
+# Calling ARG function:
+RTE_function_0 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime ARG stage 2 execution failed." 1>&2
+    echo "Runtime ARG 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
}

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

rtes="DUMMY"

read -r -d '' DUMMY <<'EOF'
# This is a dummy RTE
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -99,8 +99,21 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for DUMMY:
+RTE_function_0 () {
+# This is a dummy RTE
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}DUMMY;"
+# Calling DUMMY function:
+RTE_function_0 1  
+if [ $? -ne 0 ]; then
+    echo "Runtime DUMMY stage 1 execution failed." 1>&2
+    echo "Runtime DUMMY 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
@@ -159,6 +172,15 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}DUMMY;"
+# Calling DUMMY function:
+RTE_function_0 2  
+if [ $? -ne 0 ]; then
+    echo "Runtime DUMMY stage 2 execution failed." 1>&2
+    echo "Runtime DUMMY 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
}

function test_rte_with_options() {
read -r -d '' job_description_input <<'EOF'
<?xml version="1.0"?>
<ActivityDescription xmlns="http://www.eu-emi.eu/es/2010/12/adl">
<Application>
<Executable><Path>/bin/true</Path></Executable>
</Application>
<Resources>
<IndividualPhysicalMemory>1572864000</IndividualPhysicalMemory>
<RuntimeEnvironment>
<Name>INCREASEMEMORY_1</Name>
<Option>2000</Option>
<Option>--test "TESTING\\\\ TEST"</Option>
</RuntimeEnvironment>
<RuntimeEnvironment>
<Name>INCREASEMEMORY_2</Name>
<Option>5000</Option>
</RuntimeEnvironment>
</Resources>
</ActivityDescription>
EOF

rtes="INCREASEMEMORY_1 INCREASEMEMORY_2"

read -r -d '' INCREASEMEMORY_1 <<'EOF'
joboption_memory=$(( $(echo ${joboption_memory} + ${2} | tr -d \") ))
EOF

read -r -d '' INCREASEMEMORY_2 <<'EOF'
joboption_memory=$(( $(echo ${joboption_memory} + ${2} + ${2} | tr -d \") ))
EOF

read -r -d '' job_script_patch <<'EOF'
--- a/basic-script.sh 2014-01-28 10:35:19.780083853 +0100
+++ b/basic-script.sh 2014-01-28 10:35:19.772083853 +0100
@@ -9,6 +9,7 @@
 #SBATCH --get-user-env=10L
 #SBATCH -n 1
 #SBATCH 
+#SBATCH --mem-per-cpu=13500
 
 # Overide umask of execution node (sometime values are really strange)
 umask 077
@@ -99,8 +99,34 @@
 RESULT=0
 
 if [ "$RESULT" = '0' ] ; then
+# RunTimeEnvironment function for INCREASEMEMORY_1:
+RTE_function_0 () {
+joboption_memory=$(( $(echo ${joboption_memory} + ${2} | tr -d \") ))
+}
+# RunTimeEnvironment function for INCREASEMEMORY_2:
+RTE_function_1 () {
+joboption_memory=$(( $(echo ${joboption_memory} + ${2} + ${2} | tr -d \") ))
+}
 # Running RTE scripts (stage 1)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}INCREASEMEMORY_1;"
+# Calling INCREASEMEMORY_1 function:
+RTE_function_0 1 "2000" "--test \"TESTING\\ TEST\"" 
+if [ $? -ne 0 ]; then
+    echo "Runtime INCREASEMEMORY_1 stage 1 execution failed." 1>&2
+    echo "Runtime INCREASEMEMORY_1 stage 1 execution failed." 1>"${RUNTIME_JOB_DIAG}"
+    exit 1
+fi
+
+runtimeenvironments="${runtimeenvironments}INCREASEMEMORY_2;"
+# Calling INCREASEMEMORY_2 function:
+RTE_function_1 1 "5000" 
+if [ $? -ne 0 ]; then
+    echo "Runtime INCREASEMEMORY_2 stage 1 execution failed." 1>&2
+    echo "Runtime INCREASEMEMORY_2 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
@@ -159,6 +185,24 @@
 fi
 # Running RTE scripts (stage 2)
 runtimeenvironments=
+runtimeenvironments="${runtimeenvironments}INCREASEMEMORY_1;"
+# Calling INCREASEMEMORY_1 function:
+RTE_function_0 2 "2000" "--test \"TESTING\\ TEST\"" 
+if [ $? -ne 0 ]; then
+    echo "Runtime INCREASEMEMORY_1 stage 2 execution failed." 1>&2
+    echo "Runtime INCREASEMEMORY_1 stage 2 execution failed." 1>"${RUNTIME_JOB_DIAG}"
+    exit 1
+fi
+
+runtimeenvironments="${runtimeenvironments}INCREASEMEMORY_2;"
+# Calling INCREASEMEMORY_2 function:
+RTE_function_1 2 "5000" 
+if [ $? -ne 0 ]; then
+    echo "Runtime INCREASEMEMORY_2 stage 2 execution failed." 1>&2
+    echo "Runtime INCREASEMEMORY_2 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
}
