#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# tam_show_techsupport - Show tech-support script for tam
#
# May 2019, Manpreet Singh
#
# Copyright (c) 2014-2019 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


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

__cardtype="unspecified"

# Set the default values of the file name for which show tech-support tam 
# is to run and the file it is to write to.
# Read in the arguments to the script, setting node_required and filename
# according to these arguments.
# Note that it is important for security reasons that users can only enter
# alphanumeric filenames and nodes and that anywhere calling this script must
# enforce this.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -l) location="$2"; shift 2;;
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

node_name_internal=`uname -n`
node_number=`node_conversion -i $node_name_internal`

# global commands on RP


###############################################################################
# Show commands that run once per LR                                          #
###############################################################################
sys_exec[1]='show version'
if [[ "$platform" = "panini" ]]; then
    sys__ksh[1]='ng_show_version'
else
    sys__ksh[1]='show_version'
fi

sys_exec[2]='show logging'
sys__ksh[2]='show_logging'


sys_exec[3]='show redundancy location all'
sys__ksh[3]='redcon_show -n all'

sys_exec[4]='show context location all'
if [[ "$platform" == "panini" ]]; then 
    sys__ksh[4]='corehelper_context -c 0x1 -n all'
else
    sys__ksh[4]='dumper_context -c 0x1 -n all'
fi

sys_exec[5]='show install active'
if [[ "$platform" != "viking" ]]; then
sys__ksh[5]='sdr_instcmd show install active'
else
sys__ksh[5]='instcmd show install active'
fi

if [[ "$platform" != "viking" ]]; then
sys_exec[6]='show install repository'
sys__ksh[6]='sdr_instcmd show install repository'
else
sys_exec[6]='show install inactive'
sys__ksh[6]='instcmd show install inactive'
fi


sys_exec[7]='show platform'
if [[ "$platform" != "viking" ]]; then
sys__ksh[7]='show_platform_sysdb'
else
sys__ksh[7]='show_platform_vkg'
fi

sys_exec[8]=''
sys__ksh[8]=''

###############################################################################
# Show commands that run on every RP                                          #
###############################################################################
rplc_exec[1]='show process tamsvcs_tamm location $location'
rplc__ksh[1]='sysmgr_show -o -p tamsvcs_tamm'

rplc_exec[2]='show process tam_sync location $location'
rplc__ksh[2]='sysmgr_show -o -p tam_sync'

rplc_exec[3]='show process tam_entropy location $location'
rplc__ksh[3]='sysmgr_show -o -p tam_entropy'

rplc_exec[4]='show platform security tam all location $location'
rplc__ksh[4]='tamsvcs_show -t all -l $fq_nodeid'

rplc_exec[5]=''
rplc__ksh[5]=''


###############################################################################
# Parse the arguments to the script.
# Usage:
#
#sys.exit()
# A function called display() must be provided that calls the functions to
# Print the output heading

# TAM PATH incase of cXR and eXR
if [ "$OS_STRING" == "QNX" ];then
    TAM_DEBUG_DIR="/harddisk:"
    SWTAM_ROOT_DIR="/disk0:"
    TAR_CMD="tar -cf"
elif [ "$platform" = "enxr" ];then
    TAM_DEBUG_DIR="/disk0:"
    SWTAM_ROOT_DIR="/disk0:"
    TAR_CMD="tar --ignore-failed-read --warning=none -C / -cf"
else
    TAM_DEBUG_DIR="/var/log"
    SWTAM_ROOT_DIR="/misc/config"
    TAR_CMD="tar --ignore-failed-read --warning=none -C / -cf"
fi

save_tam_files() {

    print_heading "Compressing and archiving tam files"

    tam_outfile="${__tar_file_directory_on_node}/tam_logs_${node_name_internal}.tar"
    $TAR_CMD  $tam_outfile                         \
        $TAM_DEBUG_DIR/tam_debug*                  \
        $TAM_DEBUG_DIR/tamfs.log                   \
        $TAM_DEBUG_DIR/tam_database_backup.tar.gz  \
        $SWTAM_ROOT_DIR/SWTAmRoot/* 1>/dev/null 2>&1


    #Additionally, on Classic XR collect /disk0:/tam_debug.log
    if [ "$OS_STRING" == "QNX" ];then
         tar -rf $tam_outfile       \
             /disk0:/tam_debug.log
    fi    

    # collect ps /var/log/messages and /var/log/dmesg
    if [ "$OS_STRING" != "QNX" ];then
        # take ps tam
        ps_outfile="${__tar_file_directory_on_node}/ps_tam_$(date +%Y-%m-%d.%H:%M:%S)-${node_name_internal}.txt"
        ps -aef | grep -e UID -e tam > $ps_outfile

        messages_outfile="${__tar_file_directory_on_node}/messages_${node_name_internal}.tar" 
        tar --ignore-failed-read --warning=none -C / -cf  $messages_outfile   \
            /var/log/messages* 

        dmesg_outfile="${__tar_file_directory_on_node}/dmesg_${node_name_internal}.gz"
        dmesg | gzip > $dmesg_outfile
    fi
}


do_show_command() {

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
            "RP"|"DRP")
                save_tam_files
                exec_commands rplc
                ;;
            "LC")
                save_tam_files
                exec_commands rplc
                ;;
        esac
    fi

}

# The display function.
display() {

    print_main_heading "show tech-support tam"
    do_show_command

    # Print the closing heading.
    print_main_heading "show tech-support tam 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

