#!/bin/sh
# -*- ksh -*-
# $Id: gasgano,v 1.25 2012-05-24 14:30:52 vforchi Exp $

# Standard settings
#----------------------------------------
thisprog=`basename $0`
progdir=`dirname $0`
currentdir=`pwd`
cd $progdir/..
BASE=`pwd`

MIN_VERSION=1.6     # minimum Java version required to run Gasgano


# Set system-dependant options
#----------------------------------------

OS=`uname`
case $OS in 
  
  SunOS) optNoJIT="-Djava.compiler=NONE" 
	 ECHO='echo'
	 optJIT="" 
	 ;;

  Linux) optNoJIT=""
	 optJIT=""
	 # From the RedHat 7.1 release notes:
	 # Some Java JVMs (both from Sun and IBM) don't work with the
	 # new floating stack feature of the i686 version of glibc.
	 # The failures are due to programming assumptions in the JVMs
	 # that are now invalid.  JVM vendors are working on making
	 # the necessary corrections.  Until corrected JVM packages
	 # are available, you may force glibc to use the deprecated
	 # stack model by setting the following environment variable:
	 # LD_ASSUME_KERNEL=2.2.5
	 # LD_ASSUME_KERNEL=2.2.5 ; export LD_ASSUME_KERNEL # RedHat 7.1
	 ECHO='echo -e'
	 ;;

  HP-UX) optNoJIT=""
	 optJIT="" 
	 ECHO='echo'
	 ;;

  Darwin) optNoJIT=""
	  optJIT="" 
	  ECHO='echo'
	  ;;

  *)     echo 'Usupported OS: $OS' ; exit 1 ;;
esac

# Usage strings
#----------------------------------------
usage0="usage: $thisprog <options>\nwhere <options> are:"


options0="\n\t-help\n
\t-debug"

options1="\n\t-help\t\tDisplay this help text
\n\t-debug\t\tDisplay debugging information
"

usage="$usage0$options0"
helptext="$usage0$options1"

# Set default options and process 
# command line switches
#----------------------------------------

optHeap=1024			# by default, heap is 100 meg
optDebug=N			# by default no debugging options

options=$*			# save user options

while true; do			# Loop over input options
  case $1
    in
    -debug)	optDebug=Y ;;
    -help)	$ECHO $helptext ; exit ;;
    -*)		$ECHO $usage ; exit ;; # trap all other options
    *)		break ;;
  esac
  shift
done
opt=$*
# DS MAR 2007 - the documented option is -Xmx...
## optjre="$optjre -mx${optHeap}m"		# Needed for (eg) paramfile Parameters
optjre="$optjre -Xmx${optHeap}m"		# Needed for (eg) paramfile Parameters

if [ $optDebug = "N" ] ; then
  OUT_CHANNEL=""
  optjre="$optjre $optJIT"
else
  OUT_CHANNEL=""
  optjre="$optjre -Ddebug.mode=true $optNoJIT"
fi

# The OCA Parser uses log4j so set the location of the configuration file
if [ $optDebug = "Y" ] ; then
  optjre="$optjre -Dlog4j.debug -Dlog4j.configuration=file:$BASE/config/log4j-debug.properties"
else
  optjre="$optjre -Dlog4j.configuration=file:$BASE/config/log4j-nodebug.properties"
fi

#----------------------------------------
# Unset the JAVA_HOME. Not needed.
#----------------------------------------

unset JAVA_HOME

#----------------------------------------
# Find out which JRE we use
# Try first the system JRE but only if it is 1.1.8, 
# otherwise the distributed one.
#----------------------------------------
JREEXE=java

if [ -x $BASE/../jre/bin/$JREEXE ] ; then

  # We prefer the locally installed JRE
  #----------------------------------------
  cd $BASE/../jre
  JREBASE=`pwd`

elif [ -n "$JAVA_HOME" ] ; then

  # Use the VLT standard installation
  #----------------------------------------
  JREBASE=$JAVA_HOME

else

  jre=`which $JREEXE`
  if [ $? -ne 0 ]; then
    # Give up!
    #----------------------------------------
    $ECHO "No Java Runtime Environment found: exiting"
    exit 1
  fi

  if [ -x $jre ] ; then
 
    # Use the system-wide installation
    #----------------------------------------
    cd `dirname $jre`/..
    JREBASE=`pwd`

  else

    # Give up!
    #----------------------------------------
    $ECHO "No Java Runtime Environment found: exiting"
    exit 1
  fi

fi

JRE=$JREBASE/bin/$JREEXE

# Setup CLASSPATH for different
# runtime environments
#----------------------------------------
CLASSPATH=$CLASSPATH:$BASE/share/dfs.jar
CLASSPATH=$CLASSPATH:$BASE/share/gasgano.jar
CLASSPATH=$CLASSPATH:$BASE/share/javacpl.jar
CLASSPATH=$CLASSPATH:$BASE/share/gnu-regexp.jar
CLASSPATH=$CLASSPATH:$BASE/share/jalString.jar
CLASSPATH=$CLASSPATH:$BASE/share/oca.jar
CLASSPATH=$CLASSPATH:$BASE/share/log4j.jar
CLASSPATH=$CLASSPATH:$BASE/share/fits.jar
CLASSPATH=$CLASSPATH:$BASE/share/dfsBetUtil.jar

export CLASSPATH

# Setup the LD_LIBRARY_PATH to include
# libcplgasgano.so
#----------------------------------------
#
# The cpldir setting is set by the pipeline installation
# script - it can be edited by hand, but should not be
# renamed.
#
if [ -z "$CPLDIR" ]; then
  CPLDIR=$HOME/cpl
fi

case $OS in
HP-UX)
  SHLIB_PATH=$CPLDIR/lib:$BASE/lib:$SHLIB_PATH
  export SHLIB_PATH
  ;;

Darwin)
  DYLIB_LIBRARY_PATH=$CPLDIR/lib:$BASE/lib:$DYLIB_LIBRARY_PATH
  export DYLIB_LIBRARY_PATH

  optjre="$optjre -Djava.library.path=$DYLIB_LIBRARY_PATH"
  ;;

Linux)
  LD_LIBRARY_PATH=$CPLDIR/lib64:$CPLDIR/lib:$BASE/lib:$LD_LIBRARY_PATH
  export LD_LIBRARY_PATH

  optjre="$optjre -classpath $CLASSPATH"
  ;;

*)
   LD_LIBRARY_PATH=$CPLDIR/lib:$BASE/lib:$LD_LIBRARY_PATH
   export LD_LIBRARY_PATH
   ;;
esac

# Print out some useful information
#----------------------------------------

if [ $optDebug = "Y" ] ; then
  $ECHO "$NOW date:             `date`"
  $ECHO "$NOW jre:		$jre"
  $ECHO "$NOW JRE:		$JRE"
  $ECHO "$NOW BASE:		$BASE"
  $ECHO "$NOW options:		$options"
  $ECHO "$NOW optjre:		$optjre"
  $ECHO "$NOW CLASSPATH:"
  $ECHO $CLASSPATH | tr ":" "\n"
fi


################################################################################
#
# Now finally start gasgano
#
################################################################################
#
# From the original gasgano script
#
UNIX95=1
export UNIX95

#
# Finally return to the current dir
#
cd $currentdir


#
# Checks echo command which option uses to get NO NEW LINE
#
echo=echo
if [ "`echo -n `" = "-n" ] ; then
        SV_NONL="\c"
else
        echo="echo -n"
fi


#----------------------------------------
# Exit if DISPLAY variable is not set
#----------------------------------------

if [ -z "$DISPLAY" ];  then
  $ECHO "DISPLAY environment variable not set - exiting"
  exit 1
fi

#----------------------------------------
# Try to run Gasgano
#----------------------------------------

# DS MAR 2007
# DFS03497 - Gasgano stack overflows 
# Increase C threads stack to avoid overflow: 
optjre="$optjre -Xss2048k "

eval $JRE $optjre org.eso.gasgano.Gasgano $opt $OUT_CHANNEL
