#!/bin/sh
#cd ${0%/*} || exit 1    # Run from this directory

# Debug
#set -x

# Source tutorial run functions
# . $WM_PROJECT_USER_DIR/XylemFoam/tools/RunFunctions #Prints log to stdout AND the log.* file
. $WM_PROJECT_DIR/bin/tools/RunFunctions #Prints log to the log.* file only

#- Kill all subprocesses on exit
#jobs=()
#trap '(( #jobs == 0 )) || kill $jobs' EXIT HUP TERM INT
cleanup() {
    # kill all processes whose parent is this process
    #pkill -P $$
    pkill gnuplot
}

for sig in INT QUIT HUP TERM; do
  trap "
    cleanup
    trap - $sig EXIT
    kill -s $sig "'"$$"' "$sig"
done
trap cleanup EXIT

# Source tutorial clean functions
#. $WM_PROJECT_DIR/bin/tools/CleanFunctions

#set -o xtrace

display_help()
{
    echo "Usage: $(basename $0) [OPTION]"
    echo "options:"
    echo "  -t, --time      the simulation start time to monitor"
    echo "  -s, --supplement <filepath>"
    echo "                  location of a supplemental gnuplot script to include in monitors"
    echo "  -h, --help      print usage"
}

startTime=0

#parse arguments
while (( "$#" )); do
case "$1" in
    -t|--time)
        if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
            startTime=$2
            shift 1
            echo "Parsing time."
        else
            echo "ERROR:  Argument for $1 is missing" >&2
            exit 1
        fi
        shift
        ;;
    -s|--supplement)
        supplementalCommands=()
        shift 1
        while [ ${#} -gt 0 ]
        do
            echo "Parsing supplemental command."
            if [[ $1 == -* ]] || [[ $1 == --* ]]
            then
                break
            else
                supplementalCommands+=("$1")
                shift 1
            fi
        done
        ;;
    -h|--help)
        display_help
        exit 0
        ;;
    -*|--*)
        echo "Invalid option $1"
        display_help
        exit 1
esac
done

for dir in postProcessing/*/
do
    name=${dir#"postProcessing/"}
    name=${name%"/"}
    if [[ $name == probes ]]; then
        file="$dir$startTime"/p
        #- get the probe labels at the begining of the file
        mapfile -t -n 20 lines < ${file}
        #echo $lines
        labels=()
        for i in "${lines[@]}"; do
            if [[ $i[0] == /# ]]; then
                labels=( ${labels} ${i#?} )
            else
                break
            fi
        done
        echo $labels
    fi
done

file=postProcessing/residuals/"$startTime"/residuals
#- Get the latest log file
if [ -f "$file"_1.dat ];
then
    f=1
    while [ -f "$file"_"$((f+1))".dat ]
    do
        f=$((f+1))
    done
    file="$file"_"$f".dat
else
    file="$file".dat
fi
foamMonitor -l "$file" &> /dev/null & \
# foamMonitor  postProcessing/probes/"$startTime"/U & \
# foamMonitor  postProcessing/probes/"$startTime"/p &
pfmonitorvector U &> /dev/null &
#set terminal x11 1
#gnuplot -p ravdMonitors &> /dev/null &
# set terminal x11 2
# gnuplot -p swirlMonitors &

# x11 terminal number
nwin=2

for command in ${supplementalCommands[@]}
do

    if [[ -f $command ]]
    then
        echo "Running supplemental command."
        . $command
        # gnuplot -p $(command) &
        nwin+=1
    else
        echo "WARNING: Skipping supplemental monitor.  File not found: ${command}"
    fi
done

tail -f log.$(getApplication)

#------------------------------------------------------------------------------
