#!/bin/bash
# -----------------------------------------------------------------------------
# tech_fabric                     
#                                      
# Copyright (c) 2012-2022 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------

#
# Load the script provided by show-tech infra, which provides worker functions
#
source /opt/cisco/calvados/script/show_tech_main_fragment
source /etc/rc.d/init.d/spirit_pd.sh

#
# Parse the arguments to the script - card type and interface filter are the 
# only support options currently.  No need to use the default parser function,
# as this has been done by caller.
#
__cardtype="unspecified"
trace_only="0"
show_only="0"
fc2_string="FC2"
ufi_string="S9705-48D-4"
rack=""
wb_cluster=0
qsfpdd_port_DCF=47
qsfpdd_port_DCP=12

while [ "$#" -gt "0" ]; do
    case "$1" in
        -T) trace_only="1"; shift 1;;
        -S) show_only="1"; shift 1;;
        -t) __cardtype="$2"; shift 2;;
        -f) showtech_dir="$2"; shift 3;;
        *) shift;;
    esac
done

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`/opt/cisco/calvados/script/node_type`
fi

#
# Dynamically generate show tech cli based on
# cards/asics present in the system
#
PLATFORM_OUTPUT="/tmp/show_platform.output"
chassis_info_filename="/tmp/chassis_info.output"
drv_info_prefix="/tmp/sfe_driver_info_"
declare -a rp_dyn_show_exec
declare -a rp_dyn_show__ksh
declare -a drv_info_filename 
declare -a fc_list
declare -a fc_type
declare -a lc_list
declare -a fgid_id_list

cli_count=0
rack_count=0
max_instance=5
fc_in_RP=0
fc_plat=1
SFE_DRVR_INFO="/tmp/sfe_drvr.output"
result=0

# generate_commands function takes cli string as input
# to set rp_dyn_show_exec and rp_dyn_show__ksh array.
# show tech infra will use them to execute commands
generate_commands () {
    cli_count=$(($cli_count + 1)) 
    cli_string=$@
    rp_dyn_show_exec[$cli_count]=$(eval "echo $cli_string")
    rp_dyn_show__ksh[$cli_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; '"$(eval "echo $cli_string")"'"'
}

# generate_diagshell_commands function takes cli string as input
# to set rp_dyn_show_exec and rp_dyn_show__ksh array.
# show tech infra will use them to execute commands
# this is used for diagshell commands
generate_diagshell_commands () {
    cli_count=$(($cli_count + 1))
    cli_string=$@
    rp_dyn_show_exec[$cli_count]=$(eval "echo \${cli_string}")
    rp_dyn_show__ksh[$cli_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; '$cli_string'"'
}

#Generate the register dump for all the SFE's available in the system 
generate_register_dump () {

    if [[ $fc_in_RP -eq 1 ]] ; then
        #zermatt cards max have only 2 instance
        for instance in `seq 0 1`;
        do
          cli="show controller sfe register-dump instance $instance location 0/RP0"
          generate_commands $cli
        done
    else 
        # Register Dumps for all FC and for all instances
        for fc in ${fc_list[*]};
        do
           for instance in `seq 0 $max_instance`;
           do
             cli="show controller sfe register-dump instance $instance location $fc"
             generate_commands $cli
           done
        done
    fi 
}

#Generate qsfpdd summary, specific to whitebox
generate_qsfpdd_summary () {
    for fc in ${fc_list[*]}; do
        fc_short=$(echo $fc| cut -d'/' -f 1)
        cli="show controller fabric qsfpdd summary rack $fc_short | save qsfpdd_summary_${fc_short}.txt"
        /opt/cisco/calvados/bin/show_cmd "terminal length 0; $cli" 
    done
    for lc in ${lc_list[*]}; do
        lc_short=$(echo $lc| cut -d'/' -f 1)
        cli="show controller fabric qsfpdd summary rack $lc_short | save qsfpdd_summary_${lc_short}.txt"
        /opt/cisco/calvados/bin/show_cmd "terminal length 0; $cli"
        done
}

#Generate DCP list specific to whitebox
generate_lc_list() {
    /opt/cisco/calvados/bin/show_cmd "terminal length 0; show platform" > $PLATFORM_OUTPUT
    let lc_list_cnt=0
    for loc in $(grep "[0-47]/0 .*OPERATIONAL" $PLATFORM_OUTPUT | awk '{ print $1}'); do
        lc_list[$lc_list_cnt]=$loc
        lc_list_cnt=$((lc_list_cnt+1))
    done
}

#Generate qsfpdd dom, specific to whitebox
generate_qsfpdd_dom() {
    for fc in ${fc_list[*]}; do
        fc_short=$(echo $fc| cut -d'/' -f 1)
        for ((i=0;i<=qsfpdd_port_DCF;i++)); do
            cli="show controller fabric qsfpdd dom location $fc qsfpdd_port $i | save qsfpdd_dom_${fc_short}_${i}.txt"
            /opt/cisco/calvados/bin/show_cmd "terminal length 0; $cli"
        done
    done

    for lc in ${lc_list[*]}; do
        lc_short=$(echo $lc| cut -d'/' -f 1)
        for ((i=0;i<=qsfpdd_port_DCP;i++)); do
            cli="show controller fabric qsfpdd dom location $lc qsfpdd_port $i | save qsfpdd_dom_${lc_short}_${i}.txt" 
            /opt/cisco/calvados/bin/show_cmd "terminal length 0; $cli"
        done
    done
}

#Generate the Link Info  and Diagshell output for FC cards and Zermat 
generate_dynamic_commands () {
    if [[ $fc_in_RP -eq 1 ]] ; then
        # Zermatt Cards max have 2 instance
        for instance in `seq 0 1`;
        do
            cli="show controller sfe link-info rx 0 143 topo instance $instance location 0/RP0"
            generate_commands $cli
            cli="show controller sfe link-info rx 0 143 topo instance $instance location 0/RP0 detail"
            generate_commands $cli
            diagshell_command_1='fabric connectivity; ps; fabric link_config all; fabric link all; fabric link all; diag counters nz; diag counters nz'
            cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_1\"'' location 0/RP0'
            generate_diagshell_commands $cli
            
        done
        instance=0
        # running config show & show patches  only for one instance
        diagshell_command_3='config show; show patches'
        cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_3\"'' location 0/RP0'
        generate_diagshell_commands $cli
    else 
        let count=0
        for fc in ${fc_list[*]};
        do
            for instance in `seq 0 $max_instance`;
            do
               if [[ ${fc_type[$count]} =~ $fc2_string ]] || [[ ${fc_type[$count]} =~ $ufi_string ]]; then
                  cli="show controller sfe link-info rx 0 191 topo instance $instance location $fc"
                  generate_commands $cli
                  cli="show controller sfe link-info rx 0 191 topo instance $instance location $fc detail"
                  generate_commands $cli
                  diagshell_command_1='fabric connectivity; port status ; fabric link config all; fabric link status; diag counter nz; show counter'
                  cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_1\"'' location '$fc''
                  generate_diagshell_commands $cli
                  if [[ $fc_type[$count]} =~ $ufi_string ]] ; then
                      diagshell_command_4='date'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_4\"'' location '$fc''
                      generate_diagshell_commands $cli
                      diagshell_command_5='show pvt'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_5\"'' location '$fc''
                      generate_diagshell_commands $cli
                      diagshell_command_6='phy dsc config sfi'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_6\"'' location '$fc''
                      generate_diagshell_commands $cli
                      diagshell_command_11='phy dsc sfi'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_11\"'' location '$fc''
                      generate_diagshell_commands $cli
                      diagshell_command_7='counter on'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_7\"'' location '$fc''
                      generate_diagshell_commands $cli
                      diagshell_command_8='phy fecstat ber'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_8\"'' location '$fc''
                      generate_diagshell_commands $cli
                      diagshell_command_9='phy fecstat counter'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_9\"'' location '$fc''
                      generate_diagshell_commands $cli
                      diagshell_command_10='counter off'
                      cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_10\"'' location '$fc''
                      generate_diagshell_commands $cli
                  fi
               else 
                  cli="show controller sfe link-info rx 0 143 topo instance $instance location $fc"
                  generate_commands $cli
                  cli="show controller sfe link-info rx 0 143 topo instance $instance location $fc detail"
                  generate_commands $cli
                  diagshell_command_2='fabric connectivity; ps; fabric link_config all; fabric link all; fabric link all; diag counters nz; diag counters nz'
                  cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_2\"'' location '$fc''
                  generate_diagshell_commands $cli
               fi   
            done

            instance=0
            # running config show & show patches  only for one instance
            diagshell_command_3='config show; show patches'
            cli='show controller sfe diagshell '$instance' ''\"$diagshell_command_3\"'' location '$fc''
            generate_diagshell_commands $cli
            count=$((count+1))
        done
    fi
}


generate_plat_data () {
   /opt/cisco/calvados/bin/show_cmd "terminal length 0; show platform" > $PLATFORM_OUTPUT

    let count=0
    let fc_list_cnt=0
    let fc_type_cnt=0
    for loc in $(grep "$rack/FC[0-9] .*OPERATIONAL" $PLATFORM_OUTPUT | awk '{ print $1 " " $2 }'); do  
       if [ $(( $count % 2 )) -eq 0 ] ; then
          fc_list[$fc_list_cnt]=$loc
          fc_list_cnt=$((fc_list_cnt+1))
       else
          fc_type[$fc_type_cnt]=$loc
          fc_type_cnt=$((fc_type_cnt+1))
       fi  
          count=$((count+1))
    done
    
    rack_type=$(grep "$rack/RP0 .*OPERATIONAL" $PLATFORM_OUTPUT | awk '{ print $2 }')
    
}

is_wb () {
    let count=0
        for fc in ${fc_list[*]}; do
            if  [[ ${fc_type[$count]} =~ $ufi_string ]] ; then
                max_instance=1
                wb_cluster=1
                break
            fi
            count=$((count+1))
        done
}

#check if platforms is without SFE. 

is_fc_plat() {
    
    /opt/cisco/calvados/bin/show_cmd "terminal length 0; describe show controller sfe driver" > $SFE_DRVR_INFO
    result=$(grep "syntax error:" $SFE_DRVR_INFO)   
   
    if [[ $result == syntax* ]]; then
        fc_plat=0
    else
        fc_plat=1
    fi
    rm -rf $SFE_DRVR_INFO 2> /dev/null
}


is_fc_in_RP() {
    if [[ $rack_type == "NCS-5502" ]]; then
        fc_in_RP=1
    else
        fc_in_RP=0
    fi
}

generate_fgid_id_error () {

    cli='show controller fabric fgid program-error all | save fgid_program_error.txt'
    /opt/cisco/calvados/bin/show_cmd "terminal length 0; $cli"
    echo "======================"
    echo "show controller fabric fgid program-error all cli output is present in fgid_program_error.txt file"
    echo "======================"
    num=$(grep -o -i Fgids:Ok /misc/disk1/fgid_program_error.txt | wc -l)
    if [[ $num -eq $fc_list_cnt ]];
    then
        echo "========================="
        echo "no fgid errors found"
        echo "========================="
    else
        echo "====== error found in fgid programming for some fgid ids ======"
        fgid_ids=$(grep -Eo '[0-9\]+' /misc/disk1/fgid_program_error.txt)
        let fgid_id_cnt=0
        for number in $fgid_ids; do
            if [[ $((10#$number)) -gt  999 ]];
            then
                fgid_id_list[$fgid_id_cnt]=$((10#$number))
                fgid_id_cnt=$((fgid_id_cnt+1))
            fi
        done
        fgid_id_list=($(echo "${fgid_id_list[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

        let cnt=0
        for id in ${fgid_id_list[*]}; do
            if [[ $cnt -eq 5 ]];
            then
                break
            fi
            cli="show controller fabric fgid information id $id diagnostics"
            generate_commands $cli
            cnt=$((cnt+1))
        done
    fi
}
#
# List of commands to be run.  East set must finish with a pair of empty 
# strings.  Note that it is important to use single quotes rather than double 
# quotes for the strings containing your commands.
#
# For all of these the __ksh variable is the process that will actually be 
# spawned, the _exec variable is just a string that is printed in the output to
# describe it. 
#

###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################

generate_show_commands () {
    #
    #
    # Show commands
    #
    #
    rp_show_count=1

    rp_show_exec[$rp_show_count]='show version'
    rp_show__ksh[$rp_show_count]='cat /etc/show_version.txt'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show running-config'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show running-config"'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show install active'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show install active"'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show platform'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show platform"'
    ((rp_show_count++))

    if [[ $fc_plat -eq 1 ]] ; then
        rp_show_exec[$rp_show_count]='show controller fabric plane all detail'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric plane all detail"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller fabric plane all statistics detail'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric plane all statistics detail"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller fabric fsdb-pla rack all'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric fsdb-pla rack all"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller fabric health'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric health"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller fabric link port fia rx all'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric link port fia rx all"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller fabric link port s1 rx all'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric link port s1 rx all"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller fabric link port s3 tx all'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric link port s3 tx all"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller sfe driver location all'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller sfe driver location all"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show process sfe_driver location all'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show process sfe_driver location all"'
        ((rp_show_count++))
    
    fi
    
    if [[ $wb_cluster -eq 1 ]] ; then
        rp_show_exec[$rp_show_count]='show controller fabric bundle connection all'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric bundle connection all"'
        ((rp_show_count++))

        rp_show_exec[$rp_show_count]='show controller fabric link port s1 rx statistics detail'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric link port s1 rx statistics detail"'
        ((rp_show_count++))

    fi

    rp_show_exec[$rp_show_count]='show process fgid_mgr location all'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show process fgid_mgr location all"'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show chassis'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show chassis"'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show controller fabric fgid statistics all brief'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric fgid statistics all brief"'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show controller fabric fgid resource all'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show controller fabric fgid resource all"'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show process blocked location all'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show process blocked location all"'
    ((rp_show_count++))

    rp_show_exec[$rp_show_count]='show context'
    rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show context"'
    ((rp_show_count++))


    if [[ $fc_in_RP -eq 1 ]] ; then
        rp_show_exec[$rp_show_count]='show asic-errors SFE 0 all location 0/RP0'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show asic-errors SFE 0 all location 0/RP0"'
        ((rp_show_count++))
        rp_show_exec[$rp_show_count]='show asic-errors SFE 1 all location 0/RP0'
        rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show asic-errors SFE 1 all location 0/RP0"'
        ((rp_show_count++))

    else
        for fc in ${fc_list[*]}; do
            rp_show_exec[$rp_show_count]='show asic-errors SFE all all save location '$fc''
            rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show asic-errors SFE all all save location '$fc'"'
            ((rp_show_count++))

        done
    fi 

     # General information
     rp_show_exec[$rp_show_count]='show logging'
     rp_show__ksh[$rp_show_count]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show logging"'
     ((rp_show_count++))
}


#
# The display() function is the one that does all the work - called by us as 
# this is a worker script.
#
display() {
    print_main_heading "General fabric tech-support info"
        
	   case "$__cardtype" in
        "RP" | "CC")
           generate_plat_data
           is_fc_in_RP
           is_fc_plat
           is_wb
           generate_show_commands
           if [[ $fc_plat -eq 1 ]] ; then
               if [[ $wb_cluster -eq 1 ]] ; then
                   generate_fgid_id_error
               fi
               generate_register_dump
               generate_dynamic_commands
           fi
           if [[ $wb_cluster -eq 1 ]] ; then
               generate_lc_list
               generate_qsfpdd_summary
               generate_qsfpdd_dom
           fi
           exec_commands rp_dyn_show
           exec_commands rp_show

           
           if [[ $fc_in_RP -eq 1 ]] ; then
                # Zip all sfe logs for Zermat Cards
                count=`ls -1 /root/sfe*.txt 2> /dev/null | wc -l`
                if [ $count != 0 ] 
                then
                   gzip -f /root/sfe*.txt 2> /dev/null
                   for i in `ls /root/sfe*.gz 2> /dev/null`
                   do
                   mv $i $showtech_dir/
                   #scp -p -q $i $showtech_dir/ >& /dev/null
                   done
                fi

                # Zip all sfe init debug logs
                if [ -d "/misc/scratch/sfe" ]
                then
                   tar cfz "/misc/scratch/sfe_debug.tgz" -C "/misc/scratch/" "sfe/" >& /dev/null
                   mv /misc/scratch/sfe_debug.tgz $showtech_dir/ >& /dev/null
                fi
           else
                #copy the register dump and sfe debug logs from FC
                for fc in ${fc_list[*]}; do
                    if [[ $wb_cluster -eq 1 ]] ; then
                        cli="show vm location $fc"
                        str_cli=$(/opt/cisco/calvados/bin/show_cmd "terminal length 0; $cli")
                        ip_address="$(grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' <<< "$str_cli")"
                    else
                        last="${fc: -1}"
                        value=$(($last*4))
                        ip=$(($value+84))
                        ip_address=192.0.$ip.1
                    fi
                    #copy the sfe register dump files to show tech folder
                    scp -p -q $ip_address:/root/sfe*.txt $showtech_dir/ 

                    #below code commented out- Since logs are not available
                    # in the mentioned path. It is tested with the file
                    # and folder created under the misc/scratch location.
                    # Can un-comment the code once the logs are added in 
                    # the code.Otherwise it throw an error that 
                    #/misc/scratch/sfe no such file or directory
                    #dont have provision to check FC have that file or not
                    #from RP.
                    #copy the sfe debug logs if it available
                    #fc_info=${fc/\//_}
                    #sfe="sfe_debug_"$fc_info
                    #file_path="$showtech_dir/$sfe"
                    #mkdir "${file_path}"
                    #scp -p -q -r $ip_address:/misc/scratch/sfe/* $file_path/
                    #tar cfz "/${file_path}.tgz" -C "$showtech_dir/" "${sfe}/" >& /dev/null
                    #rm -rf "${file_path}"
                    #un-comment the above code once the debug logs get 
                    #enabled for /misc/scratch/sfe location
                done
                count=`ls -1 /tmp/asic_errors* 2> /dev/null | wc -l`
                if [ $count != 0 ] 
                then
                   cp /tmp/asic_errors* $showtech_dir/
                fi
                #Zip all the sfe logs
                gzip -f $showtech_dir/sfe*.txt 2> /dev/null
                if [[ $wb_cluster -eq 1 ]] ; then
                    #copy program-error cli output
                    cp /misc/disk1/fgid_program_error.txt $showtech_dir/
                    #copy qsfppd cli output to showtech folder
                    mkdir $showtech_dir/qsfpdd
                    cp /misc/disk1/qsfpdd_*.txt $showtech_dir/qsfpdd
                    #zip all the qsfpdd summary and qsfpdd dom cli output
                    gzip -f $showtech_dir/qsfpdd/qsfpdd_*.txt 2> /dev/null
                    gzip -f $showtech_dir/fgid_program_error.txt 2> /dev/null
                    rm /misc/disk1/qsfpdd_*.txt
                    rm /misc/disk1/fgid_program_error.txt
                fi
           fi
           ;;
        "FC")
           lspci -tv | grep Broadcom
           top  -n 1 -b -H 
           sleep 2s
           top  -n 1 -b -H 
           sleep 2s
           top  -n 1 -b -H 
           cat /proc/linux-kernel-bde
           cat /proc/linux-user-bde
           pd_get_card_inst CARD_INST
           if [ "$CARD_INST" == "IOSXRWBD-FC" ]; then
                > /tmp/cli_dump_bcm81358
                echo "+++`date '+%d/%m/%Y %T'`: Retimer Log collection Started"
                echo "`date '+%d/%m/%Y %T'`: Collecting Link status for BCM81358"

                # We have 12 ports connected via BCM81358 retimers
                for retimer_port in `seq 0 11`;
                do
                    /opt/cisco/calvados/sbin/bcm81358_dbg_cli -o 3 -p "$retimer_port" > /dev/null
                done

                cat /tmp/cli_dump_bcm81358

                echo "`date '+%d/%m/%Y %T'`: Collecting Phy status dump for BCM81358"
                start_wc=0
                if [ -f "/tmp/trace_dump_bcm81358" ]; then
                   start_wc=`wc -l /tmp/trace_dump_bcm81358  | tr -s " " | cut -d " " -f1`
                fi

                for retimer_port in `seq 0 11`;
                do
                    /opt/cisco/calvados/sbin/bcm81358_dbg_cli -o 10 -p "$retimer_port" > /dev/null
                done

                end_wc=0

                if [ -f "/tmp/trace_dump_bcm81358" ]; then
                    end_wc=`wc -l /tmp/trace_dump_bcm81358  | tr -s " " | cut -d " " -f1`
                fi

                tail_cnt=0
                if [ $end_wc -ge $start_wc ]; then
                    tail_cnt=`expr $end_wc - $start_wc`
                fi
                tail -"$tail_cnt" /tmp/trace_dump_bcm81358

                > /tmp/cli_dump_bcm81358
                echo "`date '+%d/%m/%Y %T'`: Collecting AN status for BCM81358"
                for retimer_port in `seq 0 11`;
                do
                    /opt/cisco/calvados/sbin/bcm81358_dbg_cli -o 11 -p "$retimer_port" > /dev/null
                done

                cat /tmp/cli_dump_bcm81358

                echo "----`date '+%d/%m/%Y %T'`: Retimer Log collection Ended"
           fi
           ;;
        "LC")
            ;;
        esac
            
    print_main_heading "General fabric tech-support info complete"
}

display
