#!/bin/sh
# This single shell script should support both the generic
# distribution which doesn't includes a jre, and all other
# distributions which do include a jre.  Search order
# is $JAVA_HOME, distributed jre, java/jre in path, 
# and finally common locations.
#
# NOTE: If you want to set which java to use, and bypass the
# searching, just set the JAVA_HOME environment variable.
#
# 3/02/98: Support many of the java options on the command
#          line.  (ie "aim -nojit" will work correctly now)
# 2/20/98: AIM doesn't work "well" with 1.1.[012] so we
#          no longer search for them.
##########################################################

##########################################################
# This routine actual runs the jre or java.
# $JAVA_ARGS arguments to send to java or jre
# $JAVA_HOME where bin/java or bin/jre resides
# $O_ARGS    arguments for main.Oscar 
# $O_HOME    where oscar is installed
##########################################################
runit () 
{

#   echo "JAVA_ARGS = $JAVA_ARGS"
#   echo "JAVA_HOME = $JAVA_HOME"
#   echo "O_ARGS = $O_ARGS"
#   echo "O_HOME = $O_HOME"

   # Make sure JAVA_HOME is really set
   if [ ! -d "$JAVA_HOME" ] ; then
      echo "Invalid JAVA_HOME: $JAVA_HOME" 1>&2 
      echo "Make sure java is in your path or \$JAVA_HOME is set" 1>&2
      exit 1
   fi

   # Check for a full java install, must have one of these.
   if test ! -r "$JAVA_HOME/lib/classes.zip" && \
           test ! -r "$JAVA_HOME/lib/rt.jar" && \
           test ! -d "$JAVA_HOME/classes" ; then
      echo "$JAVA_HOME doesn't seem to contain a full Java install." 1>&2 
      exit 1
   fi

   # Create home directory config area
   if [ ! -d "$HOME/.aim" ] ; then
       echo "Creating $HOME/.aim directory"
       mkdir $HOME/.aim
       chmod og-rwx $HOME/.aim
       cp $O_HOME/AIM.cfg $HOME/.aim/
       cp $O_HOME/Info.sig $HOME/.aim/
   fi


   # Use java before jre, since some jre's are broken (HP, IRIX)
   if [ -x $JAVA_HOME/bin/java ] ; then
      exec $JAVA_HOME/bin/java -classpath $O_HOME/AIM.jar:$JAVA_HOME/lib/classes.zip:$JAVA_HOME/classes:$JAVA_HOME/lib/rt.jar main.Oscar -root $O_HOME $O_ARGS
   else
      exec $JAVA_HOME/bin/jre -cp $O_HOME/AIM.jar main.Oscar -root $O_HOME $O_ARGS
   fi
}

##########################################################
# Main
##########################################################
# Solaris yuck.
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/dt/lib"
export LD_LIBRARY_PATH

#Look and save command line args, including java args.
for arg in $* ; do
   case "$arg" in
   -nojit)     JAVA_ARGS="$JAVA_ARGS $arg" ;;
   -ms*)       JAVA_ARGS="$JAVA_ARGS $arg" ;;
   -mx*)       JAVA_ARGS="$JAVA_ARGS $arg" ;;
   -verbosegc) JAVA_ARGS="$JAVA_ARGS $arg" ;;
   *)          O_ARGS="$O_ARGS $arg" ;;
   esac
done

ARGS=$*

# Find where AIM lives.  Did they give the full path to aim?
if [ -x $0 ] ; then 
   O_HOME=`dirname $0`
else 
   PRG=`which $0`
   O_HOME=`dirname $PRG`
fi

# Check for full Oscar install, must have ALL of these.
if test ! -r "$O_HOME/AIM.jar" || \
   test ! -d "$O_HOME/uipacks" || \
   test ! -d "$O_HOME/modules" ; then

   echo "'$O_HOME' doesn't seem to contain a full AIM install." 1>&2 
   exit 1
fi

# If you set JAVA_HOME then we skip all of this
if [ -n "$JAVA_HOME" ]; then
   runit
fi

# First check for a jre with the distribution
TRY="jre1.1.5 jre1.1.4 jre1.1.3"

for t in $TRY; do
   if test -d "$O_HOME/$t" ; then
       JAVA_HOME="$O_HOME/$t"
       runit
   fi
done

# Now check our path for jre.
WHICH=`which jre`
if [ -x "$WHICH" ]; then
   JAVA_HOME=`dirname $WHICH`
   JAVA_HOME=`cd $JAVA_HOME/.. ; /bin/pwd`
   if test  -r "$JAVA_HOME/lib/classes.zip" || \
           test  -r "$JAVA_HOME/lib/rt.jar" || \
           test  -d "$JAVA_HOME/classes" ; then
      runit
   fi
fi

# Now check our path for java.
WHICH=`which java`
if [ -x "$WHICH" ]; then
   JAVA_HOME=`dirname $WHICH`
   JAVA_HOME=`cd $JAVA_HOME/.. ; /bin/pwd`
   if test  -r "$JAVA_HOME/lib/classes.zip" || \
           test  -r "$JAVA_HOME/lib/rt.jar" || \
           test  -d "$JAVA_HOME/classes" ; then
      runit
   fi
fi

# Doh! No jre or java in path, try some standard install locations
TRY="/usr/local/jre1.1.5 /usr/local/jdk1.1.5 \
     /usr/local/jre1.1.4 /usr/local/jdk1.1.4 \
     /usr/local/jre1.1.3 /usr/local/jdk1.1.3 \
     /usr/local/java /usr/java /opt/java"

for t in $TRY; do
  if test -d $t ; then
     if test -x $t/bin/jre || test -x $t/bin/java ; then
        JAVA_HOME=$t
        runit
     fi
  fi
done

echo "I tried to guess where java is installed, but I still couldn't find it."
echo "You must add java or jre to your path, or set JAVA_HOME."
exit 1

