##############################################################################
##                                                                          ##
##  Groovy JVM Bootstrap for UN*X                                           ##
##                                                                          ##
##############################################################################

##
## $Revision: 4181 $ $Date: 2006-10-28 15:36:34 +0000 (Sat, 28 Oct 2006) $
##

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

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

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

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

earlyInit() {
	return
}
lateInit() {
	return
}

earlyInit

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

    Darwin*)
        darwin=true
        ;;
esac

if [ "$1" = "-cp" ] || [ "$1" = "-classpath" ]; then
  CP=$2
  shift 2
fi

ARGUMENTS=$@

# 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 "$GRIFFON_HOME" ] &&
        GRIFFON_HOME=`cygpath --unix "$GRIFFON_HOME"`
    [ -n "$JAVACMD" ] &&
        JAVACMD=`cygpath --unix "$JAVACMD"`
    [ -n "$JAVA_HOME" ] &&
        JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
    [ -n "$CP" ] &&
        CP=`cygpath --path --unix "$CP"`
fi

# Remove possible trailing slash (after possible cygwin correction)
GRIFFON_HOME=`echo $GRIFFON_HOME | sed -e 's|/$||g'`

# Locate GRIFFON_HOME if not it is not set
if [ -z "$GRIFFON_HOME" -o ! -d "$GRIFFON_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\"`/.."
  GRIFFON_HOME="`pwd -P`"
  cd "$SAVED"
fi

# Use default groovy-conf config
if [ -z "$GROOVY_CONF" ]; then
    GROOVY_CONF="$GRIFFON_HOME/conf/groovy-starter.conf"
fi
STARTER_CLASSPATH="$GRIFFON_HOME/lib/groovy-all-1.8.6.jar:$GRIFFON_HOME/dist/griffon-cli-0.9.5-rc2.jar:$GRIFFON_HOME/dist/griffon-rt-0.9.5-rc2.jar"

# Allow access to Cocoa classes on OS X
if $darwin; then
    STARTER_CLASSPATH="$STARTER_CLASSPATH:/System/Library/Java"
fi

# Create the final classpath
# Setting a classpath using the -cp or -classpath option means not to use
# the global classpath. Groovy behaves then the same as the java
# interpreter
if [ -n "$CP" ] ; then
    CP="$CP":.
elif [ -n "$CLASSPATH" ] ; then
    CP="$CLASSPATH":.
else
    CP=.
fi

# 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=$(which 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 [ "$MAX_FD_LIMIT" != "unlimited" ]; then
	    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
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 Darwin, add some dock integration in case we show a GUI
if $darwin; then
   JAVA_OPTS="-Xdock:name=Griffon -Xdock:icon=$GRIFFON_HOME/media/griffon.icns $JAVA_OPTS"
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
    GRIFFON_HOME=`cygpath --path --mixed "$GRIFFON_HOME"`
    JAVA_HOME=`cygpath --path --mixed "$JAVA_HOME"`
    GROOVY_CONF=`cygpath --path --mixed "$GROOVY_CONF"`
    CP=`cygpath --path --mixed "$CP"`
    TOOLS_JAR=`cygpath --path --mixed "$TOOLS_JAR"`
    STARTER_CLASSPATH=`cygpath --path --mixed "$STARTER_CLASSPATH"`
	# We build the pattern for arguments to be converted via cygpath
    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
    SEP=""
    for dir in $ROOTDIRSRAW; do
    	ROOTDIRS="$ROOTDIRS$SEP$dir"
	    SEP="|"
    done
    OURCYGPATTERN="(^($ROOTDIRS))"
    # Add a user-defined pattern to the cygpath arguments
    if [ "$GROOVY_CYGPATTERN" != "" ] ; then
    	OURCYGPATTERN="$OURCYGPATTERN|($GROOVY_CYGPATTERN)"
    fi
    # Now convert the arguments
	ARGUMENTS=""
    for arg in "$@" ; do
    	CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
    	if [ $CHECK -ne 0 ] ; then
	    	convArg=`cygpath --path --ignore --mixed "$arg"`
    	else
			convArg=$arg
    	fi
		ARGUMENTS="$ARGUMENTS $convArg"
    done
fi

STARTER_MAIN_CLASS=org.codehaus.griffon.cli.support.GriffonStarter

lateInit

STACKTRACE_FILTERS="groovy., org.codehaus.groovy., java., javax., sun., gjdk.groovy., gant., org.codehaus.gant."

startGriffon() {
  CLASS=$1
  shift
  JAVA_OPTS="-server -Xmx512M $JAVA_OPTS"
  # Start the Profiler or the JVM
  if $useprofiler; then
      runProfiler
  else
  	if [ $# -eq 0 ] ; then         # no argument given
         exec "$JAVACMD" $JAVA_OPTS \
          -classpath "$STARTER_CLASSPATH" \
          -Dprogram.name="$PROGNAME" \
          -Dgroovy.starter.conf="$GROOVY_CONF" \
          -Dgriffon.home="$GRIFFON_HOME" \
          -Dbase.dir="." \
          -Dtools.jar="$TOOLS_JAR" \
          -Dgroovy.sanitized.stacktraces="$STACKTRACE_FILTERS" \
          $STARTER_MAIN_CLASS \
          --main $CLASS \
          --conf "$GROOVY_CONF" \
          --classpath "$CP"
  	else
         exec "$JAVACMD" $JAVA_OPTS \
          -classpath "$STARTER_CLASSPATH" \
          -Dprogram.name="$PROGNAME" \
          -Dgroovy.starter.conf="$GROOVY_CONF" \
          -Dgriffon.home="$GRIFFON_HOME" \
          -Dbase.dir="." \
          -Dtools.jar="$TOOLS_JAR" \
          -Dgroovy.sanitized.stacktraces="$STACKTRACE_FILTERS" \
          $STARTER_MAIN_CLASS \
          --main $CLASS \
          --conf "$GROOVY_CONF" \
          --classpath "$CP" \
          "${ARGUMENTS}"
  	fi
  fi
}
