#!/pkg/bin/ksh
#--------------------------------------------------------------------
# show_tech_cpa - CPA show tech command
#
# Copyright (c) 2018-2020 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified"
__tar_file_directory_on_node="unspecified"
__file="unspecified"
ctrace_directory="/var/log/ctrace"
decode_directory="/pkg/ctrace"
additional_decode_directory="/usr/local/etc/ctrace"
log_file="/tmp/show_tech_cpa_log.txt"

log() {
     log_str=$1
     echo $log_str >> $log_file
}

# Parse the arguments to the script.
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

if [ "$__tar_file_directory_on_node" == "unspecified" ]; then
    log "ABORT: showtech directory unspecified"
    return
fi

if [ ! -d "$__tar_file_directory_on_node" ]; then
    log "ABORT: showtech directory $__showtech_directory does not exist"
    return
fi

# TODO: We really need the $node_name that we had in calvados.
#       If that is available we can remove our reliance on $__file.
#       Pursue with show tech team.
if [ "$__file" == "unspecified" ]; then
    log "ABORT: dsc node name not unspecified"
    return
fi


# ***********************************************************
#  Show commands to be run by the show tech-support commands
# ***********************************************************

#############################################################

# Commands that run on once per system
sys_exec[1]="show running-config"
sys__ksh[1]='nvgen -c -l 1 -t 1 -o 1'
sys_exec[2]="show version"
sys__ksh[2]='ng_show_version'
sys_exec[3]="show logging"
sys__ksh[3]='show_logging'
sys_exec[4]='show platform'
sys__ksh[4]='show_platform'
sys_exec[5]="show environment all"
sys__ksh[5]='spi_envmon_show -A'
sys_exec[6]="show chassis"
sys__ksh[6]='spi_show_chassis'
sys_exec[7]="show inventory all"
sys__ksh[7]='show_inventory -t'

#end
sys_exec[8]=""
sys__ksh[8]=''

# Commands that run on RP and LC nodes

rplc_show_exec[1]="show process shelfmgr"
rplc_show__ksh[1]='sysmgr_show -o -p shelfmgr'
rplc_show_exec[2]="show process invmgr"
rplc_show__ksh[2]='sysmgr_show -o -p invmgr'
rplc_show_exec[3]="show process envmon"
rplc_show__ksh[3]='sysmgr_show -o -p envmon'

rplc_show_exec[4]="pstack `pidof shelfmgr`"
rplc_show__ksh[4]='pstack `pidof shelfmgr`'
rplc_show_exec[5]="pstack `pidof invmgr`"
rplc_show__ksh[5]='pstack `pidof invmgr`'
rplc_show_exec[6]="pstack `pidof envmon`"
rplc_show__ksh[6]='pstack `pidof envmon`'
rplc_show_exec[7]="inv_dump_tool rack"
rplc_show__ksh[7]='inv_dump_tool rack'

#rplc_show_exec[8]="ctracedec -AdgHkstpNLw shelfmgr"
#rplc_show__ksh[8]='ctracedec -AdgHkstpNLw shelfmgr'
#rplc_show_exec[9]="ctracedec -AdgHkstpNLw envmon"
#rplc_show__ksh[9]='ctracedec -AdgHkstpNLw envmon'
#rplc_show_exec[10]="ctracedec -AdgHkstpNLw invmgr"
#rplc_show__ksh[10]='ctracedec -AdgHkstpNLw invmgr'

# End
rplc_show_exec[11]=""
rplc_show__ksh[11]=''

#############################################################

# We do not need these files when CPA moves completely to ctrace
collect_cpa_data() {
    print_main_heading "General CPA tech-support info"

    local tmp_files=(/tmp/cpa* /tmp/devobj* /tmp/fpga* /tmp/sensor*)

    if [ "$__cardtype" != "SYS" ]; then
       for file in ${tmp_files[@]}; do
           if [ -f $file ]; then
               tar -uf $__tar_file_directory_on_node/"$__file".cpa.tar -P $file
           fi
       done

       if [ -f $__tar_file_directory_on_node/"$__file".cpa.tar ];
       then
           gzip $__tar_file_directory_on_node/"$__file".cpa.tar
       fi
    fi
}

find_and_touch_ctrace_ctrl_for_process() {
    # Below are the processes which need the kill
    # signal to be sent to it to dump trace buffers
    proc_arr=( "$1" )

    for proc in ${proc_arr[@]};
    do
        find "$ctrace_directory/$proc" -name *.ctrl | xargs touch \
                                                            > /dev/null 2>&1
        if [[ $? != 0 ]]; then
            log "Failed to update buffer with $proc trace files"
        fi
    done
    
    . /pkg/bin/pd_ctrace_dump

}

wait_for_ctrace_dump() {
    sleep 5
}

collect_ctrace_data() {
    proc_arr=( "$1" )
    for proc in ${proc_arr[@]};
        do
            str_cat="$str_cat $ctrace_directory/$proc"
        done
    
    cd $__tar_file_directory_on_node
    if [[ $? != 0 ]]; then
        log "ABORT: Failed to cd to showtech directory"
        return
    fi

    node_name=$showtech_node_name
    tar -zcPf ctrace_$node_name.tgz -b 1024 $str_cat 
    if [[ $? != 0 ]]; then
        log "ABORT: Failed to tar ctrace directory for SPI components"
        return
    fi

    tar -zhcPf decode_$node_name.tgz -b 1024 "$decode_directory" \
                "$additional_decode_directory" \
                --transform "s,$additional_decode_target,$decode_target,"

    if [[ $? != 0 ]]; then
        log "Failed to tar decode binaries for SPI components"
    fi

}


collect_ctrace_data_rplc() {
    print_main_heading "ctrace info for cpa related components on RP/LC"
    
    procs=(shelfmgr envmon fpd_client ledmgr watchdog) 
    
    find_and_touch_ctrace_ctrl_for_process "$(echo ${procs[@]})"

    wait_for_ctrace_dump

    collect_ctrace_data "$(echo ${procs[@]})"
}

#############################################################
# Script

display() {
    print_main_heading "show tech-support cpa"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "RP")
            exec_commands rplc_show
            collect_ctrace_data_rplc
            ;;
        "DRP")
            exec_commands rplc_show
            collect_ctrace_data_rplc
            ;;
        "LC")
            exec_commands rplc_show
            collect_ctrace_data_rplc
            ;;
        esac
    fi
    
    # Add log file if it exists
    if [[ -f $log_file ]] ; then
        cp $log_file $__tar_file_directory_on_node
    fi
    
    print_main_heading "show tech-support complete"
}

collect_cpa_data

# Run the appropriate function depending on the node specified and if a
# file is specified write the output to that file. We need to redirect
# stderr to stdout when writing to a file because some of the show
# commands output to stderr instead of stdout
. /pkg/bin/show_tech_file_fragment
