#!/bin/bash
# -----------------------------------------------------------------------------
# jruby-ng - Start Script for the JRuby interpreter, using NailGun
#
#   JRUBY_HOME    (Optional) May point at your JRuby "build" directory.
#                 If not present, the current working directory is assumed.
#
# -----------------------------------------------------------------------------

cygwin=false

# ----- Identify OS we are running under --------------------------------------
case "`uname`" in
CYGWIN*) cygwin=true
esac

# ----- Verify and Set Required Environment Variables -------------------------

if [ -z "$JRUBY_HOME" ] ; then
  ## resolve links - $0 may be a link to  home
  PRG=$0
  progname=`basename "$0"`
  
  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
  
  JRUBY_HOME_1=`dirname "$PRG"`           # the ./bin dir
  JRUBY_HOME_1=`dirname "$JRUBY_HOME_1"`  # the . dir
  if [ -d "${JRUBY_HOME_1}/lib" ] ; then 
    JRUBY_HOME="${JRUBY_HOME_1}"
  fi
else
  if $cygwin; then
    JRUBY_HOME=`cygpath -u "$JRUBY_HOME"`
  fi
fi

# ----- Set Up JRUBY_BASE If Necessary -------------------------------------

if [ -z "$JRUBY_BASE" ] ; then
  JRUBY_BASE="$JRUBY_HOME"
fi

# ----- Execute The Requested Command -----------------------------------------

if $cygwin; then
  JAVA_HOME=`cygpath --mixed "$JAVA_HOME"`
  JRUBY_BASE=`cygpath --mixed "$JRUBY_BASE"`
  JRUBY_HOME=`cygpath --mixed "$JRUBY_HOME"`
  JRUBY_SHELL=`cygpath --mixed "$JRUBY_SHELL"`
fi

exec $JRUBY_HOME/tool/nailgun/ng org.jruby.util.NailMain $JRUBY_OPTS "$@"
