#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech_ptp_main - Show tech-support fast script for ppp
#
# December 2008, Matthew Edwards
#
# Copyright (c) 2008-2011, 2017-2018, 2021 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------

#
# This script runs most of the show_tech_ppp_main script commands, but where 
# possible, they are run on individual RPs/LCs to save time.
#

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

#
# Parse the arguments to the script - in particular, to work out what node type
# we are running on and any interface filter.
#
# The args passed to this script seem to get easily mixed in with the show tech
# arguments, so make no assumption about ordering of arguments here - and pass
# anything unhandled over to showtech infra.
#
internal_if="unspecified"
external_if=""
arg_string=""
showtech_argv=""
showtech_argc="0"

while [ $# -gt 0 ]; do
    case "$1" in
        -I*) internal_if="${1#??}"; shift 1;;
        *) showtech_argv="$showtech_argv $1"; (( showtech_argc+=1 )); shift 1;;
    esac
done

#
# Call the showtech infra default parser - using set to recreate the $#, $@, 
# etc. - "--" is necessary so set doesn't try to interpret any of the 
# arguments we are passing through!
#
if [[ $showtech_argc -ne 0 ]]; then
    set -- $showtech_argv
    default_parser_function $@
fi

if [ "$internal_if" != "unspecified" ]; then
    external_if=`convert_interface_fmt '-e' $internal_if`
    arg_string="$arg_string -I $internal_if"
fi


if [ "$__filename" = "unspecified" ]; then
    echo "$0: output file not specified"
    exit
fi

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

arg_string="$arg_string -t $__cardtype"

#
# 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. 
#
# [Would be far preferable for the __ksh variables to be generated from the 
# _exec variables to keep everything in step, but no support for that]
#
  
#
# get platform we are running on in order to make sure that the right version
# of certain commands gets run. Note that all spirit platforms set platform
# to panini.
#
get_platform

if [[ "$platform" = "flexr-asr9k" ]]; then
      #Setting platform as panini
      platform="panini"
fi

#
# Work out the platform dependant commands
#
if [[ "$platform" = "panini" ]]; then
    show_version_command='ng_show_version'
    show_proc_blocked_command='sh_proc_ng_blocked'
else
    show_version_command='show_version'
    show_proc_blocked_command='show_processes -b'
fi


###############################################################################
# Show commands that run once per LR                                          #
###############################################################################
sys_exec[1]='show logging'
sys__ksh[1]='show_logging'

sys_exec[2]='show version' 
sys__ksh[2]=$show_version_command

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

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

sys_exec[5]=''
sys__ksh[5]=''

###############################################################################
# Interface specific show commands that run once per LR                       #
# (ie. global commands or those only runnable on RP)                          #
###############################################################################
sys_intf_exec[1]='show interface $external_if'
sys_intf__ksh[1]='show_interface -i $internal_if'

sys_intf_exec[2]='show ipv4 interface $external_if'
sys_intf__ksh[2]='show_ip_interface -I $internal_if -v default'

sys_intf_exec[3]=''
sys_intf__ksh[3]=''

###############################################################################
# Show commands that run on every RP                                          #
###############################################################################
rp_show_exec[1]='show frequency synchronization ptp location $location'
rp_show__ksh[1]='fsync_show_ptp'

rp_show_exec[2]=''
rp_show__ksh[2]=''

###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################
rplc_show_exec[1]='show processes blocked'
rplc_show__ksh[1]=$show_proc_blocked_command

rplc_show_exec[2]=''
rplc_show__ksh[2]=''

###############################################################################
# Interface specific show commands that run on all RP or LC                   #
###############################################################################
rplc_intf_exec[1]=''
rplc_intf__ksh[1]=''

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

rplc_trace_exec[1]=''
rplc_trace__ksh[1]=''

###############################################################################
# Run this after all other commands, to leave a time stamp after the last     #
# command we run - makes analysis easier!                                     #
###############################################################################
final_exec[1]='Last show tech ptp command'
final__ksh[1]='echo Last show tech ptp command'

final_exec[2]=''
final__ksh[2]=''

#
# The display() function is the one called by the show-tech infra to actually
# do the work of running the commands (it is run by show_tech_file_fragment, 
# called below).
#
# NOTE: our "sys" commands ideally want to be run once per LR.  But seems there
# is currently no way to detect this, so for now we duplicate the SYS handling
# on all RP cards - this means for each RP type node in an LR we'll duplicate 
# the information (not ideal, but better than losing data).
#
display() {
    print_main_heading "show tech-support ptp"
        
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
        if [ "$internal_if" != "unspecified" ]; then
            exec_commands sys_intf
        fi
    else
        case "$__cardtype" in
        "DRP"|"RP"|"LC")
    		case "$__cardtype" in "DRP"|"RP")
    		    exec_commands rp_show
	    	esac

            exec_commands rplc_show

            exec_commands rplc_trace
            
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands rplc_intf
            fi
            ;;
        esac
    fi
    
    run_module show_tech_ptp "$arg_string" 

    if [[ -x `which show_tech_pd_ptp 2>/dev/null` ]]; then
        run_module show_tech_pd_ptp
    fi

    exec_commands final
    
    print_main_heading "show tech-support ptp complete"
}

#
# This function calls the display() function and sends the output to file if
# the file option has been set. 
#
. /pkg/bin/show_tech_file_fragment
