
#  N.B. the previous line should be blank.
#+
#  Name:
#     starjava

#  Purpose:
#     Locate and run the Starlink installed "java" command, using installed
#     native libraries, with given arguments.

#  Description:
#     This script should be used as the "java" command when starting a
#     Starlink program. It does the following things: 
#        - locates a good JVM to use
#        - sets the LD_LIBRARY_PATH to pick up the right versions of 
#          shared libraries
#        - configures logging properties in a sensible way
#        - defines the SOAP contact directory.
#     
#     The preferred "java" command is Starlink one; this increases the 
#     likelihood that programs are only executed by JVMs that they have 
#     been tested against.  The "java" command used is searched for 
#     in the following order:
#
#        - $STARLINK/java/jre/bin/java
#        - /star/java/jre/bin/java
#        - anywhere on the PATH
#
#     For development purposes it is possible to overide this
#     behaviour and force the use of a specific "java" command.
#     To do this define the variable STAR_JAVA to point at a "java"
#     command before starting this script.
#
#     Any existing LD_LIBRARY_PATH has the appropriate architecture-specific 
#     one in the lib directory prepended.  For development purposes
#     this can be overridden by setting STAR_JAVA_LD_PATH to a 
#     preferred directory for native libraries.

#  Type of Module:
#     Shell script.

#  Copyright:
#     Copyright (C) 2001-2002 Central Laboratory of the Research Councils

#  Authors:
#     PWD: P.W. Draper (Starlink, Durham University)
#     MBT: Mark Taylor (Starlink, Bristol University)
#     {enter_new_authors_here}

#  History:
#     24-OCT-2001 (PWD):
#        Original version.
#     02-OCT-2002 (PWD):
#        Changed to look for a "jre" link in "/star/java" rather than 
#        examining the contents of the "/star/java/jrehome" file.
#     22-NOV-2002 (MBT):
#        Added manipulation of LD_LIBRARY_PATH.
#     27-NOV-2002 (MBT):
#        Added logging configuration
#     21-APR-2004 (BC):
#        Added support for Power Macintosh
#     21-OCT-2004 (PWD):
#        Added support for Cygwin.
#     16-DEC-2004 (PWD):
#        Added adam.user property to define ADAM_USER with the JVM.
#     {enter_further_changes_here}

#  Bugs:
#     {note_any_bugs_here}

#-

#.

#  Development version of "java".
if test "$STAR_JAVA" != ""; then
   java="$STAR_JAVA"

else

   #  Fall-back "java" is on current PATH.
   java="java"

   #  If $STARLINK is defined then try that. Only look for the JRE
   #  command (only need JDK for development, use "STAR_JAVA" to
   #  override).
   if test "$STARLINK" != ""; then
      if test -r "$STARLINK/java/jre/bin/java"; then
         java="$STARLINK/java/jre/bin/java"
      fi
   fi

   #  If still not changed, look for "java" in the standard installation.
   if test "$java" = "java"; then
      if test -r "/star/java/jre/bin/java"; then
         java="/star/java/jre/bin/java"
      fi
   fi
fi

#  Now set the path for loading native libraries.
#  First determine the architecture - this should be the same value which
#  a java call to the method System.getProperty("os.arch") would return
#  (the correct thing to do would be to ask java the value of this, but
#  it would be slow, so we use a simple lookup).
case "`uname -m`" in
   i[3-7]86 | ia32 | ia64)
       arch=i386
       ;;
   sparc*)
       arch=sparc
       ;;
   sun4*)
       arch=sparc
       ;;
   Power*)
       arch=ppc
       ;;
   CYGWIN*)
       arch=x86
       ;;
   *)
       arch="`uname -m`"
       ;;
esac

#  Identify the location of this file.
bindir="`dirname $0`"

#  Find the architecture-specific lib directory relative to it.
arch_lib_dir="$bindir/../lib/$arch"

#  Prefix this to the LD_LIBRARY_PATH.
if test -z "$LD_LIBRARY_PATH"; then
   LD_LIBRARY_PATH="$arch_lib_dir"
else
   LD_LIBRARY_PATH="${arch_lib_dir}:${LD_LIBRARY_PATH}"
fi

#  Prefix any development directories.
if test ! -z "$STAR_JAVA_LD_PATH"; then
   LD_LIBRARY_PATH="${STAR_JAVA_LD_PATH}:${LD_LIBRARY_PATH}"
fi

#  Export it.
export LD_LIBRARY_PATH

#  Set any necessary properties.
props=""
if test -z "$ADAM_USER"; then
  if test -d "$HOME/adam"; then
     props="-Dadam.user=${HOME}/adam"
  fi
else
  props="-Dadam.user=${ADAM_USER}"
fi  
props="$props -Djava.util.logging.config.file=$bindir/../etc/logging.properties"

#  Execute the java command with the command line arguments.
$java $props ${1+"$@"}
