#!/bin/sh
#
#
#    Copyright (c) 1996-2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      chippropshow - display chip properties
# 
# SYNOPSIS
#      chippropshow [slot/]chip | [ [--slot] slotno ]  -all
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This  command  will  display the properties of the specified
#      chips.
# 
# OPTIONS
#      [slot/]chip
#                Specify the index of the  chip  within  the  blade
#                specified  by slot to be displayed. If slot is not
#                specified, the current slot will be  assumed.  You
#                can  set  the current slot by execution of setslot
#                command.
# 
#      [ [--slot] slotno ] -all
#                If specified, it will display  the  chip  property
#                contents  for  the entire chips in the slot speci-
#                fied by slotno.  If slotno is not  specified,  the
#                current slot will be assumed.
# 
# EXAMPLES
#      > chippropshow 1 -all
#      slot: 1, minis: 0 chip: 0
#      [1657/0001,0104,2/8]
#      slot: 1, minis: 0 chip: 1
#      [1657/0001,0104,2/8]
#      slot: 1, minis: 1 chip: 0
#      [1657/0001,0104,2/8]
#      slot: 1, minis: 1 chip: 1
#      [1657/0001,0104,2/8]
#      slot: 1, minis: 2 chip: 0
#      [1657/0001,0104,2/8]
#      slot: 1, minis: 2 chip: 1
#      [1657/0001,0104,2/8]
#      slot: 1, minis: 3 chip: 0
#      [1657/0001,0104,2/8]
#      slot: 1, minis: 3 chip: 1
#      [1657/0001,0104,2/8]
# 
#      > chippropshow 1/3
#      slot: 1, minis: 1 chip: 1
#      [1657/0001,0004,2/8]
#
# SEE ALSO
#      minispropshow(1d), ptpropshow(1d), setslot(1d)
# 
# NOTES
#      The  output  of this command is only for internal diagnostic
#      engineers and must not be interpreted by normal users in any
#      case.
# 

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`
if [ $multiBlade = TRUE ]
then
    syntax="`/bin/basename $0` [slot/]chip \| [ [--slot] slotno ] -all"
else
    syntax="`/bin/basename $0` chip \| -all"
fi

#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}
chip=$slot

dashslot_found=0
permslot_set=0
permchip_set=0
tempchip_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/port 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

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

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

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

    # In this case a number  has been set that originally looked
    # like it might be a chip number, but since -all was specified
    # we know that it really was a slot number. So transfer this
    # number to the appropriate variable
    if [ $tempchip_set -eq 1 ] ; then
      slot=$chip
    fi

	chip=$param   # chip variable now gets the "-all" string
	tempchip_set=1
	shift ; continue
    fi
done

#
# At this point, $chip should have a valid value and no parameter
# should remain.
#

if [ -z $chip ] ; then
    err "Invalid command"
    exit 4
fi

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

#
# chippropshow()
#
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 $slot not found."
   exit 3
fi
enableSlotProc $slot
if [ $? != 0 ] ; then exit 3 ; fi

_s=`/usr/bin/expr "$chip" : "[-]*all"`
if [ "0" -ne "$_s" ]
then
    let minis=0
    let chip=0
    # 8 chips is currently the max for a blade or board, however
    # some boards have less. This is handled below
    let total=8
else
    if [ $chip -lt 0 ] || [ $chip -gt 7 ] ; then
	err "Chip: $chip invalid number"
	exit 4
    else
	let minis="$chip / 2"
	let chip="$chip % 2"     
	let total=1
    fi
fi

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

file="$path/$minis/$chip"

if [ ! -e "$file" ] ; then
   err "minis: $minis chip: $chip not found$slot_message."
else
   let num=0
   while [ $num -lt $total ]; do
      prop="$path/$minis/$chip/property"

      # Not all boards have the maximum of 8 chips, so check that
      # the chip information is available before trying to display
      # unless the user has entered a specific chip number, then
      # try it and let the error message come out if it is not a
      # a valid chip number.

      if [ $total -eq 1 -o -e "$prop" ] ; then
        echo ""
        echo "$slot_message minis: $minis chip: $chip"
        /bin/cat $prop
      fi 

      let num="$num+1"
      let chip="$num % 2"
      let minis="$num / 2"
   done
fi
echo ""

# disable the proc entries of this slot
disableSlotProc $slot
