#!/bin/sh
#
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   burninStatus
#    Module name: diag/scripts
#
#    This file reports the state of the burnin activity.
#
# NAME
#      burninstatus - display the diagnostics burnin status
# 
# SYNOPSIS
#      burninstatus [ --slot <slot_number> ]
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This command displays the burnin status of each blade in the
#      system.  The output contains the slot,  state,  current  run
#      number, current command in the run, total commands in a run,
#      and the burnin script name.
# 
# EXAMPLES for blade based systems
#      > burninstatus
#      Slot    State    Status  Run     Cmd   TotCmds Script
#      1       ABORT    PASS    3       18    41      switchess.sh
#      2       ABORT    PASS    3       18    41      switchess.sh
#      3       ABORT    PASS    3       18    41      switchess.sh
#      4       ABORT    FAIL    3       11    34      switchess.sh
#
# EXAMPLES for switch based systems
#      > burninstatus
#      State    Status  Run     Cmd   TotCmds Script
#      ABORT    PASS    3       18    41      switchess.sh
# 
# SEE ALSO
#      diagsetburnin(1d)
# 

# Check RBAC permission on command
/fabos/libexec/rbac_check `/bin/basename $0`

if [ $? -ne 0 ]; then
	exit 127
fi

do_chassis()
{
  if [ "$unit_number" != -1 ]
  then
    slots=$unit_number
  else
    slots=`getAvailSlots -chassis`
  fi
  if [ ! -z "$slots" ] ;
  then
	for slot in $slots
	do
		script=`getBladeParam $slot SCRIPT_NAME`
		state=`getBladeParam $slot STATE`
		cum_status=`getBladeParam $slot CUM_STATUS`
		run_num=`getBladeParam $slot RUN_NUMBER`
		tot_cmd=`getBladeParam $slot TOTAL_CMD_NUMBER`
		cmd_num=`getBladeParam $slot CMD_NUMBER`
		pid=`getBladeParam $slot PID`
		if [ $cum_status -eq 0 ]
		then
			cum_status=PASS
		else
			cum_status=FAIL
		fi
		case $state in
		"ABORT" ) state="ABORT    ";;
		"ACTIVE" )state="ACTIVE   ";;
		"TESTED" )state="TESTED   ";;
		esac

		# cover the case where the run_number and command number are post incremented
		if [ "`/bin/echo $state | /bin/grep COMPLETE`" != "" -o \
		     "`/bin/echo $state | /bin/grep TEST`" != "" ]
		then
		    if [ $run_num -gt 0 -a $cmd_num -gt 0 ]
		    then
			run_num=`expr $run_num - 1`
			cmd_num=`expr $cmd_num - 1`
		    fi
		fi
		if [ `isMultiBlade` = TRUE ]
		then
		    /bin/echo "$slot	$state	$cum_status	$run_num	$cmd_num	$tot_cmd	$pid	$script"
		else
		    /bin/echo "$state	$cum_status	$run_num	$cmd_num	$tot_cmd	$pid	$script"
		fi
	done
  fi
}

home="/fabos/share"
util="diagcommon.sh"
export syntax="$0 [ --slot <slot_number> ]"
ok=0
for f in "./$util" "$FABOSHOME/share/$util" "$home/$util" ; do
	if [ -r $f ] ; then
		. $f
		ok=1
		break;
	fi
done
if [ $ok -ne 1 ] ; then
	err "Could not locate $util"
	exit 3
fi
unit_number=-1
while [ "$1" != "" ]
do
  case $1 in
  "-slot" | "--slot")
    shift
    if [ "`isValidSlot $1 -chassis`" = TRUE ]
    then
      unit_number=$1
      shift
    else
      /bin/echo "$0 invalid slot specified $1"
      /bin/echo "valid slots are: $all_slots"
      /bin/echo $syntax
      exit 3
    fi
    ;;
  * )
    /bin/echo "Invalid parameter $1"
    /bin/echo $syntax
    exit 3
    ;;
  esac
done

if [ `isMultiBlade` = TRUE ]
then
    /bin/echo "Slot	State   	Status	Run	Cmd	TotCmds	PID	Script"
else
    /bin/echo "State   	Status	Run	Cmd	TotCmds	PID	Script"
fi
do_chassis
exit 0
