TESTS="outputfiles"

simulate_cmds="sbatch rm" # Simulate rm in order not to get job script deleted
read -r -d '' simulator_output <<'EOF'
rargs="/sbatch .*/"
output="Submitted batch job 1"

rargs="/rm .*/"
output=""
EOF

read -r -d '' general_arc_test_configuration <<EOF
[lrms]
slurm_bin_path=@PWD@/bin
EOF

function test_outputfiles() {
read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
(stdin="in")
(stdout="out")
(stderr="error")
(outputfiles =
  ("testfile" "")
  ("@list_of_outputfiles_generated_by_job" "")
  ("another_testfile" "https://example.com/foo")
  ("file with spaces" "")
  ("very tricky\\ file\\\\" "https://example.com/foo")
  ("another'tricky'file" "")
  ("another\'tricky\'file" "")
)
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
@@ -23,9 +23,9 @@
 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_STDIN=in
+RUNTIME_JOB_STDOUT=@TEST_SESSION_DIR@/@TEST_JOB_ID@/out
+RUNTIME_JOB_STDERR=@TEST_SESSION_DIR@/@TEST_JOB_ID@/error
 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`
@@ -148,9 +148,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=$?
@@ -161,6 +161,19 @@
 if [ ! -z  "$RUNTIME_LOCAL_SCRATCH_DIR" ] ; then
   find ./ -type l -exec rm -f "{}" ";"
   chmod -R u+w "./"
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'testfile' 2>/dev/null
+  dynlist='list_of_outputfiles_generated_by_job'
+  chmod -R u-w "./$dynlist" 2>/dev/null
+  cat "./$dynlist" | while read name rest; do
+    chmod -R u-w "./$name" 2>/dev/null
+  done
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'another_testfile' 2>/dev/null
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'another_testfile' 2>/dev/null
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'file with spaces' 2>/dev/null
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'very tricky\\ file\\\\' 2>/dev/null
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'very tricky\\ file\\\\' 2>/dev/null
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'another'\''tricky'\''file' 2>/dev/null
+  chmod -R u-w "$RUNTIME_JOB_DIR"/'another\'\''tricky\'\''file' 2>/dev/null
   find ./ -type f -perm /200 -exec rm -f "{}" ";"
   chmod -R u+w "./"
 fi
EOF
echo "${job_script_patch}" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}
