#!/bin/bash
# -----------------------------------------------------------------------------
# show_tech_install
#                                       
# Copyright (c) 2008-2009, 2012-2019 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

shopt -s nullglob

#
# 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="unspecified"
trace_only="0"
show_only="0"
arg_string=""
showtech_argv=""
showtech_argc="0"

while [ "$#" -gt "0" ]; do
    case "$1" in
        -T) trace_only="1"; shift 1;;
        -S) show_only="1"; shift 1;;
        -t) __cardtype="$2"; shift 2;;
        *) showtech_argv="$showtech_argv $1"; (( showtech_argc+=1 )); shift 1;;
    esac
done

#
# Call the showtech infra default parser - using set to recreate the $#, $@, 
# etc. - "--" is necessary so set doesn't try to interpret any of the 
# arguments we are passing through!
#
if [[ $showtech_argc -ne 0 ]]; then
    set -- $showtech_argv
    default_parser_function $@
fi

if [ "$trace_only" != "0" ]; then
    arg_string="$arg_string -T"
fi

if [ "$show_only" != "0" ]; then
    arg_string="$arg_string -S"
fi

if [ "$__filename" = "unspecified" ]; then
    echo "$0: output file not specified"
    exit
fi

if [ "$__cardtype" = "unspecified" ]; then
    __cardtype=`/opt/cisco/calvados/script/node_type`
fi

cp="/bin/cp -f"
mkdir="/bin/mkdir"
touch="/bin/touch"
node_name=`uname -n`
node_subdir=$__tar_file_directory_on_node/$node_name
if [ ! -d "$node_subdir" ]; then
   $mkdir $node_subdir
fi
error_file="/var/log/install/sh_tech_errors.txt"
if [ ! -f "$error_file" ]; then
   $touch $error_file
fi
a=(/var/log/install/inst_agent.log*)
if [ -n "$a" ]; then
   $cp  /var/log/install/inst_agent.log* $node_subdir  2>>$error_file
fi
b=(/var/log/install/inst_mgr.log*)
if [ -n "$b" ]; then
   $cp /var/log/install/inst_mgr.log* $node_subdir 2>>$error_file
fi
c=(/var/log/install/inst_hooks.log*)
if [ -n "$c" ]; then
   $cp /var/log/install/inst_hooks.log* $node_subdir 2>>$error_file
fi
d=(/var/log/inst_mismatch_log)
if [ -f "$d" ]; then
   $cp /var/log/inst_mismatch_log $node_subdir 2>>$error_file
fi
e=(/var/log/install/disk*log*)
if [ -n "$e" ]; then
   $cp /var/log/install/disk*log* $node_subdir 2>>$error_file
fi
f=(/var/log/disk-functions.log)
if [ -n "$f" ]; then
   $cp $f $node_subdir 2>>$error_file
fi
g=(/var/log/bootlogs)
if [ -d "$g" ]; then
   $mkdir -p $node_subdir/bootlogs 2>>$error_file
   $cp /var/log/bootlogs/* $node_subdir/bootlogs 2>>$error_file
fi
f=(/var/log/install/sig*log)
if [ -n "$f" ]; then
   $cp /var/log/install/sig*log $node_subdir 2>>$error_file
fi
h=(/var/log/install/inst_health_tool*)
if [ -n "$h" ]; then
   $mkdir -p $node_subdir/healthtoollogs 2>>$error_file
   $cp /var/log/install/inst_health_tool* $node_subdir/healthtoollogs 2>>$error_file
fi
i=(/var/log/install/install_functions_py.log*)
if [ -n "$i" ]; then
    $cp /var/log/install/install_functions_py.log* $node_subdir 2>>$error_file
fi
j=(/var/log/install/prep_ids)
if [ -d "$j" ]; then
   $mkdir -p $node_subdir/prep_ids 2>>$error_file
   $cp -rf /var/log/install/prep_ids/* $node_subdir/prep_ids 2>>$error_file
fi
k=(/var/log/install/install_system.log)
if [ -n "$k" ]; then
    $cp $k $node_subdir 2>>$error_file
fi
l=(/var/log/install/install_node.log)
if [ -n "$l" ]; then
    $cp $l $node_subdir 2>>$error_file
fi
m=(/var/log/install/install.log)
if [ -n "$m" ]; then
    $cp $m $node_subdir 2>>$error_file
fi
o=(/var/log/install/add_replicate.log)
if [ -n "$o" ]; then
    $cp $o $node_subdir 2>>$error_file
fi

if [ "$__node_type" == "RP" ]; then
    # On self-test failure membooted cards copy over log files to the lead RP
    # Copy latest 2 log files of each memboot card type (if available)
    memboot_log=(/misc/disk1/install-admin-*FC*.tgz)
    if [ -n "$memboot_log" ]; then
        ls -1t /misc/disk1/install-admin-*FC*.tgz | head -2 | awk -v c="$cp" -v n="$node_subdir" '{print c " ", $1, " " n}' | sh
    fi
    memboot_log=(/misc/disk1/install-admin-*SC*.tgz)
    if [ -n "$memboot_log" ]; then
        ls -1t /misc/disk1/install-admin-*SC*.tgz | head -2 | awk -v c="$cp" -v n="$node_subdir" '{print c " ", $1, " " n}' | sh
    fi
fi

find /install /install_repo > $node_subdir/install_files.txt 2>>$error_file
ls -l /install_repo/gl/host /install_repo/gl/calvados /install_repo/gl/xr /misc/disk1/tftpboot >> $node_subdir/install_files.txt 2>>$error_file
(find /install /install_repo/gl /install_repo/local -name "*.txt" -or -name "*.json"  | tar cf $node_subdir/install_files.tar -T -) 2>>$error_file

arg_string="$arg_string -t $__cardtype"

display() {
    print_main_heading "show tech-support install"

    run_module tech_install2 "$arg_string"
        
    print_main_heading "show tech-support install complete"
}        

#
# This function calls the display() function and sends the output to file if
# the file option has been set. 
#
source /opt/cisco/calvados/script/show_tech_file_fragment
j=(/var/log/host_user.log)
if [ -n "$j" ]; then
    $cp $j $node_subdir 2>>$error_file
fi
k=(/var/log/pxe_install.sh.flow.log)
if [ -n "$k" ]; then
    $cp $k $node_subdir 2>>$error_file
fi

if [ -f "$error_file" ]; then
   $cp  $error_file $node_subdir 2>/dev/null
fi

rm -f $error_file 2>/dev/null
