#!/bin/sh
#
#   sceneviewer
#   Unix platform run script
#
#   Copyright 2008
#   Computational Dynamics, adapco, Computational Dynamics North America
#   All Rights Reserved
#

# Note that "if 0" evaluates to true!
is_java_ok() {
   if [ ! -r "$1" ]; then
       return 1  # false
   fi

   JDK_VERSION=`$1/bin/java -version 2>&1 | sed 's/.*"\(.*\)".*/\1/;q'`
   case "$JDK_VERSION" in
       1.4* | 1.5* )
           return 1  # false
       ;;
   esac
   return 0 # true
}

setup_for_dev() {
   mode="client"  # Need this so that the Java environment gets setup correctly
   setup_environment
   setup_client

   jardir=$STARBIN/../lib/java/platform/modules/ext
   CLASSPATH=$jardir/vtk.jar:$jardir/vtkpanel.jar:$jardir/scenefile.jar:$jardir/sceneviewer.jar
   export CLASSPATH
   JAVA_HOME=$JDK_HOME
   export JAVA_HOME
}

setup_standalone() {
   # In this mode, everything is in one directory:  VTK shared libraries,
   # the jar files (see CLASSPATH), and the starenv, mapjdk, maparch,
   # getjdk, getarch scripts.
   JAVA=`$SCRIPTDIR/mapjdk`
   if is_java_ok $JAVA; then
       JAVA_HOME=$JAVA
       export JAVA_HOME
   else
       if is_java_ok $JAVA_HOME; then
           :
       else
           if [ -f "$JAVA_HOME" ]; then
               echo "Found Java using JAVA_HOME, but need Java 1.6 (JRE6) or later."
           else
               echo "Could not find Java, please install Java 1.6 (JRE6) or later, or set JAVA_HOME."
           fi
           exit 1
       fi
   fi

   LD_LIBRARY_PATH=$STARBIN
   export LD_LIBRARY_PATH
   CLASSPATH=$SCRIPTDIR/../vtk.jar:$SCRIPTDIR/../vtkpanel.jar:$SCRIPTDIR/../scenefile.jar:$SCRIPTDIR/../sceneviewer.jar
   export CLASSPATH
}

checkLinuxArchitecture() {
  IS_ARCH_SUPPORTED=0
  case `uname` in
    Linux)
      case `uname -m` in
        ia64)
        IS_ARCH_SUPPORTED=-1
        ;;
      esac
  esac
}

checkHostArchitecture() {
  STAR_CONFIG_DIR=`dirname "$0"`
  DEV_HOME=`cd $STAR_CONFIG_DIR/../.. && pwd`
  STAR_ARCH=`$DEV_HOME/star/bin/getarch`
  IS_ARCH_SUPPORTED=0
  case "$STAR_ARCH" in
    aix-powerpc-5.1|aix-powerpc-5.2|aix-powerpc-5.3|hpux-ia64-11.23|sunos-sparc-*)
    IS_ARCH_SUPPORTED=-1
    ;;
  esac
}

main() {
   # run exit_hook on exit
   trap 'eval $exit_hook' 0
   # having one trap statement for EXIT does not cut it using the Korn shell
   # introduction of specialisations seems to solve the problem.
   trap 'eval $exit_hook; echo "`basename $0` Interrupted"; exit_hook=; exit' 2
   trap 'eval $exit_hook; echo "`basename $0` Terminated"; exit_hook=; exit' 15
   trap 'eval $exit_hook; echo "`basename $0` Aborted"; exit_hook=; exit' 6

   checkLinuxArchitecture 
   #checkHostArchitecture 
   if [ "$IS_ARCH_SUPPORTED" -eq "-1" ]; then
    echo_error "Starview+ is not supported on this platform."\
               "Microsoft Windows and Linux-x86 are the only platforms that are supported."
     exit 1
   fi

   process_arguments "$@"

   if [ "$isStandalone" -eq 0 ]; then
       setup_for_dev
   else
       setup_standalone
   fi

   jvmArgs=$jvm_args #Not sure why this wasn't getting used before
   if is_64bit; then
       jvmArgs="$jvmArgs -d64";
   fi

   # Need to pass full path to help directory.  Giving a relative path would
   # not necessarily work if the JVM's cwd gets set elsewhere (such as opening
   # a file).
   helpDir=`dirname "$0"`/help   # This may be relative
   if [ -d "$helpDir" ]; then
       here=`pwd`
       cd `dirname "$0"`/help
       helpDir=`pwd`  # This is now absolute
       cd $here
       jvmArgs="$jvmArgs -Dstar.helpDir=$helpDir"
   fi

   echo_command $JAVA_HOME/bin/java $jvmArgs star.viewer.SceneViewerMain "$@"
}

SCRIPTDIR=`dirname "$0"`
if [ -f "$SCRIPTDIR/starccm+" ]; then
   isStandalone=0
   STARBIN=$SCRIPTDIR
else
   isStandalone=1
   STARBIN=$SCRIPTDIR/lib
   SCRIPTDIR=$SCRIPTDIR/bin/scripts
   arch=`$SCRIPTDIR/maparch`
   STARBIN=$STARBIN/$arch
fi
star_arch=`$SCRIPTDIR/maparch`
. "$SCRIPTDIR/starenv"

main "$@"
