#!/bin/sh
#
# $Header: cluvfy.sbs 10-jul-2007.06:14:17 skoduru Exp $
#
# cluvfy.sbs 
#
# Copyright (c) 2003, 2007, Oracle. All rights reserved.  
#
#    NAME
#      cluvfy.sbs - Starts the cluster verification driver
#
#    DESCRIPTION
#      This requires these environmental variables.
#      CV_HOME: home for CV tools
#      CV_JDKHOME: java home
#      CV_DESTLOC: the remote locations where the required files can be
#                  copied and executed. The user MUST have write permission
#                  on this location, on all the nodes.
#
#    NOTES
#      
#
#    Build: 101305

AWK=/bin/awk
if [ -z "$ECHO" ]; then ECHO=/bin/echo; fi

validateInput()
{
##validates if any value is assigned to the script variables
PARAM_NAME=$1
valid=`$ECHO $PARAM_NAME | $AWK '/^%/ { print "false"; }'`
if [ "$valid" = "false" -o ! -d $PARAM_NAME ];
then
return 1;
fi

return 0;
}

CRSHOME=%s_OracleHome%
JREDIR=%s_jreLocation%
DESTLOC=%s_destLocation%

validateInput $CRSHOME
ret=$?
if [ "$ret" = "0" ];
then
  CV_HOME=$CRSHOME
fi

if [ -z "$CV_HOME" ];
then
  cmdpath=`dirname $0`
  case "$cmdpath" in
    /*) ;;
    .) cmdpath=`pwd`;;
     *) cmdpath=`pwd`/$cmdpath;;
  esac
  if [ -d $cmdpath/../bin ] && [ -f $cmdpath/../jlib/cvu.jar ]
  then
     CV_HOME=$cmdpath/..
  else
     $ECHO "Set CV_HOME environmental variable before proceeding."
     exit 1
  fi
fi

validateInput $JREDIR
ret=$?
if [ "$ret" != "0" ];
then
  if [ -z "$CV_JDKHOME" ];
  then
     if [ -d $CV_HOME/jdk/jre ];
     then
        CV_JDKHOME=$CV_HOME/jdk/jre
     else
        $ECHO "Set CV_JDKHOME to a valid JRE 1.5 location"
        exit 1
     fi
  fi
  JREDIR=$CV_JDKHOME
fi

#If ORACLE_HOME is not set in the environment make ORACLE_HOME point to CV_HOME
if [ -z "$ORACLE_HOME" ];
then
  ORACLE_HOME=$CV_HOME
  export ORACLE_HOME
fi

REM_CVHOME="-DCV_HOME=$CV_HOME"

CVUJAR=$CV_HOME/jlib/cvu.jar
SRVMJAR=$CV_HOME/jlib/srvm.jar

JRE=$JREDIR/bin/java
JREJAR=$JREDIR/lib/rt.jar

CLASSPATH=$JREJAR:$CVUJAR:$SRVMJAR


#Check RemoteExec destination location
if [ -z "$CV_DESTLOC" ]
then
    validateInput $DESTLOC
    ret=$?
    if [ "$ret" = "0" ];
    then
	CV_DESTLOC=$DESTLOC
    fi
fi

#If remote destination location is empty show error msg. Proceed with default.
if [ -n "$CV_DESTLOC" ]
then
    REM_DESTLOC="-DCV_DESTLOC=$CV_DESTLOC"
else
    REM_DESTLOC="-DCV_DESTLOC=/tmp"
fi

REM_ENV="$REM_DESTLOC $REM_CVHOME"

if [ -n "$SRVM_TRACE" ]
then
   if [ "$SRVM_TRACE" = "false" ]  || [ "$SRVM_TRACE" = "FALSE" ]
   then
     TRACE="-DTRACING.ENABLED=false"
   else
     TRACE="-DTRACING.ENABLED=true -DTRACING.LEVEL=2"
   fi
fi

if [ -n "$CV_TRACELOC" ];
then
  if [ ! -d $CV_TRACELOC ]; then
    $ECHO "Trace directory \"$CV_TRACELOC\" does not exist."
    exit 1
  fi
  if [ ! -w $CV_TRACELOC ]; then
    $ECHO "Trace directory \"$CV_TRACELOC\" is not writable."
    exit 1
  fi
  TRACELOC="-DCV_TRACELOC=$CV_TRACELOC"
fi

LD_LIBRARY_PATH=$CV_HOME/lib:$LD_LIBRARY_PATH


# Set the shared library path for JNI shared libraries
# A few platforms use an environment variable other than LD_LIBRARY_PATH
PLATFORM=`uname`
case $PLATFORM in
  HP-UX) 
       # bugfix 5404049
       if [ -z "$JAVA_HOME" ];
       then
         JAVA_HOME=$JREDIR/..
         export JAVA_HOME
       fi
       JRE_OPTS=-d64
       ;;
  AIX) LIBPATH=$CV_HOME/lib:$LIBPATH
       export LIBPATH
       JRE_OPTS=-d64
       ;;
  Linux) arch=`uname -m`
       # Linux ( ppc64 ) => LD_LIBRARY_PATH lib32 for 32-bit Java 
       if [ "$arch" = "ppc64" -o "$arch" = "s390x" ]
       then
         LD_LIBRARY_PATH=$CV_HOME/lib32:$LD_LIBRARY_PATH
       fi
       ;;
  SunOS) arch=`/usr/bin/isainfo -k`
       # Bug 4620768. Unset LD_LIBRARY_PATH_64 
       unset LD_LIBRARY_PATH_64 

       # Solaris  ( x86_64 ) => LD_LIBRARY_PATH lib32
       # LD_LIBRARY_PATH variable setting can be removed 
       # when we move to 64bit JRE on Solaris AMD64
       if [ "$arch" = "amd64" ] 
       then
         LD_LIBRARY_PATH=$CV_HOME/lib32:$LD_LIBRARY_PATH
       fi

       arch=`/bin/uname -i`
       if [ "$arch" != "i86pc" ]
       then
         JRE_OPTS=-d64
       fi
       ;;
esac
 
export LD_LIBRARY_PATH

# Run cluvfy driver
$JRE $JRE_OPTS -classpath $CLASSPATH $REM_ENV $TRACE $TRACELOC oracle.ops.verification.client.CluvfyDriver "$@"
exit $?
