#!/bin/ksh

cat <<EOF >$BATCH_SCRIPT
#!/bin/ksh
#
# @ job_type = serial
# @ class = serial
# @ resources = ConsumableCpus(4) ConsumableMemory(1gb)
# @ initialdir = $RUN_DIR_HPC
# @ output = hpc_batch.stdout
# @ error  = hpc_batch.stderr
# @ queue

. prg_12_1_0_2 
export PATH=$RUN_DIR_HPC/fcm/bin:\$PATH
export DIFF="/opt/freeware/bin/diff"

. $BATCH_DIRS_NAME
let failed=0
for TEST in \$TESTS
do
  cd $RUN_DIR_HPC/\$TEST
  echo "\$(date): Running \$TEST ..."
  find src -exec touch {} \\;
  fcm build -v 2 -j 4 >../\$TEST.build.stdout.1 2>../\$TEST.build.stderr.1
  RC=\$?
  if [[ \$RC != 0 ]]; then
    echo "FAILED: \$TEST failed"
    let failed=failed+1
  else
EOF

if [[ $TYPE == control ]]; then
  echo "    touch .tests.complete" >>$BATCH_SCRIPT
else
  cat <<EOF >>$BATCH_SCRIPT
    export COMPARE_TIMES=true
    export DEBUG=$DEBUG
    cd $BASE_DIR_HPC
    ./compare_results \$TEST
    if [[ \$? != 0 ]]; then
      let failed=failed+1
    fi
EOF
fi

cat <<EOF >>$BATCH_SCRIPT
  fi
done

echo "\$(date): HPC performance tests finished"
if [[ \$failed == 0 ]]; then
  echo "SUMMARY: All HPC performance tests succeeded"
else
  echo "SUMMARY: \$failed HPC performance tests failed"
fi
EOF
