#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech__pd_ptp_main - Show tech-support fast script for PD ptp
#
# December 2008, Matthew Edwards
#
# Copyright (c) 2008-2011, 2017-2018 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=""
trace_only="0"
show_only="0"
arg_string=""
showtech_argv=""
showtech_argc="0"

while [ $# -gt 0 ]; do
    case "$1" in
        -I*) internal_if="${1#??}"; shift 1;;
        -T) trace_only="1"; shift 1;;
        -S) show_only="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 [ "$trace_only" != "0" ]; then
    arg_string="$arg_string -T"
fi

if [ "$show_only" != "0" ]; then
    arg_string="$arg_string -S"
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
#

is_asr9k=0
if [[ "$platform" = "viking" ]]; then
    is_asr9k=1
else
    os_kernel=`echo $(uname -a | cut -d' ' -f1)`
    if [[ "$os_kernel" != "QNX" ]]; then
        platform_variant=`echo $(grep "platform" /proc/cmdline | cut -d'=' -f3 | cut -d' ' -f1)`
        if [[ "$platform_variant" = "viking" || "$platform_variant" = "asr9k"  ]]; then
            is_asr9k=1
        fi
    fi 
fi

###############################################################################
# Show commands that run on every RP                                          #
###############################################################################
rp_show_exec[1]='show controller timing Controller trace Init'
rp_show__ksh[1]='show_ltrace_syncctrl -I'

rp_show_exec[2]='show controller timing Controller trace Event'
rp_show__ksh[2]='show_ltrace_syncctrl -V'

rp_show_exec[3]='show controller timing Controller trace Error'
rp_show__ksh[3]='show_ltrace_syncctrl -E'

rp_show_exec[4]=''
rp_show__ksh[4]=''

###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################
rplc_show_exec[1]='show controller sync-agent trace location $location'
rplc_show__ksh[1]='show_sync_agent_ltrace'

rplc_show_exec[2]='show logging'
rplc_show__ksh[2]='show_logging'

rplc_show_exec[3]=''
rplc_show__ksh[3]=''

###############################################################################
# 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]=''


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 pd information"
        
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
        if [ "$internal_if" != "unspecified" ]; then
            exec_commands sys_intf
        fi
    else
        case "$__cardtype" in
        "DRP"|"RP"|"LC")
            if [ "$trace_only" = "0" ]; then
		case "$__cardtype" in "DRP"|"RP")
    		    exec_commands rp_show
		esac

                exec_commands rplc_show
            fi

            if [ "$show_only" = "0" ]; then
                exec_commands rplc_trace
            fi
            
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands rplc_intf
            fi
            ;;
        esac
    fi
    
    print_main_heading "show tech-support ptp pd information complete"
}

display
