#!/bin/bash

# This script runs Goby with the specified amount of memory.
# The script will find the Goby jar if it is located in the same directory as this script

# Usage: goby mem-requirement mode-name [param]+
# Where mem-requirement indicates how much memory Goby should be given (e.g., 1g, 500m).
# Mode-name should specify a Goby mode.
# For instance, the following command will display the Goby version number:

#     goby 40m version
if [ $# -lt 1 ]; then
  echo "The first argument must be memory (i.e., goby 4g)"
  exit 1
fi

memory_requirement=$1
shift
other_parameters=$*
set +x
WORKING_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ $OSTYPE == "cygwin" ]]; then
    WORKING_DIR=`cygpath -m "${WORKING_DIR}"`
fi

export GOBY_HOME=${WORKING_DIR}
GOBY_JAR=${GOBY_HOME}/goby.jar
SLF4J_CONFIG=${GOBY_HOME}/config/goby-logback.xml
if [ ! -z ${GOBY_USE_RJAVA} ]; then
    if [ "${RJAVA_HOME:-notset}" == "notset" ]; then
      #echo "Trying to set RJAVA_HOME environment variable"
    cat >/tmp/r-cmd.txt <<EOT
    system.file("jri",package="rJava")
EOT
      tmpWithQuotes=`R --no-save </tmp/r-cmd.txt|grep [1]|tail -1 |awk '{print $2}'`
      temp="${tmpWithQuotes%\"}"
      temp="${temp#\"}"
     # echo "$temp"
      export RJAVA_HOME=${temp}
      rm -f /tmp/r-cmd.txt
    fi
fi
DL_SOMATIC_CLASSPATH=${GOBY_HOME}/somatic.jar
if [ ! -e "${DL_SOMATIC_CLASSPATH}" ]; then
    echo "Model somatic.jar not found. It must be installed at ${GOBY_HOME}/somatic.jar"
fi
DL_GENOTYPE_CLASSPATH=${GOBY_HOME}/genotype.jar
if [ ! -e "${DL_SOMATIC_CLASSPATH}" ]; then
    echo "Model genotype.jar not found. It must be installed at ${GOBY_HOME}/genotype.jar"
fi
DL_FRAMEWORK_CLASSPATH=${GOBY_HOME}/framework.jar
if [ ! -e "${DL_FRAMEWORK_CLASSPATH}" ]; then
    echo "Model framework.jar not found. It must be installed at ${GOBY_HOME}/framework.jar"
fi
#echo java -ea -Xmx${memory_requirement} -Dlog4j.configuration=${LOG4J_PROPS} -Djava.library.path=${RJAVA_HOME} -jar ${GOBY_JAR} --mode ${other_parameters}
java -ea -Djava.ext.dirs=${GOBY_HOME}/  -Xmx${memory_requirement} -Dlogback.configurationFile=${SLF4J_CONFIG} -Djava.library.path=${RJAVA_HOME} -jar ${GOBY_JAR} --mode ${other_parameters}
if [ $# -lt 2 ]; then
  echo "The second argument must be the goby mode you want to run (i.e., goby 1g version). See list of modes in help above."
fi