#!/bin/sh

########################################################################
#
#       Licensed Materials - Property of IBM
#
#       (C) Copyright IBM Corp. 2005-2015. All Rights Reserved
#
#       US Government Users Restricted Rights - Use, duplication
#       or disclosure restricted by GSA ADP Schedule Contract
#       with IBM Corp.
#
#       ========================================================
#       Module Information:
#
#       DESCRIPTION:
#       nco_jprobe
#
########################################################################


###################################################
# Find nco_common
NCO_COMMON=`dirname $0`/../bin/nco_common

# Check for nco_common, and load if found
if [ ! -f "$NCO_COMMON" ]; then
        echo "Cannot find nco_common" 1>&2
        exit 1
fi
. $NCO_COMMON

# Need to unset the XPG_SUS_ENV var as java application do not run properly on AIX when this var is set
if [ "$SYSTEM" = "AIX" ]
then
        unset XPG_SUS_ENV
fi


#
# What program are we attempting to run ?
#
PROGRAM=`basename $0`

#
# Check the script name - if nco_probe is being run as itself it does nothing
#
if [ $PROGRAM = nco_jprobe ]; then
	exit 0
fi

#
# Can we run this probe ?
#
if [ \( ! -f $OMNIHOME/probes/java/$PROGRAM.class \) -a \( ! -f $OMNIHOME/probes/java/$PROGRAM.jar \) ]; then
	err "$PROGRAM not installed for java"
fi

#
# Set classpath
#
if [ -z "$CLASSPATH" ]; then
    # classpath isn't set so set it to nothing to start with
    CLASSPATH=
fi

#
# try and set up some environment variables for this probe by running
# the probe's env script - if there is one
#
if [ -f $OMNIHOME/probes/java/$PROGRAM.env ]; then
	. $OMNIHOME/probes/java/$PROGRAM.env
	
	# If we're dealing with an IDK developed probe we need different jars in the classpath
	if [ -z "$OIDK_PROBE" ] ; then
		# Put IntegrationsSupport.jar at the front of the CLASSPATH to ensure it's loaded first
		CLASSPATH=$OMNIHOME/probes/java/IntegrationsSupport.jar:$OMNIHOME/probes/java/NSProbe.jar:$CLASSPATH
	else
		CLASSPATH=$OMNIHOME/probes/java/ProbeServices.jar:\
$OMNIHOME/probes/java/NSHeadServices.jar:\
$OMNIHOME/probes/java/framework.jar:\
$OMNIHOME/probes/java/TestServices.jar:\
$OMNIHOME/probes/java/JSON4J_Apache.jar:\
$CLASSPATH
	fi
else
	# this probe hasn't got its own env script yet, so prepend the classpath 
	# with every jar file in $OMNIHOME/probes/java except probe jar files
	for jar_file in `ls $OMNIHOME/probes/java/*.jar | grep -v nco_p_`
	do
		if [ "$jar_file" != "$OMNIHOME/probes/java/IntegrationsSupport.jar" ]; then
			# Add anything but IntegrationsSupport.jar, as it will be added to the front of the CLASSPATH
			CLASSPATH=$jar_file:$CLASSPATH	
		fi
	done
	if [ -f $OMNIHOME/probes/java/IntegrationsSupport.jar ]; then
		# Make sure IntegrationsSupport.jar is loaded first
		CLASSPATH=$OMNIHOME/probes/java/IntegrationsSupport.jar:$CLASSPATH
	fi
fi


# add the probe we're trying to run to the classpath
if [ -f $OMNIHOME/probes/java/$PROGRAM.jar ]; then
    CLASSPATH=$OMNIHOME/probes/java/$PROGRAM.jar:$CLASSPATH
fi

# add $OMNIHOME/probes/java so that single classes will be included
CLASSPATH=$CLASSPATH:$OMNIHOME/probes/java

#
# select the JRE to run the probe
#
if [ -n "$NCO_PROBE_JRE" ]; then
	# NCO_PROBE_JRE is set, check if it's set to a valid JRE
	if [ -f $NCO_PROBE_JRE/bin/java ]; then
		# NCO_PROBE_JRE is set to an existing JRE, so use it
		JAVA="$NCO_PROBE_JRE/bin/java"
	else
		# Not a valid JRE
		echo "Cannot find Java in NCO_PROBE_JRE=$NCO_PROBE_JRE."
		echo "Set the NCO_PROBE_JRE variable to a valid JRE directory in the $PROGRAM.env file. Exiting."
		exit 1
	fi
elif [ -n "$JAVA_HOME" ]; then
	# NCO_PROBE_JRE is not set, but JAVA_HOME is. 
	# Check if the JRE is valid
	# Note that the recommended way to select a specific JRE is to set the NCO_PROBE_JRE env variable
	if [ -f $JAVA_HOME/bin/java ]; then
		# Valid java, use it
		JAVA="$JAVA_HOME/bin/java"
	else
		# Not a valid JRE
		echo "Cannot find Java in JAVA_HOME=$JAVA_HOME"
		echo "Set the NCO_PROBE_JRE variable to a valid JRE directory in the $PROGRAM.env file. Exiting."
		exit 1
	fi	
else
	# Look for the highest JRE shipped with OMNIbus
	. $OMNIHOME/probes/java/set_ibm_jre.sh
	if [ -n "$NCO_PROBE_JRE" ] ; then
		# Only set JAVA if a valid JRE has been found
		JAVA="$NCO_PROBE_JRE/bin/java"
	fi
fi

# set the Visibroker license variable
VBROKER_ADM=$OMNIHOME/probes/java
export VBROKER_ADM

#
# JVM options
# NCO_JPROBE_JAVA_FLAGS are general options
# NCO_JPROBE_JAVA_XFLAGS are non-portable JVM options
# NBS 019226 - Using -server argument can significantly increase performance
# -server is not an available option on IBM/AIX java
#

if [ "solaris2" = "$ARCH" -o "linux2x86" = "$ARCH" -o  "hpux11" = "$ARCH" ]; then
    NCO_JPROBE_JAVA_FLAGS="-server $NCO_JPROBE_JAVA_FLAGS"
fi

if [ -z "$NCO_JPROBE_JAVA_XFLAGS" ]; then
	#
	# Sun's JVM interferes with our signalling, so we reduce
	# the Sun JVM signal use using the -Xrs flag.
	# Setting NCO_JPROBE_JAVA_XFLAGS in your environment overrides 
	# completely the default behaviour here, so be aware that setting this
	# may affect probe behaviour in unexpected ways.
	#
	if [ "solaris2" = "$ARCH" -o "linux2x86" = "$ARCH" ]; then
		NCO_JPROBE_JAVA_XFLAGS="-Xrs"
	fi
fi

#
# Check if we have a valid java set
#
if [ -z "$JAVA" ] ; then
	# Java not found
	echo "Java not found. Set the NCO_PROBE_JRE variable to a valid JRE directory in the $PROGRAM.env file"
	exit 1
elif [ ! -x "$JAVA" ] ; then
	# Java found, but it does not have 'execute' permission
	echo "Cannot use $JAVA as it does not have 'execute' permission."
	exit 1
fi

#
# Hopefully we have JAVA set by now, so test it to make sure
#
RESULT=`"$JAVA" -version 2>&1`
#
# If the test failed, or $JAVA still doesn't exist, give up
#
if [ "$?" != 0 ] ; then
  echo "Problem running java using $JAVA. Check the binary is accessible and has correct permissions"
  exit 
fi

#
# Provide a little feedback about the vendor and version if
# available
#
IBM_JAVA=`echo $RESULT | grep -i IBM`
if [ "$IBM_JAVA" != "" ] ; then
  echo "Using IBM Java"
else
  echo "Using non-IBM Java"
fi

if [ "$JRE_VERSION" = "" ] ;
then
  # extract it
  JRE_VERSION=`echo $RESULT | head -1 | awk '{ print $3 }' | sed s/\"//g | cut -d'_' -f1`
fi
echo "Version $JRE_VERSION"

# Execute probe
PROBENAME=`echo "$PROGRAM" |cut -d'_' -f3-`
for arg in ${*}; do
     case ${arg} in
        -check)
        exec  "$JAVA" -cp "$OMNIHOME/bin/ConfigAnalyser.jar:$OMNIHOME/java/jars/jlog.jar:$OMNIHOME/probes/java/$PROBENAME.check.jar"  $NCO_JPROBE_JAVA_XFLAGS com.ibm.tivoli.netcool.integrations.configanalyser.ConfigAnalyser $OMNIHOME/probes/$ARCH/ $PROGRAM $*
        exit
        ;;
	esac
done

if [ -z "$OIDK_PROBE" ] ; then
    nonnative="$OMNIHOME/probes/nco_p_nonnative"
    exec "$nonnative" "$JAVA" $NCO_JPROBE_JAVA_FLAGS -cp "$CLASSPATH" $NCO_JPROBE_JAVA_XFLAGS -DOMNIHOME="$OMNIHOME" $PROGRAM "$@"
else
    exec "$JAVA" $NCO_JPROBE_JAVA_FLAGS -cp "$CLASSPATH" -DOMNIHOME="$OMNIHOME" -DKERN_ARCH="$KERN_ARCH" com.ibm.tivoli.netcool.omnibus.oidk.Probe "$@"
fi
