#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# macsec_show_techsupport_pd_cmn - PD common Show tech-support macsec
#
# June 2019
#
# Copyright (c) 2019 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------
. /pkg/bin/show_tech_main_fragment


if [ -f /pkg/lib/libsecy_driver_main.so ]; then
    if [ -f /tmp/cpa_optics_log.txt ]; then
        cp /tmp/cpa_optics_log.txt $__tar_file_directory_on_node/cpa_optics_log.txt
    fi
fi

    i=1
    #Determining card type to run the CLI  
    cardtype=`node_type`

    #libsecy_driver_main.so will be packaged only on supported LCs on modular
    #plattforms, on RP on fixed platforms
    if [ -f /pkg/lib/libsecy_driver_main.so ]; then
	
    	msc_clis_exec[i]='show macsec platform idb location $ext_node_name'
    	msc_clis__ksh[i]='platform_macsec_show_ea -n -l $fq_nodeid'
	    i=$(($i + 1))

        msc_clis_exec[i]='show controllers macsec idb location $ext_node_name'
        msc_clis__ksh[i]='secy_driver_show -o -l $fq_nodeid'
        i=$(($i + 1))
    
    	# convert location of form for ex :  0/1/CPU0 to 0/1/
	    node_loc=$(echo $ext_node_name | awk -F"/" '{print $1"/"$2"/"}')
	
	    # Get the list of interfaces registered in this node from IM.
	    intf_list_arr=$(im_show database | grep ^Interface| sed 's/,//' \
	        | cut -d" " -f2)
	
        # Get the list of interfaces in which we have macsec running.
        # These cannot be used directly for passing to client processes.
        # Store the interface in the form R/S/I/P  only.
        # They will be compared with interfaces on this node obtained from IM
        macsec_interfaces_from_mka=`show_macsec_mka -m -i all | tail -n+4 | \
	        tr -s ' '  | cut -d' ' -f2 | sed '/^$/d' | sed "s|.*$node_loc|$node_loc|"`

        # Iterate mka list of interfaces and compare with IM interface 
        for msc_if in $macsec_interfaces_from_mka; do
            msc_if_str=$(echo $intf_list_arr | sed 's/ /#\n/g' | \
	        grep $msc_if# | sed 's/#//')
	
	        if [[ $msc_if_str ]]; then
	            msc_if_str_internal=`convert_interface_fmt '-i' $msc_if_str`
	      
                msc_clis_exec[i]='show macsec mka statistics interface $msc_if_str'
                msc_clis__ksh[i]='show_macsec_mka -m -t intf -f $msc_if_str_internal'
                i=$(($i + 1))

                msc_clis_exec[i]='show macsec platform hardware flow interface $msc_if_str'
                msc_clis__ksh[i]='platform_macsec_show_ea -f -i $msc_if_str_internal -h'
                i=$(($i + 1))

                msc_clis_exec[i]='show macsec platform hardware sa interface $msc_if_str'
                msc_clis__ksh[i]='platform_macsec_show_ea -j -i $msc_if_str_internal -h'
                i=$(($i + 1))

                msc_clis_exec[i]='show macsec platform hardware stats interface $msc_if_str'
                msc_clis__ksh[i]='platform_macsec_show_ea -s -i $msc_if_str_internal -h'
                i=$(($i + 1))

                msc_clis_exec[i]='show macsec platform stats interface $msc_if_str'
                msc_clis__ksh[i]='platform_macsec_show_ea -s -i $msc_if_str_internal'
                i=$(($i + 1))

                msc_clis_exec[i]='show macsec secy stats interface $msc_if_str'
                msc_clis__ksh[i]='show_macsec_secy -s -f $msc_if_str_internal'
                i=$(($i + 1))

                msc_clis_exec[i]='show controllers phy stats interface $msc_if_str stats'
                msc_clis__ksh[i]='optics_driver_phy_show -s -i $msc_if_str_internal'
                i=$(($i + 1))

                msc_clis_exec[i]='show controllers $msc_if_str stats'
                msc_clis__ksh[i]='ethernet_show_controller -i $msc_if_str_internal -s stats'
                i=$(($i + 1))
            fi
        done

        msc_clis_exec[i]='show macsec platform trace all location $ext_node_name'
        msc_clis__ksh[i]='platform_macsec_show_trace -N -h $fq_nodeid'
        i=$(($i + 1))

        msc_clis_exec[i]='show controllers macsec trace all location $ext_node_name'
        msc_clis__ksh[i]='secy_driver_show_trace -N -h $fq_nodeid'
        i=$(($i + 1))
    fi

    if [ $cardtype == "RP" ]; then
        # Card type is RP
        node_loc=$(echo $ext_node_name | awk -F"/" '{print $1"/"$2"/"}')
        intf_list_arr=$(im_show database | grep ^Interface| sed 's/,//' \
             | cut -d" " -f2)
        macsec_interfaces_from_mka=`show_macsec_mka -m -i all | tail -n+4 | \
             cut -d' ' -f1 |sed '/^$/d' | sed "s|.*$node_loc|$node_loc|"`
        # Iterate mka list of interfaces and compare with IM interface 
        for msc_if in $macsec_interfaces_from_mka; do
		    #Getting port num of macsec interface
        	port_num=$(echo $msc_if | grep -o -E '[0-9/]+')
         	msc_if_str=`IFS=$'\n' ; echo "${intf_list_arr[*]}" | grep "$port_num"`
          	msc_if_str_internal=`convert_interface_fmt '-i' $msc_if_str`
          	if [[ $msc_if_str ]]; then
                msc_clis_exec[i]='show interfaces $msc_if_str'
                msc_clis__ksh[i]='show_interface -i $msc_if_str_internal'
                i=$(($i + 1))
            fi
	    done
     fi

     msc_clis_exec[i]='show process optics_driver location all'
     msc_clis__ksh[i]='sysmgr_show -o -p optics_driver -n all'
     i=$(($i + 1))
     
     msc_clis_exec[i]=''
     msc_clis__ksh[i]=''
     exec_commands msc_clis

. /pkg/bin/show_tech_main_fragment
