
#  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.

#  Type of Module:
#     Shell script.

#  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:
#
#        - `dirname $0`/../../java/jre/bin/java
#        - `dirname $0`/../../java/bin/java
#        - $STARLINK/java/jre/bin/java
#        - $STARLINK/java/bin/java
#        - /star/java/jre/bin/java
#        - /star/java/bin/java
#        - anywhere on the PATH
#     
#     So this presumes a either a JDK installed into /star/java or an JRE
#     installed into /star/java/jre (which could be a softlink to a versioned
#     directory). 
#
#     For development purposes it is possible to override 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.

#  Copyright:
#     Copyright (C) 2001-2004 Central Laboratory of the Research Councils.
#     Copyright (C) 2005-2006 Particle Physics and Astronomy Research Council.
#     All Rights Reserved.

#  Authors:
#     PWD: P.W. Draper (Starlink, Durham University)
#     MBT: Mark Taylor (Starlink, Bristol University)
#     TIMJ: Tim Jenness (JAC, Hawaii)
#     {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.
#     17-JUN-2005 (MBT):
#        Added a getout flag to prevent additional configuration.
#     18-DEC-2006 (TIMJ):
#        - Recognize x86_64 and amd64 architectures.
#        - Support jrehome and jdkhome files.
#     20-DEC-2006 (PWD):
#        Remove jrehome and jdkhome use, back to 02-OCT-2002 behaviour.
#        Now also look for java in a relative position to this script
#        and check for JDKs as well as JREs. The JDK lives in /star/java
#        not /star/java/jdk as previously presumed.
#     {enter_further_changes_here}

#-

#.

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

else

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

   #  If installed in a tree then should be able to locate the correct
   #  java in a relative position. Make actual name absolute.
   scriptdir=`dirname "$0"`
   if test -f "$scriptdir/../../java/jre/bin/java"; then
      basedir=`cd "$scriptdir/../../" && pwd`
      java="$basedir/java/jre/bin/java"
   elif test -f "$scriptdir/../../java/bin/java"; then
      basedir=`cd "$scriptdir/../../" && pwd`
      java="$basedir/java/bin/java"
   else
      #  If $STARLINK is defined then try that.
      if test "$STARLINK" != ""; then
         if test -f "$STARLINK/java/jre/bin/java"; then
            java="$STARLINK/java/jre/bin/java"
         elif test -f "$STARLINK/java/bin/java"; then
            java="$STARLINK/java/bin/java"
         fi
      fi

      #  If still not defined, look for Java in the standard installation.
      if test "$java" = "java"; then
         if test -f "/star/java/jre/bin/java"; then
            java="/star/java/jre/bin/java"
         elif test -f "/star/java/bin/java"; then
            java="/star/java/bin/java"
         fi
      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
       ;;
   amd64 | x86_64)
       arch=amd64
       ;;
   sparc*)
       arch=sparc
       ;;
   sun4*)
       arch=sparc
       ;;
   Power*)
       arch=ppc
       ;;
   CYGWIN* | i86pc)
       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  

if test -z "$STARJAVA_DEFAULT_LOGGING"
then
   props="$props -Djava.util.logging.config.file=$bindir/../etc/logging.properties"
fi

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