#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# save_np_init_logs - Generates a debug file that contains information
#                     useful for debugging NP/prm init failures.
# Arguents:
#             -h (optional) dump help info
#
#             -n (optional) np number of np for which to collect data.
#                If not specified, data is collected for all NPs.
#     
#             -r (optional) rp number of rp to where we will scp debug
#                files.   If not specified, data is still collected,
#                but left on linecard filesystem.
#
# April 2017, Doug Rex
#
# Copyright (c) 2017-2018 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

rp_dir="/misc/scratch/np"
lc_tmp_dir="/tmp"
file_string="np_init_logs"
date=`date +%Y%m%d-%H%M%S`
issu_role="1"
np_num="-1"
rp_num="-1"

USAGE="Usage: `basename $0`\n"
USAGE+=" -h : print this help\n"
USAGE+=" -i <issu_role> : ISSU role, 1 for primary or 2 secondary (defaults to primary if not specified)\n"
USAGE+=" -n <np_number> : capture debug data for this np (defaults to all if not specified)\n"
USAGE+=" -r <rp_number> : scp debug files over to this rp\n"

# Parse command line options.
while getopts hi:n:r: OPT; do
    case "$OPT" in
        h)
            echo -e $USAGE
            exit 0
            ;;
        i)
            issu_role=$OPTARG
            if [ $issu_role != "1" ];then
                if [ $issu_role != "2" ];then
                    echo "Invalid ISSU role!"
                    exit 1
                fi
            fi                
            ;;
        n)
            np_num=$OPTARG
            ;;
        r)
            rp_num=$OPTARG
            ;;
        \?)
            # getopts issues an error message
            echo $USAGE >&2
            exit 1
            ;;
    esac
done


if [ "$rp_num" != "-1" ];then

    # bundle up all np init debug files and scp over to this rp_num

    rp_path="rp$rp_num"
    rp_path+="_xr"

    if [ "$issu_role" == "2" ];then
        rp_path+="2"
    fi

    cat /etc/hosts | grep -q $rp_path

    if [ $? -ne 0 ];then
        echo "Invalid RP path!"
        exit 1
    fi

    echo "creating server trace logs"

    
    show_npu_server_ltrace -E > /misc/scratch/np/error_trace_$date.txt
    
    show_npu_server_ltrace -N > /misc/scratch/np/init_trace_$date.txt

    show_npu_server_ltrace -I > /misc/scratch/np/interrupt_trace_$date.txt

    show_npu_server_ltrace -v > /misc/scratch/np/event_trace_$date.txt

    

    ext_node_name=`node_list_generation -f MY | sed -e "s/\//_/g"`

    tar_file_name="$file_string.$date.$ext_node_name.tgz"

    tar cvzf /tmp/$tar_file_name -C /misc/scratch/np . 1>/dev/null 2>&1
    
    if [ $? -ne 0 ];then
        echo "tar command failed with return code of $?"
        exit 1
    fi

    scp $lc_tmp_dir/$tar_file_name $rp_path:$rp_dir/$tar_file_name

    if [ $? -ne 0 ];then
        echo "scp command failed with return code of $?"
        exit 1
    fi

    rm -f /misc/scratch/np/error_trace_$date.txt
     
    rm -f /misc/scratch/np/init_trace_$date.txt

    rm -f /misc/scratch/np/interrupt_trace_$date.txt

    rm -f /misc/scratch/np/event_trace_$date.txt


fi

exit
