#!/pkg/bin/ksh
# --------------------------------------------------------------------------
# daps_show_tech - Show tech-support script for DAPS
#
# March 2008, Suresh R Thesayi
#
# Copyright (c) 2018 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

# Initialise any variables used. 
                                 
__cardtype="unspecified"                      
pool_name="unspecified"
vrf_name="unspecified"

# Parse the arguments to the script.
# Usage: 
# 
# -P  : Run command set 2 for pool
# -V  : Run command set 3 for vrf
# Always run         : Run command set 1
while [ $# -gt 0 ]; do
  case "$1" in
     -t) __cardtype="$2"; shift 2;;
     -l) location="$2"; shift 2;;
     -P) pool_name="$2"; shift 2;;
     -V) vrf_name="$2"; shift 2;;
      *) default_parser_function "$@"; shift $#;;
  esac
done

pool_name_option=""
if [ "$pool_name" != "unspecified" ]; then
  pool_name_option="-n $pool_name"
fi

file_name_option=""
if [ "$__filename" != "unspecified" ]; then
  file_name_option="-F $__filename"
else
    echo "show_tech_parser_testing: output file not specified"
    exit
fi

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

vrf_name_option=""
if [ "$vrf_name" != "unspecified" ]; then
  vrf_name_option="-n $vrf_name"
fi

# 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. 

command1_exec[1]='show pool trace errors location $location'
command1__ksh[1]='daps_show_ltrace -E' #i $fq_nodeid -E'
command1_exec[2]='show pool trace events location $location'
command1__ksh[2]='daps_show_ltrace -V' #i $fq_nodeid -V'
command1_exec[3]='show pool trace internals location $location'
command1__ksh[3]='daps_show_ltrace -I' #i $fq_nodeid -I'
command1_exec[4]='show pool trace allocations location $location'
command1__ksh[4]='daps_show_ltrace -L' #i $fq_nodeid -L'
command1_exec[5]='show pool trace client errors location $location'
command1__ksh[5]='daps_client_show_ltrace -E' #i $fq_nodeid -E'
command1_exec[6]='show pool trace client events location $location'
command1__ksh[6]='daps_client_show_ltrace -V' #i $fq_nodeid -V'
command1_exec[7]='show pool trace client internals location $location'
command1__ksh[7]='daps_client_show_ltrace -I' #i $fq_nodeid -I'
command1_exec[8]='show pool trace client allocations location $location'
command1__ksh[8]='daps_client_show_ltrace -L' #i $fq_nodeid -L'
command1_exec[9]='show system statistics component daps-clients'
command1__ksh[9]='ship_show -c daps-clients'
command1_exec[10]='show system statistics component daps-global'
command1__ksh[10]='ship_show -c daps-global'
command1_exec[11]='show process daps location $location'
command1__ksh[11]='sysmgr_show -o -p daps'
command1_exec[12]=''
command1__ksh[12]=''

# 
# command2 is not functional. This is unused for now. Proper CLI
# will be created in a future DDTS.
#
command2[0]='run daps_debug -p c $pool_name_option $file_name_option'
command2[1]='run daps_debug -u $file_name_option'
command2[2]='run daps_debug -g $file_name_option'
command2[3]='run daps_debug -c ci $file_name_option'
command2[4]=''

# -P pool
command3_exec[1]='show pool ipv4 name $pool_name location $location'
command3__ksh[1]='daps_show -p $pool_name -f' #L $fq_nodeid -f'
command3_exec[2]='show pool ipv6 name $pool_name location $location'
command3__ksh[2]='daps_show -p $pool_name -s' #L $fq_nodeid -s'
command3_exec[3]='show pool ipv4 name $pool_name remote-info'
command3__ksh[3]='daps_show -p $pool_name -S -f' #L $fq_nodeid -f'
command3_exec[4]='show pool ipv6 name $pool_name remote-info'
command3__ksh[4]='daps_show -p $pool_name -S -s' #L $fq_nodeid -s'
command3_exec[5]='show pool ipv6 name $pool_name verbose'
command3__ksh[5]='daps_show -p $pool_name -s -a' #L $fq_nodeid -s'
command3_exec[6]='show pool ipv6 name $pool_name remote-info verbose'
command3__ksh[6]='daps_show -p $pool_name -S -s -a' #L $fq_nodeid -s'
command3_exec[7]='show pool ipv4 name $pool_name verbose'
command3__ksh[7]='daps_show -p $pool_name -f -a' #L $fq_nodeid -s'
command3_exec[8]='show pool ipv4 name $pool_name remote-info verbose'
command3__ksh[8]='daps_show -p $pool_name -S -f -a' #L $fq_nodeid -s'
command3_exec[9]=''
command3__ksh[9]=''

# -V vrf_name
command4_exec[1]='show pool vrf $vrf_name ipv4'
command4__ksh[1]='daps_show -v $vrf_name -f'
command4_exec[2]='show pool vrf $vrf_name ipv6'
command4__ksh[2]='daps_show -v $vrf_name -s'
command4_exec[3]=''
command4__ksh[3]=''

command5_exec[1]='show pool ipv4'
command5__ksh[1]='daps_show -v all -f'
command5_exec[2]='show pool ipv6'
command5__ksh[2]='daps_show -v all -s'
command5_exec[3]='show pool statistic'
command5__ksh[3]='daps_show -c'
command5_exec[4]=''
command5__ksh[4]=''

# A function called display() must be provided that calls the functions to 
# run the required show commands. The display() function will be called in 
# the /pkg/bin/show_tech_comp_file_frag

display() {

     # Print the output heading 
     print_main_heading "show tech-support "

     # Execute commands
     if [ "$__cardtype" == "SYS" ]; then
         if [ "$vrf_name" != "unspecified" ]; then
             exec_commands command4
         fi
         exec_commands command5
     else
         case "$__cardtype" in
         "RP")
             exec_commands command1
             if [ "$pool_name" != "unspecified" ]; then
                 exec_commands command3
             fi
             ;;
         "DRP")
              exec_commands command1
              if [ "$pool_name" != "unspecified" ]; then
                  exec_commands command3
              fi
             ;;
         "LC")
             exec_commands command1
             if [ "$pool_name" != "unspecified" ]; then
                 exec_commands command3
             fi
             ;;
         esac
     fi

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

# 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
