#!/bin/bash
# $Id$
#
#SCRIPT_LANG="unknown"
#SCRIPT_LANG="python"
SCRIPT_LANG="ruby"
#
DEBUG=0

## resolve links - $0 may be a link to home
PRG=$0

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

# try to dynamically determine JAS home
# (this script typically resides in JAS home)
if [ -z "$JAS_HOME" ] ; then
  if [ "$PRG" = "./jas" ] ; then
    # current dir is the script dir
    JAS_HOME_1=`pwd`
  else
    # current dir is not the script dir
    JAS_HOME_1=`dirname "$PRG"`
  fi
  if [ -f "$JAS_HOME_1"/examples/jas.rb -a -f "$JAS_HOME_1"/examples/jas.py ] ; then
    JAS_HOME="$JAS_HOME_1"
  else
    JAS_HOME=`dirname "$JAS_HOME_1"`
    if [ -f "$JAS_HOME"/examples/jas.rb -a -f "$JAS_HOME"/examples/jas.py ] ; then
       echo -n ""
    else
       JAS_HOME="/usr/share/jas"
    fi
  fi
fi

# verify that JAS home is found
if [ -d "$JAS_HOME/examples" ] ; then
   JAVA_LIBS=".:$JAS_HOME:$JAS_HOME/examples:$JAS_HOME/jas.jar"
elif [ -d "examples" ] ; then
   JAVA_LIBS=".:examples:jas.jar"
else
   echo "===================================" 
   echo "Error: examples directory not found" 
   echo "Please install JAS properly"
   echo "===================================" 
   exit 0
fi

#
# setup required libs: JAVA_LIBS="log4j*.jar:junit*.jar:jas.jar:examples"
#
if [ -f "../lib/log4j-core.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:../lib/log4j-core.jar"
   JAVA_LIBS="$JAVA_LIBS:../lib/log4j-api.jar"
elif [ -f "lib/log4j-core.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:lib/log4j-core.jar"
   JAVA_LIBS="$JAVA_LIBS:lib/log4j-api.jar"
elif [ -f "$JAS_HOME/../lib/log4j-core.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/../lib/log4j-core.jar"
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/../lib/log4j-api.jar"
elif [ -f /usr/share/java/log4j-core.jar ] ; then
   JAVA_LIBS="$JAVA_LIBS:`ls /usr/share/java/log4j-core.jar|tail -1`"
   JAVA_LIBS="$JAVA_LIBS:`ls /usr/share/java/log4j-api.jar|tail -1`"
elif [ -f "../lib/log4j.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:../lib/log4j.jar"
elif [ -f "lib/log4j.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:lib/log4j.jar"
elif [ -f "$JAS_HOME/../lib/log4j.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/../lib/log4j.jar"
else
   echo "==================================" 
   echo "Error: library log4j.jar not found" 
   echo "Please install log4j.jar" 
   echo "==================================" 
   exit 0
fi
#
if [ -f "../lib/junit-4.12.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:../lib/junit-4.12.jar"
   JAVA_LIBS="$JAVA_LIBS:../lib/hamcrest-core-1.3.jar"
elif [ -f "lib/junit-4.12.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:lib/junit-4.12.jar"
   JAVA_LIBS="$JAVA_LIBS:lib/hamcrest-core-1.3.jar"
elif [ -f "$JAS_HOME/../lib/junit-4.12.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/../lib/junit-4.12.jar"
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/../lib/hamcrest-core-1.3.jar"
elif [ -f `ls /usr/share/java/junit4*.jar|tail -1` ] ; then
   JAVA_LIBS=$JAVA_LIBS:`ls /usr/share/java/junit4*.jar|tail -1`
   JAVA_LIBS=$JAVA_LIBS:`ls /usr/share/java/hamcrest-core-*.jar|tail -1`
elif [ -f "../lib/junit.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:../lib/junit.jar"
elif [ -f "lib/junit.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:lib/junit.jar"
elif [ -f "$JAS_HOME/../lib/junit.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/../lib/junit.jar"
elif [ -f `ls /usr/share/java/junit*.jar|tail -1` ] ; then
   JAVA_LIBS=$JAVA_LIBS:`ls /usr/share/java/junit*.jar|tail -1`
else
   echo "====================================" 
   echo "Warning: library junit.jar not found" 
   echo "Please install junit.jar"
   echo "====================================" 
fi
#
if [ -d "edu/jas" ] ; then
   JAVA_LIBS="$JAVA_LIBS:."
elif [ -f "../lib/jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:../lib/jas.jar"
elif [ -f "lib/jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:lib/jas.jar"
elif [ -f "jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:jas.jar"
elif [ -f "$JAS_HOME/../lib/jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/../lib/jas.jar"
elif [ -f "$JAS_HOME/jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:$JAS_HOME/jas.jar"
elif [ -f `ls $JAS_HOME/jas*bin.jar|tail -1` ] ; then
   JAVA_LIBS=$JAVA_LIBS:`ls $JAS_HOME/jas*bin.jar|tail -1`
elif [ -f `ls /usr/share/java/jas*.jar|tail -1` ] ; then
   JAVA_LIBS=$JAVA_LIBS:`ls /usr/share/java/jas*.jar|tail -1`
else
   echo "===============================================" 
   echo "Error: library jas.jar or class files not found" 
   echo "Please install jas.jar or JAS classes" 
   echo "===============================================" 
   exit 0
fi
#
#JAVA_LIBS="$JAVA_LIBS:."
#
#JAVA_OPTS="-J-verbose:gc -J-Xms1100M -J-Xmx1900M"
#JAVA_OPTS="-J-verbose:gc -J-Xms350M -J-Xmx800M"
#JAVA_OPTS="-J-Xms350M -J-Xmx800M"
JAVA_OPTS=""
LANGOPT=0
SCRIPT_OPTS=
#
# check for scripting language
# Scanning for args is aborted by '--'.
#set -- $JAS_OPTS "$@"
#echo "jas_args: " $*
while [ $# -gt 0 ]
do
    case "$1" in
    # use python
    -py*|-jy*) 
         SCRIPT_LANG="python"
         # old: 
         LANGOPT=`jython -h 2>&1|grep '\-J\-'| grep -v '\-Jarg' -c`
         ;;
    # use ruby
    -rb*|-ruby|-jr*) 
         SCRIPT_LANG="ruby"
         ;;
    -h|--help)
         help_requested=true
         ;;
    -d)
         DEBUG=1
         ;;
    -v|--verbose)
         SCRIPT_OPTS="-v"
         ;;
    --) break ;;
    # Other opts go through  
    -*) jas_args=("${jas_args[@]}" "$1") ;;
    # Abort processing on first non-opt arg 
    *) jas_args=("${jas_args[@]}" "$1")
        break ;;
    esac
    shift
done
[[ $DEBUG > 0 ]] && echo "PRG      :" $PRG
[[ $DEBUG > 0 ]] && echo "JAS_HOME :" $JAS_HOME
[[ $DEBUG > 0 ]] && echo "JAVA_LIBS:" $JAVA_LIBS
[[ $DEBUG > 0 ]] && echo "LANGOPT  :" $LANGOPT

if [ "$SCRIPT_LANG" = "python" ]; then
   if [ -f "examples/jas.py" ] ; then
      echo -n "" 
   elif [ -f "$JAS_HOME/examples/jas.py" ] ; then
      echo -n "" 
   else 
      echo "===============================" 
      echo "Error: library jas.py not found" 
      echo "Please install jas.py" 
      echo "===============================" 
      exit 0
   fi
   TMPX=`which jython`
   if [[ -n $TMPX && -x $TMPX ]] ; then
      echo -n ""
   else 
      echo "==================================" 
      echo "Error: jython script not found" 
      echo "Please install jython in your PATH" 
      echo "==================================" 
      exit 0
   fi
fi

JASRB=
if [ "$SCRIPT_LANG" = "ruby" ]; then
   if [ -f "examples/jas.rb" ] ; then
      #JASRB="examples/jas.rb"
      JASRB="jas"
      echo -n "" 
   elif [ -f "$JAS_HOME/examples/jas.rb" ] ; then
      #JASRB="$JAS_HOME/examples/jas.rb"
      JASRB="jas"
      echo -n "" 
   else 
      echo "===============================" 
      echo "Error: library jas.rb not found" 
      echo "Please install jas.rb" 
      echo "===============================" 
      exit 0
   fi
   TMPX=`which jruby`
   if [[ -n $TMPX && -x $TMPX ]] ; then
      echo -n "" 
   else 
      echo "=================================" 
      echo "Error: jruby script not found" 
      echo "Please install jruby in your PATH" 
      echo "=================================" 
      exit 0
   fi
fi


#echo "jas_args: " $jas_args
# Put the jas_args back into the position arguments $1, $2 etc
set -- "${jas_args[@]}"

if [ -n "$help_requested" ] ; then
  echo "JAS options:" >&2
  echo "-py|-jy   : use Python scripting with jython" >&2
  echo "-rb|-jr   : use Ruby scripting with jruby" >&2
  echo "-d        : debug this script" >&2
  echo "-v        : verbose option for scripting language" >&2
  echo "-h|--help : this infos" >&2
  echo "--        : pass remaining arguments through to Jython|JRuby" >&2
  exit 0
fi

#
# run scripting language
#
if [ "$SCRIPT_LANG" = "python" ]; then
   if [[ $LANGOPT > 0 ]] ; then
      JAVA_LIBS="-J-cp $JAVA_LIBS"
      [[ $DEBUG > 0 ]] && echo JAVA_LIBS: $JAVA_LIBS
   else
      export CLASSPATH=$CLASSPATH:$JAVA_LIBS
      JAVA_LIBS="-Dpython.path=$JAVA_LIBS"
      export JAVA_OPTIONS="${JAVA_OPTS//-J/}"
      [[ $DEBUG > 0 ]] && echo CLASSPATH: $CLASSPATH
      [[ $DEBUG > 0 ]] && echo JAVA_OPTIONS: $JAVA_OPTIONS
      [[ $DEBUG > 0 ]] && echo JAVA_LIBS: $JAVA_LIBS
   fi
   #[[ $DEBUG > 0 ]] && jython --print $SCRIPT_OPTS "$JAVA_LIBS" $JAVA_OPTS $* 
   [[ $DEBUG > 0 ]] && echo jython $SCRIPT_OPTS $JAVA_LIBS $JAVA_OPTS $* 
   jython $SCRIPT_OPTS $JAVA_LIBS $JAVA_OPTS $*
elif [ "$SCRIPT_LANG" = "ruby" ]; then
   if [[ "$SCRIPT_OPTS" = "-v" ]]; then
      SCRIPT_OPTS="-d -w -W"
   fi
   [[ $DEBUG > 0 ]] && echo jruby $SCRIPT_OPTS -J-cp "$JAVA_LIBS" $JAVA_OPTS -S jirb --noinspect --readline -r "$JASRB" $*
   jruby $SCRIPT_OPTS -J-cp "$JAVA_LIBS" $JAVA_OPTS -S jirb --noinspect --readline -r "$JASRB" $*
else 
   echo "===========================================" 
   echo "Unknown scripting language $SCRIPT_LANG" 
   echo "===========================================" 
   exit 0
fi
#
