#!/bin/sh
#
#
#    Copyright (c) 1996-2000 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      diagclearerror - clears diagnostics failure status
# 
# SYNOPSIS
#      diagclearerror  [[  --slot  ]  slotno ] [ -switch switch ] |
#      -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.
#                You  can set the current slot by execution of set-
#                slot command.
# 
#      -switch switch
#                Selects the switch number to operate on. If  omit-
#                ted, then all blades will be cleared.
# 
#      -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
# 
# SEE ALSO
#      setslot(1d)
# 
def_slot=0
def_switch=0
shtyp=reg

#
# 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 ] [ -switch switch ] \| -all"
else
     syntax="`/bin/basename $0`"
fi


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

#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}
switch=${FABOS_SWITCHNO:-$def_switch}

dashslot_found=0
dashswitch_found=0

permslot_set=0
tempslot_set=0
permswitch_set=0

for param in $@ ; do

    #
    # if the previous param was "-slot" :
    #
    # 1) we check if the current param is a number.
    # 2) we set $slot_num with current parameter if none of the above
    #    condition is met. otherwise we generate an error.
    #
    if [ $dashslot_found -eq 1 ] ; then
	num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
	if [ -z $num_matched ] ; then
	    if [ $permslot_set -ne 1 ] ; then
		slot=$param
		permslot_set=1
	    fi
	    dashslot_found=0
	    shift ; continue
	else
	    err "Invalid slot number : $param"
	    exit 4
	fi
    fi

    #
    # if the previous param was "-switch" :
    #
    # 1) we check if the current param is a number.
    # 2) we set $switch_num with current parameter if none of the above
    #    condition is met. otherwise we generate an error.
    #
    if [ $dashswitch_found -eq 1 ] ; then
	num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
	if [ -z $num_matched ] ; then
	    switch=$param
	    permswitch_set=1
	    dashswitch_found=0
	    shift ; continue
	else
	    err "Invalid switch number : $param"
	    exit 4
	fi
    fi

    dashslot_matched=`echo $param | /bin/sed 's/[-]\{1,\}slot//'`
    if [ -z $dashslot_matched ] ; then
	dashslot_found=1
	shift ; continue
    fi

    #
    # temporary compatibility stuff
    #
    dashslot_matched=`echo $param | /bin/sed 's/[-]\{1,\}p1//'`
    if [ -z $dashslot_matched ] ; then
	dashslot_found=1
	shift ; continue
    fi

    dashswitch_matched=`echo $param | /bin/sed 's/[-]\{1,\}switch//'`
    if [ -z $dashswitch_matched ] ; then
	dashswitch_found=1
	shift ;	continue
    fi

    #
    # temporary compatibility stuff
    #
    dashswitch_matched=`echo $param | /bin/sed 's/[-]\{1,\}p2//'`
    if [ -z $dashswitch_matched ] ; then
	dashswitch_found=1
	shift ;	continue
    fi

    num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
    if [ -z $num_matched ] ; then
	if [ $permslot_set -ne 1 ] ; then
	    slot=$param
	    tempslot_set=1
	    shift ; continue
	else
	    shift ; continue
	fi
    fi

    #
    # if "-all" found, set the slot to null.
    #
    all_matched=`echo $param | /bin/sed 's/[-]\{1,\}all//'`
    if [ -z $all_matched ] ; then
	slot=""
	shift ; continue
    fi

    err "Invalid parameter : $param."
    exit 4
done

#
# if slot is not set, it is same as "-all"
#
if [ $permslot_set -ne 1 ] && [ $tempslot_set -ne 1 ] ; then
    slot=""
else
    if [ -n "$slot" ] ; then
	slot="-slot $slot"
    fi
fi

if [ $permswitch_set -ne 1 ] ; then
    switch=
else
    switch="-switch $switch"
fi

$cmd diagclearerror
exit 0






