#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# mpa_mgr_show_techsupport                     
# author : bhkalla@cisco.com                                  
# Copyright (c) 2018-2019 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"
__mpa_detail="unspecified"

# Set the default values of the file name for which show tech-support mpa-mgr
# 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;;
        -d) __mpa_detail=1; shift 1;;
         *)  default_parser_function "$@"; shift $#;;
    esac
done

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

###############################################################################
# Show commands that run once per RP and LC
###############################################################################

sys_exec[1]='show version'
sys__ksh[1]='ng_show_version'

sys_exec[2]='show platform'
sys__ksh[2]='show_platform_sysdb'

sys_exec[3]='show inventory'
sys__ksh[3]='show_inventory'

sys_exec[4]=''
sys__ksh[4]=''


rp_exec[1]='show hw-module fpd'
rp__ksh[1]='/pkg/bin/show_fpd_loc_ng fpd'

rp_exec[2]='show diag'
rp__ksh[2]='/pkg/bin/show_diag'

rp_exec[3]=''
rp__ksh[3]=''

#mpa_mgr_show_fpga_flash is a test utitity to show mpa fpga flash content.

#mpa_mgr_show_fpga_flash with -t option displays the fpga flash image information
#such as fpga type, version, build date, image size, and the active status for 
#for both primary and backup mpa fpga of a target device type.

#mpa_mgr_show_fpga_flash with -m option displays the fpga flash content version information 
#such as manufacturer id, device type, device size, block protection status, status register
#and write to status register is enabled or not for a target device.

#mpa_mgr_show_fpga_flash with -d option displays the detailed fpga flash image information
#such as fpga name, type, version, build time, image size, CRC of the image of a target
#device type for both primary and backup mpa fpga.

lc_exec[1]='show mpa_mgr fpga flash image information for both primary and backup mpafpga1'
lc__ksh[1]='/pkg/bin/mpa_mgr_show_fpga_flash -t mpafpga_1'

lc_exec[2]='show mpa_mgr fpga flash image information for both primary and backup mpafpga2'
lc__ksh[2]='/pkg/bin/mpa_mgr_show_fpga_flash -t mpafpga_2'

lc_exec[3]='show detailed mpa_mgr flash image information for both primary and backup mpafpga1'
lc__ksh[3]='/pkg/bin/mpa_mgr_show_fpga_flash -t mpafpga_1 -d'

lc_exec[4]='show detailed mpa_mgr flash image information for both primary and backup mpafpga2'
lc__ksh[4]='/pkg/bin/mpa_mgr_show_fpga_flash -t mpafpga_2 -d'

lc_exec[5]='show mpa_mgr fpga flash information for mpafpga1'
lc__ksh[5]='/pkg/bin/mpa_mgr_show_fpga_flash -t mpafpga_1 -m'

lc_exec[6]='show mpa_mgr fpga flash information for mpafpga2'
lc__ksh[6]='/pkg/bin/mpa_mgr_show_fpga_flash -t mpafpga_2 -m'

lc_exec[7]=''
lc__ksh[7]=''


#Add the adder as the passed string argument and append it to a target file
mpa_add_header() {
    echo $1
    touch /root/logs
    echo $2
    echo "" >> $2
    date_str=`date +"%k:%M:%S.%3N UTC %a %b %d %Y"`
    echo "++++++++++++++++ $1  [$date_str] ++++++++++++++++" >> $2
    echo "++++++++++++++++ $1  [$date_str] ++++++++++++++++" >> /root/logs 
    echo "" >> $2
}

#Add the footer as the passed string argument and append it to the end of a target file
mpa_add_footer() {
    date_str=`date +"%k:%M:%S.%3N UTC %a %b %d %Y"`
    echo "" >> $2
    echo "--------------- $1  [$date_str] ----------------" >> $2
    echo "" >> $2
}

#save the ctrace for the mpa_mgr for a given LC node process to the outfile argument passed
mpa_save_ctrace () {
    outfile=$1/ctrace
    node_str=$2
   
    mpa_add_header "Collecting mpa_mgr ctrace for location $node_str" $outfile
    /pkg/bin/ctracedec -gktTdslAwp mpa_mgr >> $outfile
    mpa_add_footer "Collecting mpa_mgr ctrace for location $node_str" $outfile
}

#get the platform details
save_platform() {
    /pkg/bin/show_platform_sysdb
}

#saves the current iso image version on the testbed 
save_version() {
    /pkg/bin/ng_show_version
}

#saves the logging information
save_show_logging() {
    node_subdir=$__tar_file_directory_on_node
    show_logging >> $node_subdir/show_logging
}

#saves the boot.py script output
save_boot_py_logs() {
    node_subdir=$__tar_file_directory_on_node
    python /pkg/bin/boot.py >> $node_subdir/boot_py_logs
}

# A function called display() must be provided that calls the functions to
# Print the output heading

do_show_command() {
    # Print the starting statement.
    print_main_heading "show tech-support mpa_mgr for $ext_node_name"
    print_main_heading `node_type`

    if [ "$__cardtype" == "SYS" ]; then

        save_platform
        save_version
        exec_commands sys
        exec_commands rp
        save_show_logging
        save_boot_py_logs

    else

        case "$__cardtype" in
            "RP")
                #Nothing to be done in RP as we are doing general checks above
                ;; 
            "LC")                    
                node_name=`uname -n`
                card_type=`cat -v /dev/xr_bootstrap | grep PD_CARD_TYPE | awk -F "=" '{ print $2}'`
                
                node_str=`uname -n | sed -e 's/xr-vm_node//' | sed -e 's/_/\//g'`
                node_subdir=$__tar_file_directory_on_node/$node_name

                if [ ! -d "$node_subdir" ]; then
                    /bin/mkdir $node_subdir
                fi  
  
                exec_commands lc
                mpa_save_ctrace $node_subdir $node_str

                ;;
        esac
    fi

    # Print the closing statement.
    print_main_heading "show tech-support mpa_mgr complete"
}

# The display function.
display() {
        do_show_command
}
  
# the file option has been set.
. /pkg/bin/show_tech_file_fragment
