#!/bin/sh
##############################################################################
##                                                                          ##
##  Groovy JVM Bootstrap for UN*X                                           ##
##                                                                          ##
##############################################################################

##
## $Revision: 1.7 $ $Date: 2005/04/05 21:27:38 $
##

PROGNAME=`basename "$0"`
DIRNAME=`dirname "$0"`

# Use the maximum available, or set MAX_FD != -1 to use that
MAX_FD="maximum"

# The version of classworlds to boot with
CLASSWORLDS_VERSION="1.1-alpha-2-SNAPSHOT-NO-SYSTEM-EXIT"

warn() {
    echo "${PROGNAME}: $*"
}

die() {
    warn "$*"
    exit 1
}

# OS specific support (must be 'true' or 'false').
cygwin=false;
darwin=false;
case "`uname`" in
    CYGWIN*)
        cygwin=true
        ;;
        
    Darwin*)
        darwin=true
        ;;
esac

# Attempt to set JAVA_HOME if it's not already set
if [ -z "$JAVA_HOME" ]; then
	
	# Set JAVA_HOME for Darwin
	if $darwin; then
		
		[ -z "$JAVA_HOME" -a -d "/Library/Java/Home" ] &&
			export JAVA_HOME="/Library/Java/Home"
			
		[ -z "$JAVA_HOME" -a -d "/System/Library/Frameworks/JavaVM.framework/Home" ] &&
			export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Home"
		
	fi
	
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
    [ -n "$GROOVY_HOME" ] &&
        GROOVY_HOME=`cygpath --unix "$GROOVY_HOME"`
    [ -n "$JAVACMD" ] &&
        JAVACMD=`cygpath --unix "$JAVACMD"`
    [ -n "$JAVA_HOME" ] &&
        JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# Locate GROOVY_HOME if not it is not set
if [ -z "$GROOVY_HOME" -o ! -d "$GROOVY_HOME" ] ; then
  # resolve links - $0 may be a link to groovy's home
  PRG="$0"

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
    else
    PRG=`dirname "$PRG"`"/$link"
    fi
  done

  SAVED="`pwd`"
  cd "`dirname \"$PRG\"`/.."
  GROOVY_HOME="`pwd -P`"
  cd "$SAVED"
fi


# Use default grok-classworlds config
if [ -z "$CLASSWORLDS_CONF" ]; then
    CLASSWORLDS_CONF="$GROOVY_HOME/conf/grok-classworlds.conf"
fi
CLASSWORLDS_CLASSPATH="$GROOVY_HOME/lib/classworlds-${CLASSWORLDS_VERSION}.jar"

# Determine the Java command to use to start the JVM
if [ -z "$JAVACMD" ]; then
    if [ -n "$JAVA_HOME" ]; then
        if [ -x "$JAVA_HOME/jre/sh/java" ]; then
            # IBM's JDK on AIX uses strange locations for the executables
            JAVACMD="$JAVA_HOME/jre/sh/java"
        else
            JAVACMD="$JAVA_HOME/bin/java"
        fi
    else
        JAVACMD="java"
    fi
fi
if [ ! -x "$JAVACMD" ]; then
    die "JAVA_HOME is not defined correctly; can not execute: $JAVACMD"
fi
if [ -z "$JAVA_HOME" ] ; then
    warn "JAVA_HOME environment variable is not set"
fi

# Increase the maximum file descriptors if we can
if [ "$cygwin" = "false" ]; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ $? -eq 0 ]; then
        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
            # use the businessSystem max
            MAX_FD="$MAX_FD_LIMIT"
        fi
        
        ulimit -n $MAX_FD
        if [ $? -ne 0 ]; then
            warn "Could not set maximum file descriptor limit: $MAX_FD"
        fi
    else
        warn "Could not query businessSystem maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi

# Setup Profiler
useprofiler=false
if [ "x$PROFILER" != "x" ]; then
    if [ -r "$PROFILER" ]; then
        . $PROFILER
        useprofiler=true
    else
        die "Profiler file not found: $PROFILER"
    fi
fi

# For Darwin, use classes.jar for TOOLS_JAR
TOOLS_JAR="$JAVA_HOME/lib/tools.jar"
if $darwin; then
    TOOLS_JAR="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Classes/classes.jar"
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
    GROOVY_HOME=`cygpath --path --windows "$GROOVY_HOME"`
    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
    CLASSWORLDS_CONF=`cygpath --path --windows "$CLASSWORLDS_CONF"`
    CLASSWORLDS_CLASSPATH=`cygpath --path --windows "$CLASSWORLDS_CLASSPATH"`
    TOOLS_JAR=`cygpath --path --windows "$TOOLS_JAR"`

fi

# Start the Profiler or the JVM
if $useprofiler; then
    runProfiler
else
    exec "$JAVACMD" $JAVA_OPTS \
        -classpath "$CLASSWORLDS_CLASSPATH" \
        -Dprogram.name="$PROGNAME" \
        -Dclassworlds.conf="$CLASSWORLDS_CONF" \
        -Dgroovy.home="$GROOVY_HOME" \
        -Dtools.jar="$TOOLS_JAR" \
        org.codehaus.classworlds.Launcher grok "$@"
fi
