#!/bin/bash
# StarVZ - R-based visualization techniques for the StarPU runtime
#  Main command encapsulator

STARVZ_HOME="$( cd "$(dirname "$0")/../" >/dev/null 2>&1 ; pwd -P )"

# Help Message
usage () {
    cat <<EOF
StarVZ - R-based visualization techniques for the StarPU runtime

Usage: $0 [OPTIONS] [TRACE_FOLDER]

Options:
  -h, --help                       # Shows this message
  -1, --phase1 TRACE_FOLDER        # Execute only phase 1 on TRACE_FOLDER
  -2, --phase2 TRACE_FOLDER        # Execute only phase 2 on TRACE_FOLDER
  -b, --both-phases TRACE_FOLDER   # Execute phase 1 and 2 on TRACE_FOLDER (Default)
  -a, --application APPLICATION    # Hard define APPLICATION for scripts
  -c, --config CONFIG_FILE         # Use CONFIG_FILE as config file
                                       default is only st chart

  -m, --memory                     # Do the extend memory traces options
                                       -memory-states on starpu_fxt_tool

  -f, --feather                     # Use feather files intead of parquet (Deprecated?)
  -k, --keep                        # Keep intermediate files on phase1
  -s, --save                        # Name of the file to save
  -e, --exclude-tasks TASKLIST      # Exclude tasks from the analysis
  -t, --use-paje-trace              # Use existing paje.trace and friends (tasks.rec, dag.dot) skipping starpu_fxt_tool

      --git-version                    # Shows the git hash
      --version                        # Shows R package installed version

      --extra-memory-snaps                 # Create multiple snaps of memory
      --extra-memory-snaps-step TIME(ms)   # Steps For memory snaps

Report bugs to: <https://github.com/schnorr/starvz/issues>
StarVZ home page: <https://github.com/schnorr/starvz/>
EOF
}

if [ $# -eq 0 ]
  then
    usage;
    exit 0;
fi

STARVZ_EXEC_PHASE_1=1
STARVZ_EXEC_PHASE_2=1
STARVZ_EXEC_MEM_SNAPS=0
STARVZ_EXEC_MEM_SNAPS_STEP=1000
STARVZ_TRACE_FOLDER=""
STARVZ_APPLICATION="-"
STARVZ_CONFIG_FILE=""
STARVZ_SAVE_FILE=""
STARVZ_USE_PAJE_TRACE=0

while true; do
        case "$1" in
                --phase1|-1 )
                        STARVZ_EXEC_PHASE_1=1
                        STARVZ_EXEC_PHASE_2=0
                        shift 1
                        ;;
                --phase2|-2 )
                        STARVZ_EXEC_PHASE_1=0
                        STARVZ_EXEC_PHASE_2=1
                        shift 1
                        ;;
                --keep|-k )
                        export STARVZ_KEEP=1
                        shift 1
                        ;;
                --both-phases|-b )
                        STARVZ_EXEC_PHASE_1=1
                        STARVZ_EXEC_PHASE_2=1
                        shift 1
                        ;;
                "--git-version")
                        echo "$(cd $STARVZ_HOME; git rev-parse --verify HEAD)"
                        shift 1
                        exit 0;
                        ;;
                --help|-h)
                        usage;
                        exit 0;
                        ;;
                --memory|-m)
                        export STARPU_FXT_OPTIONS="-memory-states"
                        shift 1
                        ;;
                --application|-a)
                        STARVZ_APPLICATION=$2
                        shift 2
                        ;;
                --save|-s)
                        STARVZ_SAVE_FILE=$(readlink -f $2)
                        shift 2
                        ;;
                --config|-c)
                        STARVZ_CONFIG_FILE=$(readlink -f $2)
                        shift 2
                        ;;
                --exclude-tasks|-e)
                        export STARVZ_EXCLUDE_TASKS=$2
                        shift 2
                        ;;
		--use-paje-trace|-t)
		        export STARVZ_USE_PAJE_TRACE=1
		        shift 1
		        ;;
                --version)
                        VERSION=$(Rscript -e 'message(packageVersion("starvz"))' 2>&1)
                        echo "StarVZ version $VERSION"
                        exit 0
                        ;;
                --extra-memory-snaps)
                        STARVZ_EXEC_PHASE_1=0
                        STARVZ_EXEC_PHASE_2=0
                        STARVZ_EXEC_MEM_SNAPS=1
                        shift 1
                        ;;
                --extra-memory-snaps-step)
                        STARVZ_EXEC_MEM_SNAPS_STEP=$2
                        shift 2
                        ;;
                "")
                        break
                        ;;
                *)
                        # Make it absolute
                        STARVZ_TRACE_FOLDER=$(cd $1; pwd)
                        # if no configuration file was set yet try a config inside trace folder
                        if [[ -z "$STARVZ_CONFIG_FILE" ]]; then
                          STARVZ_CONFIG_FILE="$STARVZ_TRACE_FOLDER/config.yaml"
                        fi
                        shift 1
                        ;;
        esac
done

# Process, lets check if all we need is here
if [ ! -r "$STARVZ_TRACE_FOLDER" ]; then
        echo "$STARVZ_TRACE_FOLDER can't be read"
        exit 1
fi

# If config file dont exist fall to default
if [ ! -r "$STARVZ_CONFIG_FILE" ]; then
        STARVZ_CONFIG_FILE="$STARVZ_HOME/etc/default.yaml"
        echo "Warn: Configuration file not found, using default"
fi

pushd $STARVZ_HOME/tools/
  if [ "$STARVZ_EXEC_PHASE_1" -eq 1 ]; then
    ./phase1-workflow.sh $STARVZ_TRACE_FOLDER $STARVZ_APPLICATION $STARVZ_CONFIG_FILE
    p1e=$?
    if [ $p1e -ne 0 ]
    then
        echo "Phase 1 Failed (exit status: $p1e) stopping"
        exit 1
    fi
  fi
  if [ "$STARVZ_EXEC_PHASE_2" -eq 1 ]; then
    ./phase2-workflow.R $STARVZ_TRACE_FOLDER $STARVZ_CONFIG_FILE $STARVZ_SAVE_FILE
  fi
  if [ "$STARVZ_EXEC_MEM_SNAPS" -eq 1 ]; then
    if [ ! -r "$STARVZ_TRACE_FOLDER/events_memory.parquet" ]; then
      echo "MEMSNAPS: Please run starvz with -1 -m options first"
    else
      ./phase2-memsnaps.R $STARVZ_TRACE_FOLDER $STARVZ_EXEC_MEM_SNAPS_STEP
    fi
  fi
popd
