#!/bin/sh
# Set the classname of the Java class to execute
CLASSNAME=org.mozilla.javascript.tools.shell.Main

# If literal classpath values are needed, uncomment the line below
# This can be useful if the classpath contains URLs
# CLASSPATH_LITERAL=""
# NOTE:: jline 0.9.93 works - jline 1.0 does NOT
#CLASSPATH_LITERAL=$HOME/src/jline-0.9.93/jline-0.9.93.jar
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"

PATH_SEP=":"

JAVAARGS=" "
CMDARGS=" "

PATH_TO_ME=`which $0`;

#Now see if the path is actually a symbolic link. If it is, set TEMP
#to true
TEMP=`ls -l $PATH_TO_ME | sed -e "s/.* -> \(.*\)/true/g"`
if [ -z "$TEMP" ]
then
   TEMP='false';
fi

#While we keep finding symbolic links...
while [ "$TEMP" = "true" ]
do
   #Parse the ls output and set PATH_TO_ME to the actual location of the
   #symbolic link
   PATH_TO_ME=`ls -l $PATH_TO_ME | sed -e "s/.* -> \(.*\)/\1/g"`

   TEMP=`ls -l $PATH_TO_ME | sed -e "s/.* -> \(.*\)/true/g"`
   if [ -z "$TEMP" ]
   then
      TEMP='false'
   fi
done

#If PATH_TO_ME is a relative link, set TEMP to true, otherwise set
#TEMP to false
TEMP=`echo $PATH_TO_ME | sed -e "s/^\/.*/true/g"`
if [ -z "$TEMP" ]
then
   TEMP='false'
fi

#If PATH_TO_ME was a relative link, change it to an absolute reference
if [ $TEMP != 'true' ]
then
   PATH_TO_ME="$PWD/$PATH_TO_ME"
fi

SCRIPTNAME=`echo $PATH_TO_ME | sed -e "s/.*\/\(.*\)/\1/g"`

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

if [ -e "$PATH_TO_ME/$SCRIPTNAME.vmoptions" ]
then
	VMOPTIONS=`cat $PATH_TO_ME/$SCRIPTNAME.vmoptions`
	for OPTION in "$VMOPTIONS"
	do
		JAVAARGS="$JAVAARGS '${OPTION}'"
	done
fi

for ARG in "$@"
do
	if [ "${ARG:0:5}" == "-Jvm=" ]
	then
		JAVAPATH=${ARG:6};
	else
		if [ "${ARG:0:2}" == "-J" ]
		then
			JAVAARGS="$JAVAARGS '${ARG:2}'"
		else
			CMDARGS="$CMDARGS -f '$ARG'"
		fi
	fi
	shift;
	
done

## Assumption: all classes including rhino are packed into
## owltools-runner-all.jar
## These will be resolved into absolute pathnames
DEFAULT_OWLTOOLS_LOCATION=../bin/owltools-runner-all.jar
ALTERNATIVE_OWLTOOLS_LOCATION=../owltools-runner-all.jar

if [ -f  $PATH_TO_ME/$DEFAULT_OWLTOOLS_LOCATION ];
then
   CLASSPATH_RELATIVE=$DEFAULT_OWLTOOLS_LOCATION;
else
   if [ -f  $PATH_TO_ME/$ALTERNATIVE_OWLTOOLS_LOCATION ];
   then
      CLASSPATH_RELATIVE=$ALTERNATIVE_OWLTOOLS_LOCATION;
   else
      echo "Cannot find owltools-runner-all.jar in any of the expected locations";
      exit 1
   fi
fi

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 [ -z "$CLASSPATH" ]
		then
			CLASSPATH="$CP_ENTRY"
		else
			CLASSPATH="$CLASSPATH$PATH_SEP$CP_ENTRY"
		fi
	done
done

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

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

export OWLRUNNER_SOURCE=$PATH_TO_ME/owlrunner.js
## Construct the Java call
CMD="$JAVAPATH -Xmx4G -classpath $CLASSPATH -DlauncherDir=$PATH_TO_ME $JAVAARGS jline.ConsoleRunner $CLASSNAME -f $PATH_TO_ME/pachyowl.js -f $PATH_TO_ME/owlrunner.js $CMDARGS -f -"
echo $CMD

## Check for rlwrap, maintains a history and auto-completion for owlrhino
## Do not use which, it does not set proper exit codes for errors or non-existing application
#if command -v rlwrap >/dev/null 2>&1; then
#  CMD="rlwrap -C jsxx -C owlrhino $CMD"
#fi

echo $CMD
sh -c "$CMD"
