#!/bin/sh
# Script for OVT startup
# $Id: ovt.sh,v 1.10 2001/04/24 17:16:16 ko Exp $
# $Source: /stor/devel/ovt2g/bin/ovt.sh,v $   
#

echo "Orbit Visualization Tool 2.0"
echo "Copyright (c) OVT Team, 2000-2001"

#JAVA_HOME=/usr/java1.2;	export JAVA_HOME
#OVT_USER_HOME=$HOME/ovt2g;		export USER_HOME_DEFAULT
#OVT_HOME=$HOME/ovt2g;	export OVT_HOME_DEFAULT

VTK_CLASSPATH=/usr/local/vtk/vtk.jar;		export VTK_CLASSPATH
VTK_LIBS=/usr/local/lib;		export VTK_LIBS

if [ "$VTK_CLASSPATH" != "" ]
then
	echo Adding $VTK_CLASSPATH to CLASSPATH
	CLASSPATH=$CLASSPATH:$VTK_CLASSPATH;	export CLASSPATH
fi
if [ "$VTK_LIBS" != "" ]
then
        echo Adding $VTK_LIBS to LD_LIBRARY_PATH
        LD_LIBRARY_PATH=$VTK_LIBS:$LD_LIBRARY_PATH;    export LD_LIBRARY_PATH
fi


if [ "$OVT_HOME" = "" ] ; then
  ## resolve links - $0 may be a link to  home
  PRG=$0
  progname=`basename $0`
  echo PRG=$0
  echo progname=$progname 

  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '.*/.*' > /dev/null; then
        PRG="$link"
    else
        PRG="`dirname $PRG`/$link"
    fi
  done
 
  OVT_HOME_1=`dirname "$PRG"`/..
  echo "Guessing OVT_HOME from ovt.sh to ${OVT_HOME_1}"
    if [ -d ${OVT_HOME_1}/conf ] ; then
        OVT_HOME=${OVT_HOME_1}
        echo "Setting OVT_HOME to $OVT_HOME"
	export OVT_HOME
    else
	echo "Please set OVT_HOME or edit ovt.sh"
	exit 1
    fi
fi     


if [ -z "$JAVA_HOME" ] ;  then
  echo "JAVA_HOME is not set. Guessing from PATH..."
  JAVA=`which java`
  if [ -z "$JAVA" ] ; then
    echo "Cannot find JAVA. Please set JAVA_HOME or add JAVA to PATH."
    exit 1
  fi
  JAVA_BINDIR=`dirname $JAVA`
  JAVA_HOME=$JAVA_BINDIR/..
fi
 
if [ "$JAVACMD" = "" ] ; then
   # it may be defined in env - including flags!!
   JAVACMD="$JAVA_HOME/bin/java -mx300m"
fi   

CLASSPATH=$CLASSPATH:$OVT_HOME

#
# add all .jar files in ${OVT_HOME}/lib/ to CLASSPATH
#
for i in ${OVT_HOME}/lib/*.jar ; do
  CLASSPATH=${CLASSPATH}:$i
done 

export CLASSPATH

echo Using CLASSPATH=$CLASSPATH
 
LD_LIBRARY_PATH=$OVT_HOME/lib:$LD_LIBRARY_PATH;	export LD_LIBRARY_PATH

if [ "$1" = "-test" ]
then
	echo "Testing VTK."
	echo "Showing VTK window..."
	$JAVACMD ovt.test
elif [ "$1" = "-install" ] 
then
	echo "Creating OVT user directory..."
	if [ "$OVT_USER_HOME" = "" ] ; then
		echo "OVT_USER_HOME is not set. Set it or edit ovt.sh"
		exit 1
	fi
	if test -d $OVT_USER_HOME 
	then
		echo "OVT user directory '"$OVT_USER_HOME"' already exist."
		exit 1		
	else
		mkdir $OVT_USER_HOME
		cp -R $OVT_HOME/conf $OVT_USER_HOME
		cp -R $OVT_HOME/userdata $OVT_USER_HOME
		echo "Done."
	fi


elif [ "$1" = "" ]
then
  echo "Starting OVT2G ..."
  if [ "$OVT_USER_HOME" = "" ] ; then
	#echo "Seting OVT_USER_HOME to OVT_HOME"
	OVT_USER_HOME=$OVT_HOME; export OVT_USER_HOME
  fi
  echo "OVT_USER_HOME = "$OVT_USER_HOME
  $JAVACMD -Dovt.home=$OVT_HOME -Dovt.user=$OVT_USER_HOME ovt.XYZWindow

else
	echo "\nUsage:"
	echo "-test      test VTK (Visualization Toolkit)"
	echo "-install   create OVT user directory"
fi

