#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech_fast_pppoe   - Show tech-support fast script for pppoe  - 
#
# December 2010, Matthew Edwards
#
# Copyright (c) 2010-2011, 2017 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.
#
__cardtype="unspecified"
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;;        
        -t) __cardtype="$2"; shift 2;;
        *) 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"

PLATFORM_TYPE=`uname -m`


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

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

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

###############################################################################
# Show commands that run on every RP                                          #
###############################################################################
rp_exec[1]='show running-config'
rp__ksh[1]='nvgen -c -l 1 -t 1 -o 1'

rp_exec[2]='show context location all'
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    rp__ksh[2]='corehelper_context -c 0x1 -n all'
else 
    rp__ksh[2]='dumper_context -c 0x1 -n all'
fi

rp_exec[3]='show redundancy location $location'
rp__ksh[3]='redcon_show -n all'

rp_exec[4]='show logging'
rp__ksh[4]='show_logging'

rp_exec[5]='show interface summary'
rp__ksh[5]='show_interface -o 0x3'

rp_exec[6]='show subscriber trace all location all'
rp__ksh[6]='show_iedge_ltrace -i all -A'

rp_exec[7]=''
rp__ksh[7]=''

###############################################################################
# Interface specific show commands that run on all RPs                        #
###############################################################################

rp_intf_exec[1]='show interfaces $external_if'
rp_intf__ksh[1]='show_interface -i $internal_if'

rp_intf_exec[2]='show adjacency $external_if location all'
rp_intf__ksh[2]='aib_show_command -I $internal_if -A'

rp_intf_exec[3]='show cef $external_if'
rp_intf__ksh[3]='fib_show_command -t -O 0x0 -i $internal_if'

rp_intf_exec[4]='show subscriber session filter-id interface $external_if'
rp_intf__ksh[4]='show_iedge_subscriber -c session -i $internal_if'

rp_intf_exec[5]=''
rp_intf__ksh[5]=''


###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################
rplc_exec[1]='show process blocked location $location'
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    rplc__ksh[1]='sh_proc_ng_blocked all'
else
    rplc__ksh[1]='show_processes -b'
fi

rplc_exec[2]=''
rplc__ksh[2]=''

###############################################################################
# Interface specific show commands that run on all RP or LC                   #
###############################################################################
rplc_intf_exec[1]=''
rplc_intf__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 pppoe command'
final__ksh[1]='echo Last show tech pppoe 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 pppoe"
        
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
        if [ "$internal_if" != "unspecified" ]; then
            exec_commands sys_intf
        fi
    else
        case "$__cardtype" in
        "DRP"|"RP")
            exec_commands rp
            exec_commands rplc
            
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands rp_intf
                exec_commands rplc_intf
            fi
            ;;
        "LC")
            exec_commands rplc
            
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands rplc_intf
            fi
            ;;
        esac
    fi

    run_module show_tech_fast_ppp "$arg_string"
    run_module show_tech_fast_pppoe_common "$arg_string" 
    exec_commands final
    
    print_main_heading "show tech-support pppoe 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
