#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech_icpe_local - Shared show tech-support fast script for ICPE local
# interfaces.
#
# January 2013, James Whistance-Smith
#
# Copyright (c) 2013-2017, 2020-2021 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------


#
# Load the script provided by show-tech infra, which provides worker functions
#
. /pkg/bin/show_tech_main_fragment

#
# Parse the arguments to the script - card type and controller ID are the
# only support options currently. No need to use the default parser function,
# as this has been done by caller (parsing at least location).
#
__cardtype="unspecified"
ctrlr="unspecified"
ctrlr_id="14"

while [ "$#" -gt "0" ]; do
    case "$1" in
        -t) __cardtype="$2"; shift 2;;
        -C) ctrlr_id="$2"; shift 2;;
        *) shift;;
    esac
done

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`node_type`
fi

#
# Parse the controller string from the controller ID.
#
case "$ctrlr_id" in
    0) 
     ctrlr="optics"
     process_ma="optics_ma"
     process_ea="optics_ea"
     ;;
    1)
     ctrlr="ots"
     process_ma="ots_ma"
     process_ea="ots_ea"
     ;;
    2)
     ctrlr="sonet"
     process_ma="sonet_ma"
     process_ea="sonet_ea"
     ;;
    3)
     ctrlr="otn"
     process_ma="otn_ma"
     process_ea="otn_ea"
     ;;
    4)
     ctrlr="odu-group"
     process_ma="odu_group_ma"
     process_ea="odu_group_ea"
     ;;
    5)
     ctrlr="eth-ctrlr"
     process_ma="eth_ctrlr_ma"
     process_ea="eth_ctrlr_ea"
     ;;
    6)
     ctrlr="macsec"
     process_ma="macsec_ma"
     process_ea="macsec_ea"
     ;;
    7)
     ctrlr="cpri"
     process_ma="cpri_ma"
     process_ea="cpri_ea"
     ;;
    8)
     ctrlr="osc"
     process_ma="osc_ma"
     process_ea="osc_ea"
     ;;
    9)
     ctrlr="dfb"
     process_ma="dfb_ma"
     process_ea="dfb_ea"
     ;;
    0xa)
     ctrlr="cem"
     process_ma="cem_ma"
     process_ea="cem_ea"
     ;;
    0xb)
     ctrlr="fc"
     process_ma="fc_ma"
     process_ea="fc_ea"
     ;;
    0xc)
     ctrlr="t1e1"
     process_ma="t1e1_ma"
     process_ea="t1e1_ea"
     ;;
    0xd)
     ctrlr="icpe-test"
     process_ma="icpe_local_test_ma"
     process_ea="icpe_local_test_ea"
     ;;
esac


#
# List of commands to be run.  Each 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.
#

###############################################################################
# Helper functions                                                            #
###############################################################################

# get_pid function. Given the process name, it will
# echo the pid for that process
# it will have code to take care of the host OS.
OS_STRING=`uname -s`
function get_pid {
    if [[ ${OS_STRING} = "QNX" ]]; then
        pid=`pidin | grep $1 | cut -c1-8 | head -n 1`
    else
        pid=`ps -ef | grep $1 | grep -v grep | awk '{print \$2}'`
    fi
    echo ${pid}
}


###############################################################################
# Show commands that run once per LR                                          #
###############################################################################

sys_exec[1]='show running-config'
sys__ksh[1]='nvgen -c -l 1 -t 1 -o 1'

# RP only, so use "location all"
sys_exec[2]='show redundancy location all'
sys__ksh[2]='redcon_show -n all'

sys_exec[3]='show logging'
sys__ksh[3]='show_logging'

if [[ "$platform" = "panini" ]]; then
    sys_exec[4]='show install active'
    sys__ksh[4]='sdr_instcmd show install active'
else
    sys_exec[4]='show install active summary'
    sys__ksh[4]='instcmd show install active summary'
fi

sys_exec[5]='show configuration history'
sys__ksh[5]='cfgmgr_show_history -s -f -n 0x5dc -t all -b'

sys_exec[6]=''
sys__ksh[6]=''


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

rplc_exec[1]='show icpe-internal local controller $ctrlr trace location $location'
rplc__ksh[1]='icpe_local_show_trace -C $ctrlr_id'

rplc_exec[2]='show icpe-internal local controller $ctrlr db location $location'
rplc__ksh[2]='icpe_local_show_db -C $ctrlr_id'

rplc_exec[3]='show process $process_ma'
rplc__ksh[3]='sysmgr_show -o -p $process_ma'

rplc_exec[4]='show process $process_ea'
rplc__ksh[4]='sysmgr_show -o -p $process_ea'

rplc_exec[5]='show process blocked'
if [[ "$platform" = "panini" ]]; then
    rplc__ksh[5]='sh_proc_ng_blocked'
else
    rplc__ksh[5]='show_processes -b'
fi

# Specify context 11 to display dumper context for last 10 crashes (not in CLI)
rplc_exec[6]='show context'
if [[ "$platform" = "panini" ]]; then
    rplc__ksh[6]='corehelper_context -c 0xb -n all'
else
    rplc__ksh[6]='dumper_context -c 0xb'
fi

# Display all API stats information for all controllers.
rplc_exec[7]='show icpe-internal local controller all api-stats all'
rplc__ksh[7]='icpe_local_show_api_stats -P 0x8 -g 0x0 -S'

# Display all interface status information for all controllers.
rplc_exec[8]='show icpe-internal local controller all status detail'
rplc__ksh[8]='icpe_local_show_status -C 0x8 -D'

test_cmd_idx=9

#
# Find the process IDs of the MA and EA
#
process_ma_pid=`get_pid $process_ma`
process_ea_pid=`get_pid $process_ea`

#
# Follow (attach_process) Commands
#

if [ -n "$process_ma_pid" ]
then
    rplc_exec[$test_cmd_idx]="follow process $process_ma iteration 1"
    rplc__ksh[$test_cmd_idx]="attach_process -p $process_ma_pid -i 1"
    let test_cmd_idx+=1
fi

if [ -n "$process_ea_pid" ]
then
    rplc_exec[$test_cmd_idx]="follow process $process_ea iteration 1"
    rplc__ksh[$test_cmd_idx]="attach_process -p $process_ea_pid -i 1"
    let test_cmd_idx+=1
fi

rplc_exec[$test_cmd_idx]=''
rplc__ksh[$test_cmd_idx]=''


#
# The display() function is the one that does all the work - called by us as
# this is a worker script.
#
display() {
    print_main_heading "show tech-support icpe-internal local $ctrlr"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "DRP"|"RP"|"LC")
            exec_commands rplc
            ;;
        esac
    fi

    print_main_heading "show tech-support icpe-internal local $ctrlr complete"
}

#
# Only display the show tech info if a valid controller process ID has been
# parsed.
#
if [ "$ctrlr_id" -lt "14" ]; then
    display
fi

