#!/bin/sh
#
#
#    Copyright (c) 2005 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      pterrshow - displays a summary of back-end port errors
#
# SYNOPSIS
#		pterrshow or pterrshow [slot> | all]
#
# AVAILABILITY
# 		all users
#
# DESCRIPTION
# 		This command will display a summary of back-end port errors.
#
# OPTIONS
#      slot
#           For bladed systems only, specify the slot number of the
#           blade back-end errors to be displayed.
#
#   all
#           For bladed systems only, specify the display of back-end
#           errors of all blades.
#

shtyp=pterrshow

# 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"
portmapuitl="debugcommon.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 system info
multiBlade=`isMultiBlade`
path="/proc/fabos/blade"
valid=1

if [ $multiBlade = TRUE ] ; then

	# Check for more than 1 or no argument
	if [ $# -gt 1 -o -z "$1" ] ; then
		valid=0
	fi

	# Check for numeric or "all" input
	if ! [ -z `echo $1 | tr -d "[:digit:]"` ]; then
		if [ $1 != "all" ] ; then
			valid=0
		fi
	fi
	
	if [ $valid -eq 0 ] ; then
		echo "Error - Invalid syntax."
    		echo "Usage: pterrshow <slot#> | all "
    		exit 1
	fi

	enabled_slots=$(/bin/ls $path)

	# Check enabled slots
	if [ -z "$enabled_slots" ] ; then
		echo "No enabled slots found. Blades absent or powered-off."
		exit 1
	fi

	if [ $1 != "all" ] ; then
		# check whether slot file path exists
		if ! [ -f $path/$1/$shtyp ] ; then
			echo "Error - pterrshow available for following enabled slot(s):"
			echo $enabled_slots
			echo "Usage: pterrshow <slot#> | all"
			exit 1
		fi
	fi

else
	# Check non empty argument
	if [ -n "$1" ] ; then
		echo "Error - Invalid syntax."
			echo "Usage: pterrshow "
			exit 1
	fi

	# check whether slot file path exists
	if ! [ -f $path/0/$shtyp ] ; then
		echo "Error - pterrshow data not found"
		exit 1
	fi
fi

# Display info of all enabled slots/desired slot.
if [ $multiBlade = TRUE ] ; then
		if [ $1 == "all" ] ; then
			cd $path
		/usr/bin/find . -name $shtyp | /usr/bin/xargs /bin/cat
	else
		/bin/cat $path/$1/$shtyp
	fi
else
	/bin/cat $path/0/$shtyp
fi
