#!/bin/sh


# 
#  @(#)stop-diag	1.22 05/06/06
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#  @version 1.22 05/06/06 17:02:03
#


dirname=`/bin/dirname $0`
if [ -d $dirname ]; then
	## Set the I18N/L10N environment
	if [ -f $dirname/common.sh ]; then
		. $dirname/common.sh
	fi
else
	## this will be a simple echo
	/bin/gettext "Can not determine where start-diag is"
	echo ""
	exit 1
fi

basedir=`/usr/bin/pkgparam SUNWescom BASEDIR`
. ${basedir:=/opt}/SUNWsymon/sbin/es-common.sh
#
# Set the locale for I18N/L10N
# ----------------------------
check_for_l10n
TEXTDOMAIN="SUNW_HWDIAG"
export TEXTDOMAIN
setup_textdomain $TEXTDOMAIN


unset TEST
TEST=thisTest

if [ -f "/var/opt/SUNWhwdiag/logs/hwdserver.pid" ]; then
	EDS_PID=`cat /var/opt/SUNWhwdiag/logs/hwdserver.pid`
	if [ ! ${TEST} = ${TEST}${EDS_PID} ] ; then
		## is this process still active?
		EDS_PID_A=`/bin/ps -efo pid | /usr/xpg4/bin/sed 's/ //g' | /usr/xpg4/bin/grep -F -x ${EDS_PID} `
		if [ ! ${TEST} = ${TEST}${EDS_PID_A} ] ; then
			l_KILL=`/bin/kill  ${EDS_PID} 2> /dev/null`
			sleep 3
			EDS_PID_A=`/bin/ps -efo pid | /usr/xpg4/bin/sed 's/ //g' | /usr/xpg4/bin/grep -F -x ${EDS_PID} `
			if [ ! ${TEST} = ${TEST}${EDS_PID_A} ] ; then
				/bin/gettext "Can not kill Hardware Diagnostic Suite Server. Please check permissions."
				echo ""
				exit 1
			else
				/bin/gettext "Hardware Diagnostic Suite Server stopped."
				/bin/rm -f /var/opt/SUNWhwdiag/logs/hwdserver.pid
				/bin/rm -f /var/opt/SUNWhwdiag/logs/HWDServerRMIServer.log
				/bin/rm -f /tmp/HWDServerRMIServer.log
				/bin/rm -f /tmp/HandleMessage.log
				/bin/rm -f /tmp/javavtscmd.log
				/bin/rm -f /tmp/edserver1099
			fi

			echo ""
			exit 0;
		fi
 	fi
fi

## Try other avenues to stop HWDS Server, in the event that
## /var/opt/SUNWhwdiag/logs/hwdserver.pid does not exist
EDS_PID=`/bin/ps -ef | /bin/fgrep "DHWDS" | /bin/fgrep -v fgrep | /bin/awk '{ print $2 }'`

if [ -x /usr/ucb/ps ] ; then
        EDS_PID=`/usr/ucb/ps -aw | /bin/fgrep "DHWDS" | /bin/fgrep -v fgrep | /bin/awk '{ print $1 }'`
fi


if [ ! ${TEST} = ${TEST}${EDS_PID} ] ; then
	l_KILL=`/bin/kill ${EDS_PID}`
	sleep 3
	EDS_PID=`/bin/ps -efo pid | /usr/xpg4/bin/sed 's/ //g' | /usr/xpg4/bin/grep -F -x ${EDS_PID} `
	if [ ! ${TEST} = ${TEST}${EDS_PID} ] ; then
		/bin/gettext "Can not kill Hardware Diagnostic Suite Server. Please check permissions."
		echo ""
		exit 1
	else
		/bin/gettext "Hardware Diagnostic Suite Server stopped."
		/bin/rm -f /var/opt/SUNWhwdiag/logs/hwdserver.pid
		/bin/rm -f /var/opt/SUNWhwdiag/logs/HWDServerRMIServer.log
		/bin/rm -f /tmp/HWDServerRMIServer.log
		/bin/rm -f /tmp/HandleMessage.log
		/bin/rm -f /tmp/javavtscmd.log
		/bin/rm -f /tmp/edserver1099
	fi

	echo ""
	exit 0
else 
	/bin/gettext  "Hardware Diagnostic Suite Server not running"
	echo ""
	exit 0
fi
