#! /usr/bin/ksh
#
# @(#)u4ft_controlDevice.sh	1.4	98/05/28 SMI
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
# u4ft_controlDevice name number cms_tag command FRU_name FRU_number
#
# Purpose:      To evaluate the result of attempting to online or offline a
#               device and take appropriate action
# Result:       If the command succeeded, the Device busy lock will be unset.
#		If the command failed due to invalid arguments, device busy,
#		the request not valid or another, a message will be reported 
#		to the status log and the Device busy lock will be unset.
#
# Mandatory Parameters
# name			- class name of device
# number		- instance number of device
# cms_tag		- cms tag for device
# command		- state change request
# FRU_name		- class name of container FRU
# FRU_number		- instance number of container FRU
#
# Error Codes:
# Return 0 on success, non-zero otherwise
#

# Check that there are enough parameters
if [[ $# -ne 6 ]]
then
        $CMSHOME/lib/cmsdebug "u4ft_controlDevice: wrong no. of args \
		$1 $2 $3 $4 $5 $6"
	exit 1
fi

name="$1"
number="$2"
cms_tag="$3"
command="$4"
FRU_name="$5"
FRU_number="$6"

# Use cms_tag to find device cookie
dev_cookie="$($CMSHOME/lib/u4ftctl find / "$cms_tag")"
if [[ -z "$dev_cookie" ]]
then
	$CMSHOME/lib/cmsreport $name $number \
		info "cms_tag $cms_tag failed to find device"
	exit 2
fi

# Run online/offline command to device
$CMSHOME/lib/u4ftctl $command "$dev_cookie" "$name $number request"
integer result=$?
if [[ $result -eq 0 ]]
then
	exit 0
fi

# Set info attribute for most likely errors
case $result in
	16) error_str="device busy" ;;
	22) error_str="invalid args" ;;
	71) error_str="req invalid" 
	    $CMSHOME/lib/u4ftctl report_state "$dev_cookie" ;;
	*)  error_str="$result" ;;
esac;

$CMSHOME/lib/cmsreport $name $number info "$name $number -$error_str. ";
$CMSHOME/lib/cmslog 2 \
  "$name $number of $FRU_name $FRU_number failed to $command -$error_str. ";
exit $result;
