#! /bin/ksh
#  Copyright(c) 2003 Sun Microsystems, Inc.
#  All rights reserved.
#
#ident  "@(#)newprobe 1.31 03/03/31  SMI"
#
set +o noclobber

ERRORLOG=/var/opt/SUNWcst/newprobe.err
ERRORNUM=0
NICE=/usr/bin/nice

# First, get probe location.
if pkginfo -q SUNWcstu; then
	PROBEDIR=`pkginfo -r SUNWcstu`/SUNWcstu/bin
else
	echo CST package is not installed.
	exit 1
fi

i=1
while [ $i -le $# ]
do
        eval arg='$'{$i}
        if [ $arg == "-cfdir" ]; then
                eval CFDIR='$'{$(($i+1))}
                break
        fi
        i=$(($i+1))
done

if test -z "$CFDIR"; then
        CFDIR="."
fi

# Check for the string DISKPROBE
# # in cst.conf. If this is set to 0, then dont probe
# # peripheral devices (like disks in SAN and tape drives)
VAL=`grep DISKPROBE /var/opt/SUNWcst/cst.conf | cut -f 2 -d'	'`
if [ "$VAL" == "" ]; then
VAL=1
fi
#


# run configd
if pkginfo -q SUNWesagt; then
	SUNMCDIR=`pkginfo -r SUNWesagt`
	CONFIGD=`ls $SUNMCDIR/SUNWsymon/bin/configd* 2> /dev/null | head -1`

	if [ `uname -i` == "SUNW,Ultra-Enterprise-10000" ]; then
		# Only for the E10K platform.
		if pkginfo -q SUNWedag; then
			HDBASEDIR=`pkginfo -r SUNWedag`
			if [[ -a $HDBASEDIR/SUNWsymon/addons/AdvancedMonitoring/lib/cfd/configd4u1 ]]; then
				CONFIGD=$HDBASEDIR/SUNWsymon/addons/AdvancedMonitoring/lib/cfd/configd4u1
			fi
		fi
	fi
else
	# SunMC is not installed. Let's check whether each configd package is installed independently.
	# We assume that CST users have installed proper configd package for their platform.
	# But this can hardly happen.

	if pkginfo -q SUNWsycfd; then
		CONFIGD=`pkginfo -r SUNWsycfd`/SUNWsymon/bin/configd
	elif pkginfo -q SUNWeswga; then
		CONFIGD=`pkginfo -r SUNWeswga`/SUNWsymon/bin/configdWG
	elif pkginfo -q SUNWesnta; then
		CONFIGD=`pkginfo -r SUNWesnta`/SUNWsymon/bin/configdnt
	elif pkginfo -q SUNWeswsa; then
		CONFIGD=`pkginfo -r SUNWeswsa`/SUNWsymon/bin/configddt
	elif pkginfo -q SUNWessda; then
		CONFIGD=`pkginfo -r SUNWessda`/SUNWsymon/bin/configd
	elif pkginfo -q SUNWesvig; then
		CONFIGD=`pkginfo -r SUNWesvig`/SUNWsymon/bin/configd.4ue
	elif pkginfo -q SUNWesscd; then
		CONFIGD=`pkginfo -r SUNWesscd`/SUNWsymon/bin/configd.sunfire15000
	elif pkginfo -q SUNWedag; then
		if [ `uname -i` == "SUNW,Ultra-Enterprise-10000" ]; then
			CONFIGD=`pkginfo -r SUNWedag`/SUNWsymon/addons/AdvancedMonitoring/lib/cfd/configd4u1
		fi
	fi
fi


if test -n "$CONFIGD"; then
	$NICE -19 $CONFIGD -f > /tmp/configd.latest 2>$ERRORLOG

	if [ $? == 0 ]; then
		$NICE -19 $PROBEDIR/probe -perihw "$VAL" "$@" > /tmp/probe.latest
		if [ $? != 0 ]; then
			exit 1
		fi
		sed -f $PROBEDIR/scr1.sed /tmp/configd.latest | nawk -f $PROBEDIR/scr.awk "cfdir=$CFDIR" > /tmp/configd.HW
		sed -f $PROBEDIR/scr2.sed /tmp/probe.latest
	else
	# check this is a real error since configd sometimes returns non-zero value though it looks operating correctly.
		numoflines=`cat /tmp/configd.latest 2> /dev/null | wc -l`
		if [ "$numoflines" -gt 5 ]; then
		# now, more error checking depends on the number of lines in configd.latest file.
		# if error message is more than 5 lines, it will mess up.
			$NICE -19 $PROBEDIR/probe -perihw "$VAL" "$@" > /tmp/probe.latest
			if [ $? != 0 ]; then
				exit 1
			fi
			sed -f $PROBEDIR/scr1.sed /tmp/configd.latest | nawk -f $PROBEDIR/scr.awk "cfdir=$CFDIR" > /tmp/configd.HW
			sed -f $PROBEDIR/scr2.sed /tmp/probe.latest
		else
			$NICE -19 $PROBEDIR/probe -nocfd -perihw "$VAL" "$@"
			if [ $? != 0 ]; then
				exit 1
			fi
		fi
	fi
else
	$NICE -19 $PROBEDIR/probe -nocfd -perihw "$VAL" "$@"
	if [ $? != 0 ]; then
		exit 1
	fi
fi


# just clean up /tmp directory
\rm -f /tmp/configd.latest
\rm -f /tmp/probe.latest
\rm -f /tmp/configd.HW

exit $ERRORNUM
