#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# sys_rec_show_tech - Show tech-support script for system recovery
#
# August 2021, Alan Thomas
#
# Copyright (c) 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
# system-recovery 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
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

 # global commands on RP

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

idx=1

sys_exec[$idx]='show version'
sys__ksh[$idx]='ng_show_version'
((idx++))

sys_exec[$idx]='show install active'
sys__ksh[$idx]='sdr_instcmd show install active'
((idx++))

sys_exec[$idx]='show platform'
sys__ksh[$idx]='show_platform_sysdb'
((idx++))

sys_exec[$idx]='show logging'
sys__ksh[$idx]='show_logging'
((idx++))

sys_exec[$idx]='show disk-encryption status location all'
sys__ksh[$idx]='ssd_enc_show "-n" "all"'
((idx++))

sys_exec[$idx]=''
sys__ksh[$idx]=''


###############################################################################
# Show commands that run on every RP                                          #
###############################################################################

idx=1

rp_exec[$idx]='show running-config'
rp__ksh[$idx]='nvgen -c -l 1 -t 1 -o 1'
((idx++))

rp_exec[$idx]='show cli history detail'
rp__ksh[$idx]='show_parser_history -h 0x2'
((idx++))

rp_exec[$idx]='show context location all'
rp__ksh[$idx]='corehelper_context -c 0x1 -n all'
((idx++))

rp_exec[$idx]='show process blocked'
rp__ksh[$idx]='show_processes -b'
((idx++))

rp_exec[$idx]='show process sys_rec'
rp__ksh[$idx]='sysmgr_show -o -p sys_rec'
((idx++))

rp_exec[$idx]='ls -lrth /var/log/system_recovery_logs/'
rp__ksh[$idx]='run ls -lrth /var/log/system_recovery_logs/'
((idx++))

rp_exec[$idx]='run tail -n +1  /var/log/system_recovery_logs/*'
rp__ksh[$idx]='tail -n +1  /var/log/system_recovery_logs/*'
((idx++))

rp_exec[$idx]='run cat /misc/config/enable_system_recovery'
rp__ksh[$idx]='cat /misc/config/enable_system_recovery'
((idx++))

rp_exec[$idx]=''
rp__ksh[$idx]=''

###############################################################################
# Show commands of secy_driver that run on every LC                           #
###############################################################################
# No commands

# 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() {
    print_main_heading "show tech-support system recovery"

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

    # Print the closing heading.
    print_main_heading "show tech-support system recovery complete"
}

# The display function.
display() {
    do_show_command
}

  # 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
