#!/bin/sh

SOURCE="$0"
while [ -h "$SOURCE" ] ; do
  NEXTSOURCE="$(readlink "$SOURCE")"
  echo $NEXTSOURCE | grep -q -e "^/"
  if [ $? = 0 ]; then
    SOURCE="$NEXTSOURCE"
  else
    SOURCE="$(dirname $SOURCE)/$NEXTSOURCE"
  fi
done
# Get a Windows path under MinGW or Cygwin
BASEPATH="$( cd -P "$( dirname "$SOURCE" )" && (pwd -W 2>/dev/null || cygpath -w $(pwd) 2>/dev/null || pwd))"
if [ "${BASEPATH%${BASEPATH#?}}" != "/" ] ; then
  BASEPATH="$( echo "$BASEPATH" | sed -e 's@\\@/@g' )"
fi

if test "$1" = "--debug"
then
    CMD="org.mozilla.javascript.tools.debugger.Main -debug -opt -1"
else
    CMD="org.mozilla.javascript.tools.shell.Main"
fi

if test "$1" = "-T"
then
    cd -P "$(dirname "$SOURCE")"
    java -classpath "${BASEPATH}/rhino/js.jar" ${CMD} -opt -1 -modules "${BASEPATH}/lib" -modules "${BASEPATH}/node_modules" -modules "${BASEPATH}/rhino" -modules "${BASEPATH}" "${BASEPATH}/jsdoc.js" "$@"

else
    java -classpath "${BASEPATH}/rhino/js.jar" ${CMD} -modules "${BASEPATH}/lib" -modules "${BASEPATH}/node_modules" -modules "${BASEPATH}/rhino" -modules "${BASEPATH}" "${BASEPATH}/jsdoc.js" "$@"
fi
