#!/bin/bash
# -----------------------------------------------------------------------------
# tech_pcie
#
# Copyright (c) 2017 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------

#
# Load the script provided by show-tech infra, which provides worker functions
#
source /opt/cisco/calvados/script/show_tech_main_fragment

if [[ -f /etc/init.d/calvados_bootstrap.cfg ]]; then
    source /etc/init.d/calvados_bootstrap.cfg
fi


#
# Parse the arguments to the script - card type and interface filter are the
# only support options currently.  No need to use the default parser function,
# as this has been done by caller.
#
cardtype=`/opt/cisco/calvados/script/node_type`
trace_only="0"
show_only="0"
host_ip=my_host

while [ "$#" -gt "0" ]; do
    case "$1" in
        -T) trace_only="1"; shift 1;;
        -S) show_only="1"; shift 1;;
        -t) cardtype="$2"; shift 2;;
        -f) media_path="$2"; shift 2;;
        *) last_arg=$1; shift;;
    esac
done
if [ "$cardtype" == "unspecified" ]; then
    cardtype=`/opt/cisco/calvados/script/node_type`
fi

nodename=`echo $last_arg | cut -f 1 -d.`

#
# List of commands to be run. 
#
# Trace commands
#
sys_show_exec[1]='cat /etc/build-info.txt'
sys_show__ksh[1]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; run cat /etc/build-info.txt"'
sys_show_exec[2]='show running-config'
sys_show__ksh[2]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show running-config"'

#
# Show commands
#
###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################

#
# Trace commands
#


#
# Show commands
#
rplc_show_exec[1]='show process blocked'
rplc_show__ksh[1]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show process blocked"'
#
# The display() function is the one that does all the work - called by us as
# this is a worker script.
#
display() {

    print_main_heading "PCIE tech-support info"

    if [ "$cardtype" = "SYS" ]; then
        exec_commands sys_show
    else
         # Gather hostOS logs
        host_file=`ssh -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o StrictHostKeychecking=no $host_ip "sh /etc/init.d/showtech_pcie_vm_script.sh -f $media_path -n $nodename -l "host""`
        
        if [ -n "$host_file" ]; then
            scp -q -r -o StrictHostKeychecking=no -o LogLevel=quiet $host_ip:$host_file $host_file
        fi
        # Do a cleanup
        ssh -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o StrictHostKeychecking=no $host_ip "sh /etc/init.d/showtech_pcie_vm_script.sh -f $media_path -n $nodename -l "host" -c"
       
        # collect XR logs too

        cnt=`ls -1d /etc/vmm_* 2>/dev/null 1| wc -l`
        if [ $cnt -ne 0 ]; then
            for name in /etc/vmm_*
            do
              GUEST_IP_ADDR=$(grep -a "GUEST_IP_ADDR" $name | cut -d'=' -f2)
              xr_file=""

              ssh -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o StrictHostKeychecking=no  -o BatchMode=yes -q $GUEST_IP_ADDR exit &>/dev/null
              if [ $? -eq 0 ]; then
                 # XR is connected
                 xr_file=`ssh -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o StrictHostKeychecking=no  $GUEST_IP_ADDR "/etc/init.d/showtech_pcie_vm_script.sh -t $cardtype -f $media_path -n $nodename -l "xr""`
             
                 if [ -n "$xr_file" ]; then
                    scp -q -r -o StrictHostKeychecking=no -o LogLevel=quiet $GUEST_IP_ADDR:$xr_file $xr_file
                 fi
                 # Do a cleanup
                 ssh -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o StrictHostKeychecking=no  $GUEST_IP_ADDR "/etc/init.d/showtech_pcie_vm_script.sh -t $cardtype -f $media_path -n $nodename -l "xr" -c"
              fi    
            done 
        fi
        #Collect Admin Logs
        admin_file=`/etc/init.d/showtech_pcie_vm_script.sh -t $cardtype -f $media_path -n $nodename -l "sysadmin"`
        echo $admin_file

        exec_commands rplc_show

fi
}

display
