#!/bin/sh
#
#
#    Copyright (c) 2012 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#     chipportmap - disply chip port mappings
# 
# SYNOPSIS
#      chipportmap [slot/]chip
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will display port layouts and connections of a chip.
# 
# 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.
# 

# 

def_slot=0
shtyp=portmap

# 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
ok=0
for f in "./$portmapuitl" "$FABOSHOME/share/$portmapuitl" "$home/$portmapuitl" ; 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 [-cpt \<chip port\>]"
else
     syntax="`/bin/basename $0` chip [-cpt \<chip port\>]"
fi
#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}

dashslot_found=0
permslot_set=0
permchip_set=0
tempchip_set=0
chipport_found=0
chipport_select=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

    if [ $chipport_found -eq 1 ] ; then
	num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
	if [ -z $num_matched ] ; then
		port=$param
		chipport_select=1
		chipport_found=0
		shift ; continue
	else
	    echo "Invalid port number : $param"
	    exit 3
	fi
    fi

    chipport_matched=`echo $param | /bin/sed 's/[-]\{1,\}cpt//'`
    if [ -z $chipport_matched ] ; then
	chipport_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,\}\///'`
	permchip_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
done

#
# At this point, $port 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

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

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

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

#path="./"
# file=`/usr/bin/find $path -name "$shtyp" -print 2> /dev/null | /bin/grep asic$port/$shtyp 2> /dev/null`
file=$path/portmap
if [ -z $file ] ; then
   err "Port: $port not found."
else

	if [ $chipport_select -eq 0 ] ; then
		get_chip_line_in_minis_chip_port_list $file $chip
		echo ""
   		get_CH_portmap_list $file -CH $chip
	else
	   get_CH_portmap_list $file -CH $chip -CPt $port
    fi
   # /bin/cat $file
fi


