#!/bin/sh
#
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      minispropshow - display mini-switch ASIC property
# 
# SYNOPSIS
#      minispropshow [slot/]minis | [ slotno ] -all
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will display the mini-switch ASIC property.
# 
# OPTIONS
#      [slot/]minis
#                Specify  the  index  of the mini-switch within the
#                blade specified by slot to be displayed.  If  slot
#                is not provided, the current slot will be assumed.
# 
#      [ slotno ] -all
#                Specify which slot to operate on. The entire chips
#                in  the  slot  will be displayed. If slotno is not
#                specified, the current slot will be  assumed.  You
#                can  set  the current slot by execution of setslot
#                command.
# 
# EXAMPLES
#      > minispropshow 1 -all
# 
#      slot: 1, miniS: 0
#      [2/16]
#      <0,1657/0001 1,1657/0001>
#      (be,5) (be,4) (be,3) (be,2) (be,1) (be,0) (bi,55) (bi,54)
#      (bi,25) (bi,24) (bi,41) (bi,40) (fe,3) (fe,2) (fe,1) (fe,0)
# 
#      slot: 1, miniS: 1
#      [2/16]
#      <2,1657/0001 3,1657/0001>
#      (be,11) (be,10) (be,9) (be,8) (be,7) (be,6) (bi,39) (bi,38)
#      (bi,9) (bi,8) (bi,57) (bi,56) (fe,7) (fe,6) (fe,5) (fe,4)
# 
#      slot: 1, miniS: 2
#      [2/16]
#      <4,1657/0001 5,1657/0001>
#      (be,17) (be,16) (be,15) (be,14) (be,13) (be,12) (bi,23) (bi,22)
#      (bi,11) (bi,10) (bi,59) (bi,58) (fe,11) (fe,10) (fe,9) (fe,8)
# 
#      slot: 1, miniS: 3
#      [2/16]
#      <6,1657/0001 7,1657/0001>
#      (be,23) (be,22) (be,21) (be,20) (be,19) (be,18) (bi,7) (bi,6)
#      (bi,27) (bi,26) (bi,43) (bi,42) (fe,15) (fe,14) (fe,13) (fe,12)
# 
# SEE ALSO
#      chippropshow(1d), ptpropshow(1d), setslot(1d)
# 
# NOTES
#      This command not intended for normal user operation  and  is
#      available  to provide supplemental information during system
#      debug.
# 

def_slot=0

# 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

#
# initialize input error syntax
#

multiBlade=`isMultiBlade`
if [ $multiBlade = TRUE ]
then
     syntax="`/bin/basename $0` [slot/]minis \| [ slotno ] -all"
else
     syntax="`/bin/basename $0` minis \|  -all"
fi

#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}
miniS=$slot
dashslot_found=0
permslot_set=0
permmini_set=0
tempmini_set=0

for param in $@ ; do

    #
    # if the previous param was "--slot" :
    #
    # 1) we check if the current param is a number.
    # 2) we check if slot/mini parameter already set the slot number.
    # 3) we set $slot_num with current parameter if none of the above
    #    condition is met. otherwise we just shift or generate 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
	    fi
	    dashslot_found=0
	    shift ; continue
	else
	    echo "Invalid slot number : $param"
	    exit 3
	fi
    fi

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

    slotmini_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}\/[0-9]\{1,\}//'`
    if [ -z $slotmini_matched ] ; then
	slot=`echo $param | /bin/sed 's/\/[0-9]\{1,\}//'`
	miniS=`echo $param | /bin/sed 's/[0-9]\{1,\}\///'`
	permmini_set=1
	permmini_set=1
	shift ;	continue
    fi

    num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
    if [ -z $num_matched ] ; then
	if [ $permmini_set -ne 1 ] && [ $tempmini_set -ne 1 ] ; then
	    miniS=$param
	    tempmini_set=1
	    shift ; continue
	fi
    fi

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

#
# At this point, $miniS should have a valid value and no parameter
# should remain.
#
if [ -z $miniS ] ; then
    err "Invalid command"
    exit 4
fi

case "$#" in
    0)  ;;
    *)  err "Invalid command" ; exit 4 ;;
esac


#
# minispropshow()
#
slot=`getValue $slot`
if [ $? != 0 ] ; then exit 3 ; fi

if [ $multiBlade = TRUE ]
then
    slot_message=" Slot: $slot"
else
    slot_message=""
fi

# extra checking due to "--all" support
if [ ! -e /proc/fabos/blade/$slot ] ; then
   err "$slot_message not found"
   exit 3
fi

_s=`/usr/bin/expr "$miniS" : "[-]*all"`
if [ "0" -ne "$_s" ]
then
   miniS="0"
   let total=4
else
    if [ $miniS -lt -0 ] || [ $miniS -gt 3 ] ; then
	err "Minis: $miniS invalid number"
	exit 4
    else
	let total=1
    fi
fi

enableSlotProc $slot
if [ $? != 0 ] ; then exit 3 ; fi

path="/proc/fabos/blade/$slot"

file="$path/$miniS"

if [ ! -e "$file" ] ; then
   err "MiniS: $miniS not found"
else
   let num=0
   while [ $num -lt $total ]; do
      echo ""
      echo "$slot_message miniS: $miniS"
      prop="$path/$miniS/property"
      /bin/cat $prop
      let num="$num+1"
      let miniS="miniS+1"
   done
fi
echo ""

# disable the proc entries of this slot
disableSlotProc $slot
