#!/bin/sh
DIRNAME=`dirname $0`
#PATH_TO_ME=`which $0`;
# Give a space-separated list of classpath items RELATIVE TO THE CURRENT SCRIPT
# These will be resolved into absolute pathnames
# Wildcards are allowed
CLASSPATH_RELATIVE=owltools-runner-all.jar

## Check for Cygwin, use grep for a case-insensitive search
IS_CYGWIN="FALSE"
if uname | grep -iq cygwin; then
    IS_CYGWIN="TRUE"
fi

# If literal classpath values are needed, uncomment the line below
# This can be useful if the classpath contains URLs
# CLASSPATH_LITERAL=""

# To set a specific default Java path, set the JAVAPATH variable below.
# This value can be overridden with the -Jvm= option.
# If JAVAPATH is not set, the script will use whatever version of Java is on the
# path. If there is no copy of Java on the path, the JAVA_HOME environment
# variable will be used. If that fails, we just use "java" in the hopes that the
# failure message will make a little more sense.
# JAVAPATH="java"

if [ $IS_CYGWIN = "TRUE" ]
then
    PATH_SEP=";"
else
    PATH_SEP=":"
fi

JAVAARGS=" "
CMDARGS=" "

# Remove the name of this script from the end of the path
PATH_TO_ME=`which $0`;
PATH_TO_ME=`echo $PATH_TO_ME | sed -e "s/\(.*\)\/.*/\1/g"`

if [ $IS_CYGWIN = "TRUE" ]
then
    LAUNCHER_DIR="`cygpath --mixed $PATH_TO_ME`"
else
    LAUNCHER_DIR="$PATH_TO_ME"
fi


# Just the name of the script.
SCRIPTNAME=`echo $PATH_TO_ME | sed -e "s/.*\/\(.*\)/\1/g"`

## Add vmoptions to JAVAARGS if the proper file is available.
if [ -e "$PATH_TO_ME/$SCRIPTNAME.vmoptions" ]
then
    VMOPTIONS=`cat $PATH_TO_ME/$SCRIPTNAME.vmoptions`
    for OPTION in "$VMOPTIONS"
    do
	JAVAARGS="$JAVAARGS '${OPTION}'"
    done
else
    if [ $OWLTOOLS_MEMORY ]
    then
        JAVAARGS="$JAVAARGS -Xmx$OWLTOOLS_MEMORY"
    else
        JAVAARGS="$JAVAARGS -Xmx6G"
    fi
fi

## Walk through all the command line arguments and add them to
## the CMDARGS depending.

for ARG in "$@"
do
    CMDARGS="${CMDARGS} '${ARG}'"
    shift 1;
done

## Add to CLASSPATH using CLASSPATH_RELATIVE.
CLASSPATH=""
for ARG in "$CLASSPATH_RELATIVE"
do
    DEREFERENCED_CLASSPATH=`ls -1 -L $PATH_TO_ME/$ARG | grep -v ontologyrelease`
    for CP_ENTRY in $DEREFERENCED_CLASSPATH
    do
        if [ $IS_CYGWIN = "TRUE" ]
        then
            CP_ENTRY="`cygpath --mixed \"$CP_ENTRY\"`"
        fi
	if [ -z "$CLASSPATH" ]
	then
	    CLASSPATH="$CP_ENTRY"
	else
	    CLASSPATH="$CLASSPATH$PATH_SEP$CP_ENTRY"
	fi
    done
done

## Add to CLASSPATH using CLASSPATH_LITERAL.
if [ -n "$CLASSPATH_LITERAL" ]
then
    for CP_ENTRY in $CLASSPATH_LITERAL
    do
	if [ -z "$CLASSPATH" ]
	then
	    CLASSPATH="$CP_ENTRY"
	else
	    CLASSPATH="$CLASSPATH$PATH_SEP$CP_ENTRY"
	fi
    done
fi

## Figure out which java to use.
JAVA_8_PATH=/hps/nobackup/production/metagenomics/pipeline/tools/jre1.8.0_101/bin/java
if [ -f "$JAVA_8_PATH" ]
then
    JAVAPATH=$JAVA_8_PATH
else
    if [ -z "$JAVAPATH" ]
    then
        JAVAPATH=`which java`
        if [ -z "$JAVAPATH" ]
        then
        if [ -n "$JAVA_HOME" && -e "$JAVA_HOME" ]
        then
            JAVAPATH=$JAVA_HOME/bin/java
        else
            JAVAPATH="java"
        fi
        fi
    fi
fi

## Assemble and run the final command.
CMD="\"$JAVAPATH\" -Xms2048M -DentityExpansionLimit=4086000 -Djava.awt.headless=true -classpath \"$CLASSPATH\" -DlauncherDir=\"$LAUNCHER_DIR\" $JAVAARGS owltools.cli.CommandLineInterface $CMDARGS"
echo $CMD
## DEBUG
## Let's see a little of the environment if we declare DEBUG=1.
if [ $DEBUG ]
then
    for MYVAR in DEBUG PATH_TO_ME SCRIPTNAME DIRNAME JAVAARGS CMDARGS CLASSPATH_RELATIVE CMD
    do
	LETVAR=`echo "$"$MYVAR`
	TMPVAR=`eval echo $LETVAR`
	echo "${MYVAR}: \"${TMPVAR}\""
    done
fi

## Run the final command.
if [ $DEBUG ]
then
    echo "$CMD"
fi
sh -c "$CMD"