#!/bin/bash

# $1 and $2 are special variables in bash that contain the 1st and 2nd 
# command line arguments to the script, which are the names of the
# Dakota parameters and results files, respectively.

params=$1
results=$2

############################################################################### 
##
## Pre-processing Phase -- Generate/configure an input file for your simulation 
##  by substiting in parameter values from the Dakota paramters file.
##
###############################################################################

dprepro $params mymfile.template mymfile.m

############################################################################### 
##
## Execution Phase -- Run your simulation
##
###############################################################################

matlab -nodisplay << EOF &>> matlab.out

mymfile
save('myworkspace')

exit
EOF

############################################################################### 
##
## Post-processing Phase -- Extract (or calculate) quantities of interest
##  from your simulation's output and write them to a properly-formatted
##  Dakota results file.
##
###############################################################################

result1=$(tail -9 matlab.out | head -1 | awk '{print $1}')
result2=$(tail -3 matlab.out | head -1 | awk '{print $1}')
echo "$result1 p1_factorial" > $results
echo "$result2 p2_factorial" >> $results
