#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_gsp_client - Runs cmds for show tech-support gsp client
#
# April 2009, Caina Wei
#
# Copyright (c) 2009-2012, 2017 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------
. /pkg/bin/show_tech_main_fragment

procname="unknown"
jid=-1
pid=-1

while [ $# -gt 0 ]; do
  case "$1" in
     -L) location="-L $2"; shift 2;;
     -t) __cardtype="$2";  shift 2;;
     -n) procname="$2";  shift 2;;
     -j) jid="$2";  shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

if [[ $jid -eq -1 ]]; then
    test_jid=`sysmgr_show -p $procname | grep Job |  cut -f 2 -d ":" | cut -f 2 -d " "`
    test_pid=`sysmgr_show -p $procname | grep PID |  cut -f 2 -d ":" | cut -f 2 -d " "`
    if [[ $test_jid != "" ]]; then
       jid=$test_jid
    fi
    if [[ $test_pid != "" ]]; then
       pid=$test_pid
    fi
else 
    test_pid=`sysmgr_show -p $jid | grep PID |  cut -f 2 -d ":" | cut -f 2 -d " "`
    if [[ $test_pid != "" ]]; then
       pid=$test_pid
    fi
fi

nodename=$showtech_node_name


#
# system Commands
#      
system_exec[1]='Get group rsi map in Admin plane'
system__ksh[1]='gsp_show -m -a'
system_exec[2]='Get group rsi map in LR plane'
system__ksh[2]='gsp_show -m'
system_exec[3]='Get group rsi map in LR Ctrl plane'
system__ksh[3]='gsp_show -m -l'
  
# client stats
stats_exec[1]="show gsp stats client job $jid"
stats__ksh[1]="gsp_show_api_stats -r $jid"
stats_exec[2]="show gsp stats server jid $jid"
stats__ksh[2]="gsp_show -c $jid"
stats_exec[3]="show gsp stats client memory $pid"
stats__ksh[3]="gsp_show -M -G -Q -D -p $pid"
stats_exec[4]="show gsp trace library gid 0 slow"
stats__ksh[4]="show_ltrace_gsp -G 0 -S"
stats_exec[5]="show gsp trace library gid 0 fast"
stats__ksh[5]="show_ltrace_gsp -G 0 -F"

# Get the list of groups and then get library traces and stats
find_groups() {
    temp=`gsp_show -c $jid | grep -E "Max DataThresh" | cut -f1 -d " "`
    if [[ $# -eq 0 ]]; then
        set $temp
        while [ "$#" -gt "0" ]; do
            show_tech_gsp_group -g $1
            shift
        done
    fi
}

display() {
    if [ $jid -eq -1 ] || [ $pid -eq -1 ]; then
        echo "Process $procname jid $jid doesn't exist on $nodename"
    elif [ "$__cardtype" = "SYS" ]; then
        # Collect loggging 
        exec_commands system 
    else
        print_main_heading "show tech-support gsp client jobid $jid"

        exec_commands stats
        find_groups
        print_main_heading "show tech-support gsp client jobid $jid complete"
    fi
}

. /pkg/bin/show_tech_file_fragment

