#!/bin/bash
ERROR=false
DISPLAY_HELP=false
EXECUTABLE=
OPTIONS=
GEN_FILE_TEMPLATE="gen_*"

#Function to print usage
function printUsage {
    echo "Usage:"
    echo "load_all.sh [options] executable"
    echo "-h | --help     : Displays this help message"
    echo "-o | --options  : Provide additional options for loading"
}

#Read commandline options
while [ $# -gt 0 ] 
do
  case "$1" in
    -h | --help ) DISPLAY_HELP=true;;
    -o | --options ) OPTIONS="$2"; shift ;;
    -g | --gen ) GEN_FILE_TEMPLATE="$2"; shift ;;
    -* ) ERROR=true; ERROR_MESSAGE="Unknown option ${1}"; break;;
    * ) if [ -z $EXECUTABLE ]; then EXECUTABLE=$1; else ERROR=true; ERROR_MESSAGE="Multiple executables found: '${EXECUTABLE}' and '${1}'"; fi ;;
  esac
  shift;
done


#Perform error checking
if [ ! -f "$EXECUTABLE" -a ! $ERROR ]; then
  ERROR=true
  ERROR_MESSAGE="Directory '${EXECUTABLE}' not found"
fi

#Print an error if an error was found, or print help if that option was provided
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

#Run the analysis on the generation file
for INPUT_RUN_DIR in `find . -maxdepth 1 -type d -name 'run_*'`
do
    GEN_FILES=`find "${INPUT_RUN_DIR}" -maxdepth 2 -mindepth 2 -type f -name "${GEN_FILE_TEMPLATE}"`
    for GEN_FILE in ${GEN_FILES}
    do
        ${EXECUTABLE} --load=${GEN_FILE} --nn-postfix ${INPUT_RUN_DIR:2} ${OPTIONS} -o "${INPUT_RUN_DIR:2}indiv.log"
    done
done
