#!/bin/sh
#
#
#    Copyright (c) 1996-2006 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      diagclearerror - clears diagnostics failure status
# 
# SYNOPSIS
#      diagclearerror  [[  --slot  ]  slotno ] | -all
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This command is used to clear the diagnostics  failure  sta-
#      tus.
# 
# OPTIONS
#      [ --slot ] slotno
#                Specify  which  slot to operate on. If this option
#                is not specifed, the current slot will be assumed.
# 
#      -all      If specified, all blades will be cleared.
# 
# EXAMPLES
#      > diagclearerror 1
#       0x1bcb (fabos):  Switch: 0, Error DIAG-CLEARERR, 3,
#      Pt5 Sl1 Ch0 Qd1 Diagnostics Error Cleared
#      Err# 0120041 0105
# 
# 
def_slot=0
shtyp=reg

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

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

#
# Load library -- must be first.
#
home="/fabos/share"
util="diagcommon.sh"
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
	echo "Error -- could not locate $util"
	exit 3
fi

#
# get sys info for multi blade
#
multiBlade=`isMultiBlade`
#
# initialize for input error syntax
#
if [ $multiBlade = TRUE ]
then
     syntax="`/bin/basename $0` [[ --slot ] slotno ] \| -all"
else
     syntax="`/bin/basename $0`"
fi


#
# Find exec_diag
#
home="/fabos/cliexec"
util="exec_diag"
ok=0
for f in "./$util" "$FABOSHOME/sbin/$util" "$home/$util" "/fabos/sbin/$util" ; do
	if [ -r $f ] ; then
		cmd=$f
		ok=1
		break;
	fi
done

#
# Preprocessing command arguments 
#
# Initialize parse variables
slot="-slot ${FABOS_SLOTNO:-$def_slot}"
permslot_set=0

for param in $@ ; do
    dashslot_matched=`echo $param | /bin/sed 's/[-]\{1,\}slot//'`
    if [ -z $dashslot_matched ]
    then
	if [ $permslot_set -eq 1 ]
	then
	    err "slot number set more than once : $param."
	    exit 4
	fi
	shift ; continue
    fi

    #
    # compatibility stuff
    #
    dashslot_matched=`echo $param | /bin/sed 's/[-]\{1,\}p1//'`
    if [ -z $dashslot_matched ]
    then
	if [ $permslot_set -eq 1 ]
	then
	    err "slot number set more than once : $param."
	    exit 4
	fi
	shift ; continue
    fi

    #
    # if "-all" found, set the slot to null.
    #
    all_matched=`echo $param | /bin/sed 's/[-]\{1,\}all//'`
    if [ -z $all_matched ]
    then
	if [ $permslot_set -eq 1 ]
	then
	    err "slot number set more than once : $param."
	    exit 4
	fi
	slot="-slot -1"
	permslot_set=1
	shift ; continue
    fi

    num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
    if [ "`isValidSlot $param -chassis`" = TRUE ]
    then
	if [ $permslot_set -eq 1 ]
	then
	    err "slot number set more than once : $param."
	    exit 4
	else
	    slot="-slot $param"
	    permslot_set=1
	    shift ; continue
	fi
    else
	err "Invalid Slot number : $param."
	exit 4
    fi

    err "Invalid parameter : $param."
    exit 4
done
if [ $permslot_set = 0 ]
then
    if [ "`isValidSlot ${FABOS_SLOTNO:-$def_slot} -chassis`" != TRUE ]
    then
	err "Invalid slot specified ${FABOS_SLOTNO:-$def_slot}"
	exit 4
    fi
fi
$cmd diagclearerror $slot
exit 0
