#!/bin/sh
#
#
#    Copyright (c) 1996-2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   supportShow.sh.in
#    Module name: diag/scripts
#
# NAME
#      supportshow  -  display  multiple  test results for customer
#      debugging
# 
# SYNOPSIS
#      supportshow slot[/port1-port2] [ lines ]
# 
# AVAILABILITY
#      all users
# 
# DESCRIPTION
#      This command will display multiple pre-selected telnet  user
#      commands results with a range of ports specified in the user
#      input.
# 
# OPTIONS
#      slot[/port1-port2]
#                Specify the range of ports within the blade speci-
#                fied  by  slot  to  be displayed. slot must be provided
#		 except for non-Ulysess products.
# 
#      lines     Specify the number of lines for  portlogdump  out-
#                put.
# 
# EXAMPLES
#      > supportshow 1/1-3
#      version:
#      Kernel:     2.4.2
#      Fabric OS:  v4.0.0t4Feb
#      Made on:    Wed Feb 6 17:29:57 2002
#      Flash:      Thu Jan 1 00:03:19 1970
#      BootProm:   3.1.13b
# 
#      uptime:
#        9:59pm  up 7 days,  4:25,  2 users,  load average: ....
# 
#      tempshow:
# 
#      Index   Slot    State           Centigrade     Fahrenheit
#      =========================================================
#        1      1      Ok                  41            105
#        2      2      Ok                  39            102
#        3      3      Ok                  40            104
#      --- <output truncated> ---
# 
# SEE ALSO
#      portlogdump(1m), setslot(1d)
# 


def_slot=0

#
#
# 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



#
#
# define the syntax based on the platform
#
product=`getSWBD`
if [ "$product" = "SWBD10" ] ; then
     syntax="`/bin/basename $0` slot[/port1-port2] [ lines ]"
else
     syntax="`/bin/basename $0` [ port1 - port2 ] [ lines ]"
fi



#
# start_show()
#
# input : $begport, $endport
#


start_show() {
#fabstateshow()
echo "fabstateshow"
fabstateshow
echo ""

#fabswitchshow()
echo "fabswitchshow"
fabswitchshow
echo ""

# 1. versionshow()
echo "version:"
version
echo ""
 
# 2. uptime()
echo "uptime:"
uptime
echo ""
 
# 3. tempshow()
echo "tempshow:"
tempshow
echo ""

# 4. psshow()
echo "psshow:"
psshow
echo ""

# 5. licenseshow
echo "licenseshow:"
licenseshow
echo""

# 7. errdump()
echo "errdump -a:"
errdump -a
echo ""

echo "errdump -p:"
errdump -p
echo ""

# 8. switchShow()
echo "switchshow:"
switchshow
echo ""

# 9. fspfShow()
echo "fspfshow:"
fspfshow
echo ""

# 10.  portFlagsShow()
echo "portflagsshow:"
portflagsshow
echo ""

# 11. portErrShow()
echo "porterrshow:"
porterrshow
echo ""

echo "fabricshow:"
fabricshow
echo ""

echo "trunkshow:"
trunkshow
echo ""

echo "topologyshow:"
topologyshow -1
echo ""

echo "nsshow:"
nsshow
echo ""

echo "nsallshow:"
nsallshow
echo ""

echo "cfgshow:"
cfgshow
echo ""

#slotshow()
if [ "$product" = "SWBD10" ] ; then
echo "slotshow"
slotshow
echo ""
fi

#chassisshow()
echo "chassisshow"
chassisshow
echo ""

#fanshow()
echo "fanshow"
fanshow
echo ""

#historyshow()
echo "historyshow"
historyshow
echo ""

#Dump information regarding kernel, packages, network device, filesystem, etc
#start_linuxinformation

echo "running user commands"
cmds="/sbin/bootenv,/sbin/sin,/bin/df,/sbin/ifconfig,/sbin/route"

for cmd in `echo $cmds | /usr/bin/tr "," "\n"`; do
echo "$cmd"; $cmd; echo "";
done

#tr did not like the spaces
echo "mii-tool -vv"
/sbin/mii-tool -vv
echo ""

echo "/usr/bin/du -xh / | /bin/sort"
/usr/bin/du -xh / | /bin/sort
echo ""

echo "/bin/ps -elfH"
/bin/ps -elfH
echo ""cfcfg

echo "/bin/rpm -qa"
/bin/rpm -qa
echo ""

# Why not use the dmesg command, cause the log is overwritten by other kernel
# modules and we need the linux kernel info.
echo "/bin/cat /var/log/dmesg"
/bin/cat /var/log/dmesg
echo ""

echo "/bin/cat /etc/fstab"
/bin/cat /etc/fstab
echo ""

echo "/bin/cat /etc/mtab"
/bin/cat /etc/mtab
echo ""

echo "printing proc entries"
proc_entries="cmdline,cpuinfo,devices,filesystems,interrupts,meminfo,modules,"\
"mounts,mtd,partitions,pci,slabinfo,stat,tty/drivers,uptime,"\
"version"

for proc_entry in `echo $proc_entries | /usr/bin/tr "," "\n"`; do
echo "/proc/$proc_entry"; /bin/cat /proc/$proc_entry; echo "";
done

#end_linuxinformation

#configshow
echo "configshow"
/fabos/bin/configShow | /bin/cat # the cat at end of pipe avoids more's paging
echo""

#last one portlogdump
echo "portlogdump"
if [ $nlog_set -eq 1 ] ; then
   portlogdump $nlog
else
     portlogdump
fi
echo""
echo""
}


start_show_slot()
{
#diagshow()
if [ "$product" = "SWBD10" ] ; then
   echo "diagshow of slot $slot:"
   diagshow --slot $slot
else
   echo "diagshow:"
   diagshow
fi
echo ""

#
#display info by port #port
#
let total="$endport-$begport+1"
let port="$begport"
if [ "$product" = "SWBD10" ] ; then
  str=$slot
else
  str=""
fi

while [ $total -gt 0 ]
do
#
# need to check if port #port is active
#
     echo "portshow $str $port:"
     portshow --slot $slot $port
     echo ""

     echo "portregshow $str $port:"
     portregshow --slot $slot $port
     echo ""
     
     echo "portrouteshow $str $port:"
     portrouteshow --slot $slot $port
     echo ""

    let port="$port+1"
    let total="$total-1"
done

}



#
# Preprocessing command arguments 
#

slot=${FABOS_SLOTNO:-$def_slot}

dashslot_found=0
permslot_set=0
permport_set=0
tempport_set=0
nlog_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

    #
    #
    # permslot_set and permport_set will be set
    # when pattern "slot/port1-port2" is matched
    #
    #
    slotport_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}\/[0-9]\{1,\}\-[0-9]\{1,\}//'`
    if [ -z $slotport_matched ] ; then
	slot=`echo $param | /bin/sed 's/\/[0-9]\{1,\}\-[0-9]\{1,\}//'`
	begport=`echo $param | /bin/sed 's/[0-9]\{1,\}\///'| /bin/sed 's/\-[0-9]\{1,\}//'`
	endport=`echo $param | /bin/sed 's/[0-9]\{1,\}\/[0-9]\{1,\}\-//'`
	permslot_set=1
	permport_set=1
	shift ;	continue
    fi

    #
    #
    # tempslot_set will be set
    # when pattern "port1-port2" is matched
    #
    #
    num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}\-[0-9]\{1,\}//'`
    if [ -z $num_matched ] ; then
	if [ $permport_set -ne 1 ] && [ $tempport_set -ne 1 ] ; then
	    begport=`echo $param | /bin/sed 's/\-[0-9]\{1,\}//'`
	    endport=`echo $param | /bin/sed 's/[0-9]\{1,\}\-//'`
	    tempport_set=1
	    shift ; continue
	fi
    fi


    #
    #
    # nlog_set will be set in
    # Terminator: when the first INDIVIDUAL number is matched;
    #
    # Ulysess: when the first number is matched as SLOT_NUMBER
    #          and the second is matched as nlog number.
    #
    #
    num_matched=`echo $param | /bin/sed 's/[0-9]\{1,\}//'`
    if [ -z $num_matched ] && [ $nlog_set -ne 1 ] ; then
       if  [ "$product" = "SWBD12" ] ; then
	    nlog=$param
	    nlog_set=1
       else
	    if [ "$product" = "SWBD10" ] && [ $slot -eq $def_slot ] \
	       && [ $tempport_set -ne 1 ] ; then
		 slot=$param
	    else
		  nlog=$param
		  nlog_set=1
	    fi
       fi	  
       shift ; continue
    fi


    #
    #
    # if nothing fits
    # then it is some junk
    #
    err "bad input"
    exit 4

done


#
#
# slot is mandatory for Ulysess
#
# but we have to support none-parameter for supportshow
# in Ulysess
if [ $slot -eq $def_slot ] && [ "$product" = "SWBD10" ] ; then
   begport=0
   endport=15
   start_show
   all_slots=`getAvailSlots`
   for a_slot in $all_slots
   do
     slot=$a_slot
     start_show_slot
   done
   exit 0
fi


#
#
#
# slot must be zero for terminator
#
if [ $slot -ne $def_slot ] && [ "$product" = "SWBD12" ] ; then
   err "redundant slot number"
   exit 4
fi



#
#
#
# set port range if it is not set in input
#
#
if [ $tempport_set -ne 1 ] && [ $permport_set -ne 1 ] ; then

      if [ "$product" = "SWBD12" ] ; then
	  begport=0
	  endport=31
      else
	  begport=0
	  endport=15
      fi


fi


#
#
# check on port range validity
# Ulysses: port number should be within range (0. 15)
# termniator: range should be (0. 31)
#
if [ $begport -gt 31 ] || [ $endport -gt 31 ] || [ $begport -gt $endport ] ; then
     err "invalid port range"
     exit 4
fi

if [ $begport -gt 15 ] || [ $endport -gt 15 ] ; then
   if [ $slot -ne $def_slot ] ; then
      err "invalid port range"
      exit 4
   fi
fi


#
#
# execute start_show
# if nlog is set
# do portlogdump
#
start_show
start_show_slot


