#!/bin/bash
#ALL=20000
#SCRIPT_DIR="${HOME}/sferes/sferes2/scripts"
#STAT=0

ANALYSIS_OUTPUT_FILE="best.log"
RUN_ANALYSIS="undef"
CREATE_SUBSTRATE_VISUALISATIONS="undef"
DEFAULT_STATE=true
ERROR=false
DISPLAY_HELP=false
CHECK_ARGUMENTS=false
RUN_DIR=

function printUsage {
    echo "Usage:"
    echo "visualize.sh [options] directory"
    echo "-h | --help     : Displays this help message"
    echo "-o | --only     : Set the default of all visualisation options to false. So, to run only lineplots use -o --lineplots."
    echo "--no_analysis   : Don't load the gen_ file in sferes."
    echo "--no_substrate  : Don't create the substrates."
    echo "--analysis      : Load the gen_ file using sferes"
    echo "--substrate     : Create substrates. Use with -o."
    echo "-c | --check    : Don't run anything but check the input parameters for errors."
}

while [ $# -gt 0 ] 
do
  case "$1" in
    -h | --help ) DISPLAY_HELP=true;;
    -o | --only ) DEFAULT_STATE=false;;
    --exe ) EXECUTABLE="$2"; shift ;;
    --no_analysis ) RUN_ANALYSIS=false;;
    --no_substrate ) CREATE_SUBSTRATE_VISUALISATIONS=false;;
    --analysis ) RUN_ANALYSIS=true;;
    --substrate ) CREATE_SUBSTRATE_VISUALISATIONS=true;;
    -c | --check ) CHECK_ARGUMENTS=true;;
    -* ) ERROR=true; ERROR_MESSAGE="Unknown option ${1}"; break;;
    * ) if [ -z $RUN_DIR ]; then RUN_DIR=$1; else ERROR=true; ERROR_MESSAGE="Multiple directories found: '${RUN_DIR}' and '${1}'"; fi ;;
  esac
  shift;
done

#Set the run dir
if [ ! -d "$RUN_DIR" -a ! $ERROR ]; then
  ERROR=true
  ERROR_MESSAGE="Directory '${RUN_DIR}' not found"
else
  cd $RUN_DIR
  RELATIVE_DIR=$RUN_DIR
  RUN_DIR=`pwd`
fi

#Set al undefined variables to their default state
if [ $RUN_ANALYSIS == "undef" ]; then RUN_ANALYSIS=${DEFAULT_STATE}; fi
if [ $CREATE_SUBSTRATE_VISUALISATIONS == "undef" ]; then CREATE_SUBSTRATE_VISUALISATIONS=${DEFAULT_STATE}; fi

if $DISPLAY_HELP ; then printUsage; if $CHECK_ARGUMENTS; then exit 1; else exit 0; fi; fi
if $ERROR ; then echo Error: $ERROR_MESSAGE; printUsage; exit 1; fi

echo Settings: 
echo RUN_DIR: ${RUN_DIR}
echo EXECUTABLE: ${EXECUTABLE}
echo RUN_ANALYSIS: $RUN_ANALYSIS
echo CREATE_SUBSTRATE_VISUALISATIONS: $CREATE_SUBSTRATE_VISUALISATIONS

if $CHECK_ARGUMENTS; then exit 0; fi

#Set root
ROOT=${RUN_DIR}/..

mkdir -p ${RUN_DIR}/log


#Run the analysis on the generation file
if $RUN_ANALYSIS ;
then
    cd ${RUN_DIR}

    for INPUT_RUN_DIR in `find . -maxdepth 1 -type d -name 'run_*'`
    do
	GEN_FILES=`find "${RUN_DIR}/${INPUT_RUN_DIR}" -maxdepth 2 -mindepth 2 -type f -name 'gen_*'`
        CURRENT_RUN=${INPUT_RUN_DIR#./}
        OUTPUT_RUN_DIR=log/${CURRENT_RUN}
        #OUTPUT_GEN_DIR=${OUTPUT_RUN_DIR}/gen_${PREFIX}
	mkdir -p $OUTPUT_RUN_DIR
        cd $OUTPUT_RUN_DIR

        #if $ALWAYS_READ_GEN ;
        #then
        #        GEN=`grep -m 1 'unsigned nb_gen =' ${CURRENT_RUN}/thisistheoutput | sed -e 's/.*unsigned nb_gen = //' -e 's/1;/0/'`
        #        PREFIX=`printf "%05d" $GEN`
        #fi
        for GEN_FILE in ${GEN_FILES}
        do
            echo ${EXECUTABLE} --load=${GEN_FILE} --out ${ANALYSIS_OUTPUT_FILE}
            ${EXECUTABLE} --load=${GEN_FILE} --out ${ANALYSIS_OUTPUT_FILE}
        done
        #OUTPUT_RUN_DIR=log/${CURRENT_RUN}
        #OUTPUT_GEN_DIR=${OUTPUT_RUN_DIR}/gen_${PREFIX}
        #mkdir -p $OUTPUT_GEN_DIR
        #cd $OUTPUT_GEN_DIR
        #for INPUT_MMM_DIR in `find ${RUN_DIR}/${INPUT_RUN_DIR#.} -maxdepth 1 -type d -name 'mmm*'`
        #do
        #    echo ${EXECUTABLE} --load=${INPUT_MMM_DIR}/gen_${GEN} --out best.log
        #    ${EXECUTABLE} --load=${INPUT_MMM_DIR}/gen_${GEN} --out best.log
        #    #rm -rf mmm*
        #done
        cd ${RUN_DIR}
    done
fi

cd $ROOT


#Create substrate visualisations
if $CREATE_SUBSTRATE_VISUALISATIONS ;
then
    cd ${RUN_DIR}/log
    rm -r *_dot
    rm -r *_svg
    rm -r *_pdf
    rm -r *_sorted
    cd $ROOT

    echo "copyBest.py -d ${RUN_DIR}/log ${RUN_DIR} -i ${ANALYSIS_OUTPUT_FILE}"
    copyBest.py -d ${RUN_DIR}/log ${RUN_DIR} -i ${ANALYSIS_OUTPUT_FILE}

    cd ${RUN_DIR}/log
    echo "convertSvg.py *_svg"
    convertSvg.py *_svg
    cd $ROOT
fi
