#! /usr/bin/ksh
#
# @(#)u4ft_checkFRUBusyStatus.sh	1.4	98/07/09 SMI
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
# u4ft_checkFRUBusyStatus name number location state lock
#		dev_locked ... info dev_info ...
# 
# Purpose:	To find out whether the devices of the FRU have finished
#		onlining/offlining and whether their attempts where 
#		successful.
# Result:	"not_complete"	- devices still locked
#		"success"	- devices unlocked, no faults found
#		"failed"	- devices unlocked, faults found
#
# Mandatory Parameters:
# name		- class name of FRU
# number	- instance number of FRU
# location	- location of FRU
# state		- state of FRU
# lock 		- a space-separated list of the device _locked attributes
# info 		- a space-separated list of the device info attributes
#
# Error Codes:
# Return 0 on success, non-zero otherwise
#

# Validate that the parameters contain the word "info"
# Validate that we have six parameters 
if [[ $# -ne 6 ]]
then 
        cmsdebug "u4ft_checkFRUBusyStatus: wrong no. of args $1 $2 $3 $4 $5 $6"
	exit 1
fi

name="$1"
number="$2"
location="$3"
state="$4"
lock="$5"
info="$6"

busy=""
status=""

# returns "busy=true" if "lock" contains "yes"
# returns "status=failed" if "info" contains a word not equal to "string"

eval "$(echo | nawk -v "lock=$lock" -v "info=$info" \
	  '{	
		split(lock,locks)
		for (l in locks) {
			if ( locks[l] == "yes" )
				busy="true"
		}
		split(info,information)
		for (i in information) {
		    if ( information[i] != "string" )
				status="failed"
		}
		printf("busy=%s status=%s",busy,status);
	   }')"

# Set FRU device_status attribute to the sum of the device device_status
if [[ -n "$status" ]]
then
	if [[ "$state" = "should_be_configured" || "$state" = "configured" ]]
	then	status_message="devices have failed to setup"
	else	status_message="devices have failed to offline"
	fi
	cmsreport $name $number info "$status_message"
fi

# Return status of order, i.e. succeeded, failed, not_complete
if [[ -n "$busy" ]]
then
	return_message="not_complete"
elif [[ -z "$status" ]]
then
	return_message="success"
else
	return_message="failed"
fi;
echo $return_message
exit 0
