#!/pkg/bin/ksh
# show tech-support lwm command
#
# Jule 2012, Bharath Vasudevan
#
# Copyright (c) 2010-2014 by cisco Systems, Inc.
# All rights reserved.
#----------------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified";
procname="unspecified"
ins_id=1

while [ "$#" -gt "0" ]; do
    case "$1" in
       -P) procname="$2"; shift 2;;
       -I) ins_id="$2"; shift 2;;
       -t) __cardtype="$2"; shift 2;;
       -l) location="$2"; shift 2;;
        *) default_parser_function $@; shift $#;;
    esac
done

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

# System specifc commands
sys_exec[1]='show version'
sys__ksh[1]='ng_show_version'
sys_exec[2]='show redundancy summary'
sys__ksh[2]='redcon_show -s'
sys__ksh[3]=''

# Node specific commands
current_location_cmds_exec[1]='show log local'
current_location_cmds__ksh[1]='show_local_log'
current_location_cmds_exec[2]='run lwm_debug_proc -p $procname -l 9'
current_location_cmds__ksh[2]='lwm_debug_proc -p $procname -l 9'
current_location_cmds_exec[3]='run lwm_debug_proc -p all -b'
current_location_cmds__ksh[3]='lwm_debug_proc -p all -b'

# Process specific commands
current_location_cmds_exec[4]="run lwm_debug_proc -p $procname -Beo"
current_location_cmds__ksh[4]="lwm_debug_proc -p $procname -Beo"
current_location_cmds_exec[5]="run lwm_debug_proc -p $procname -l 4"
current_location_cmds__ksh[5]="lwm_debug_proc -p $procname -l 4"
current_location_cmds_exec[6]="run lwm_debug_proc $procname all -l 5"
current_location_cmds__ksh[6]="lwm_debug_proc -p $procname -l 5"
current_location_cmds_exec[7]="run lwm_debug_proc -p $procname -l 6"
current_location_cmds__ksh[7]="lwm_debug_proc -p $procname -l 6"
current_location_cmds_exec[8]="run lwm_debug_proc -p $procname -l 7"
current_location_cmds__ksh[8]="lwm_debug_proc -p $procname -l 7"
current_location_cmds_exec[9]="run lwm_debug_proc -p $procname -l 8"
current_location_cmds__ksh[9]="lwm_debug_proc -p $procname -l 8"
current_location_cmds__exec[10]="run ctracedec -gtk ds"
current_location_cmds__ksh[10]="ctracedec -gtk ds"
current_location_cmds__ksh[11]=""

collect_proc_ltraces() {
    output=`sysmgr_show -p $procname | grep Instance | cut -d ":" -f 2`
    if [[ $output == "" ]]; then
        output="1"
    fi
    
    for inst in  $output
    do
        show_ltrace_lwmlib -P $procname -I $inst -T all 
    done
}

collect_all_error_ltraces() {
    for procname in `ls /dev/shmem/ltrace/lwmlib |  cut -d "_" -f 2,3,4 | sort | uniq`
    do
        for inst in `sysmgr_show -p $procname | grep Instance | cut -d ":" -f 2`
        do
             err_output=`show_ltrace_lwmlib -P $procname -I$inst -T errors`
             
             if [[ $err_output != "" ]]; then
                   show_ltrace_lwmlib -P $procname -I1 -T all 
             fi
        done
    done
}

display() {

    print_main_heading "show tech-support lwm for $procname"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        if [ "$procname" == "all" ]; then
            collect_all_error_ltraces
        else 
            exec_commands current_location_cmds
            collect_proc_ltraces
        fi
    fi

    print_main_heading "show tech-support lwm complete"

}

. /pkg/bin/show_tech_file_fragment
