#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# attestation_agent_show_techsupport - Show tech-support script for 
# attestation_agent
#
# Feb 2021, Manik Bhasin
#
# Copyright (c) 2020-2021 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

# This script fragment contains the code for the following functions
# - print_main_heading
# - print_command_heading
# - run_single_command
# - run_commands
# - run_single_command_on_all_nodes
# - run_commands_on_all_nodes
# - default_parser_function
. /pkg/bin/show_tech_main_fragment


# List each set of show commands to be run. Each set must finish with an empty
# string. Note that it is important to use single quotes rather than double
# quotes for the strings containing your commands.

__cardtype="unspecified"

# Set the default values of the file name for which show tech-support attestation-agent 
# is to run and the file it is to write to.
# Read in the arguments to the script, setting node_required and filename
# according to these arguments.
# Note that it is important for security reasons that users can only enter
# alphanumeric filenames and nodes and that anywhere calling this script must
# enforce this.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -l) location="$2"; shift 2;;
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

node_name_internal=`uname -n`
node_number=`node_conversion -i $node_name_internal`

# global commands on RP


###############################################################################
# Show commands that run once per LR                                          #
###############################################################################
sys_exec[1]='show version'
if [[ "$platform" = "panini" ]]; then
    sys__ksh[1]='ng_show_version'
else
    sys__ksh[1]='show_version'
fi

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


sys_exec[3]='show redundancy location all'
sys__ksh[3]='redcon_show -n all'

sys_exec[4]='show context location all'
if [[ "$platform" == "panini" ]]; then 
    sys__ksh[4]='corehelper_context -c 0x1 -n all'
else
    sys__ksh[4]='dumper_context -c 0x1 -n all'
fi

sys_exec[5]='show install active'
if [[ "$platform" != "viking" ]]; then
sys__ksh[5]='sdr_instcmd show install active'
else
sys__ksh[5]='instcmd show install active'
fi

if [[ "$platform" != "viking" ]]; then
sys_exec[6]='show install repository'
sys__ksh[6]='sdr_instcmd show install repository'
else
sys_exec[6]='show install inactive'
sys__ksh[6]='instcmd show install inactive'
fi


sys_exec[7]='show platform'
if [[ "$platform" != "viking" ]]; then
sys__ksh[7]='show_platform_sysdb'
else
sys__ksh[7]='show_platform_vkg'
fi

sys_exec[8]='show install log reverse detail'
if [[ "$platform" != "viking" ]]; then
sys__ksh[8]='sdr_instcmd show install log 0x0 detail reverse'
else
sys__ksh[8]='instcmd show install log 0x0 detail reverse'
fi


sys_exec[9]=''
sys__ksh[9]=''

###############################################################################
# Show commands that run on every RP                                          #
###############################################################################
rp_exec[1]='show logging'
rp__ksh[1]='show_logging'

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

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

rp_exec[4]='show cli history detail'
rp__ksh[4]='show_parser_history -h 0x2'

rp_exec[5]='show running-config'
rp__ksh[5]='nvgen -c -l 1 -t 1 -o 1'

rp_exec[6]='show process blocked'
if [ "$platform" == "panini" ]; then
rp__ksh[6]='sh_proc_ng_blocked'
else
rp__ksh[6]='show_processes -b'
fi

rp_exec[7]='show auth-service trace client cepki'
rp__ksh[7]='show_ltrace_authservice -C cepki -W'

###############################################################################
# Collect traces for attestation-agent from RP/LC                             #
###############################################################################
rplc_exec[1]='show attestation-agent trace'
rplc__ksh[1]='attestation_agent_show_ltrace'

###############################################################################
# Collect show-tech for rdsfs and TAM                                         #
###############################################################################

attestation_dependency_tech_support() {
   if [ -f /pkg/bin/tam_show_techsupport ]; then
      enable_techs ""  /pkg/bin/tam_show_techsupport
   fi
   if [ -f /pkg/bin/rdsfs_show_tech_support ]; then
      enable_techs ""  /pkg/bin/rdsfs_show_tech_support
   fi
}

##############################################################################
# Parse the arguments to the script.
# Usage:
#
#sys.exit()
# A function called display() must be provided that calls the functions to
# Print the output heading

do_show_command() {

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

}

# The display function.
display() {

    print_main_heading "show tech-support attestation-agent"
    do_show_command

    # Print the closing heading.
    print_main_heading "show tech-support attestation-agent 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

