#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_gnss_platform - Runs show commands for show tech-support gnss 
#                     platform using the 'show_tech_fast' infrastructure.
#                     See EDCS-631313 for details on show_tech_fast
#
# January 2022, Manik Singhal
#
# Copyright (c) 2022 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

#
# show_tech_main_fragment is part of the show tech infrastructure
# and contains a number of functions and ksh variables that are used
# within this script.  We execute show_tech_main_fragment to
# essentially "#include" those functions.
#
. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified"       # "RP/LC/sys"
__gnss_type="unspecified"      # "trimble"/"ublox"
__gnss_location="unspecified"  # "RP"/"LC"
__gnss_os="unspecified"        # "exr"/"lindt"
__gnss_log_files=""            # Log files to bundle

sys_cmd_index=1
rplc_cmd_index=1

# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -t)      __cardtype="$2"; shift 2;;
        *)       default_parser_function "$@"; shift $#;;
    esac
done

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

#
# Attempts to determine what type of card we are executing
# on.
#
set_exr_vars() {

    trimble_cards="TORTIN-RP NCS560-RP TURINCR-RP BIFROSTT-RP"

    if [ -e /root/card_instances.txt ]; then
        card_instance=`cat /root/card_instances.txt`
        for card in $trimble_cards; do
            if [[ $card_instance == *$card* ]]; then
                __gnss_type="trimble"
                __gnss_location="RP"  #All trimble cards have gnss on RP
                __gnss_os="exr"
                __gnss_log_files="/misc/disk1/trimble_raw_dump.txt"
            fi
        done

        #Check for Eyrie
        if [[ $card_instance == *FRETTA-HYBRID-LC* ]]; then
            __gnss_type="ublox"
            __gnss_location="LC"
            __gnss_os="exr"
        fi

    fi
}

set_lindt_vars() {

    #plarform.txt get populated at setup_cpa.sh
    trimble_cards="Yukon RP2 SF-C"

    if [ -e /root/platform.txt ]; then
        card_instance=`cat /root/platform.txt`
        for card in $trimble_cards; do
            if [[ $card_instance == *$card* ]]; then
                  #All Lindt platforms have same GNSS parameters
                __gnss_type="ublox"
                __gnss_location="RP"  #All trimble cards have gnss on RP
                __gnss_os="lindt"
            fi
        done

    fi
}

#
# Init commands to be executed at the system level
#
init_sys_commands_common() {

    #
    # Use sys_cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    sys_exec[$sys_cmd_index]='show platform'
    sys__ksh[$sys_cmd_index]='show_platform_sysdb'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show version'
    sys__ksh[$sys_cmd_index]='ng_show_version'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show install active'
    sys__ksh[$sys_cmd_index]='sdr_instcmd show install active'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show inventory'
    sys__ksh[$sys_cmd_index]='show_inventory -e'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show running-config'
    sys__ksh[$sys_cmd_index]='nvgen -c -l 1 -t 1 -o 1'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show logging'
    sys__ksh[$sys_cmd_index]='show_logging'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show cli history detail'
    sys__ksh[$sys_cmd_index]='show_parser_history -h 0x2'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show configuration history'
    sys__ksh[$sys_cmd_index]='cfgmgr_show_history -s -f -n 0x5dc -t all -d'

    ((sys_cmd_index++))

    sys_exec[$sys_cmd_index]='show configuration commit changes all'
    sys__ksh[$sys_cmd_index]='show_config_changes -x'

    ((sys_cmd_index++))

    # MUST be the last command
    sys_exec[$sys_cmd_index]=''
    sys__ksh[$sys_cmd_index]=''
}

#
# Init commands to be executed at the system level
#
init_sys_commands_lindt() {

    #
    # Use sys_cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #

    # MUST be the last command
    sys_exec[$sys_cmd_index]=''
    sys__ksh[$sys_cmd_index]=''
}

#
# Init commands to be executed at the system level
#
init_sys_commands_exr() {

    #
    # Use sys_cmd_index rather than hardcoding the array indexes to
    # make it easier / less error prone to reorder commands.
    #
    sys_exec[$sys_cmd_index]='show redundancy location all'
    sys__ksh[$sys_cmd_index]='redcon_show -n all'

    ((sys_cmd_index++))

    # MUST be the last command
    sys_exec[$sys_cmd_index]=''
    sys__ksh[$sys_cmd_index]=''
}


init_gnss_commands_trimble() {

    #Query required information from the chip using TSIP messages. The response
    # shall be captured in the ltraces
    # 9103000000  - Query timing configuration from ICM720
    # 9101000000  - Query GNSS configuration from ICM720
    for test_cmd in 9103000000 9101000000; do
        rplc_exec[$rplc_cmd_index]="test gnss-ea raw-cmd $test_cmd location $ext_node_name"
        rplc__ksh[$rplc_cmd_index]="gnss_ea_test_cmd raw-cmd $test_cmd $fq_nodeid"
        ((rplc_cmd_index++))

        rplc_exec[$rplc_cmd_index]="Sleep for 1 second after $test_cmd"
        rplc__ksh[$rplc_cmd_index]='sleep 1'
        ((rplc_cmd_index++))
    done

    # MUST be the last command
    rplc_exec[$rplc_cmd_index]=''
    rplc__ksh[$rplc_cmd_index]=''
}

#
# Init commands which are same for eXR and Lindt
# for ublox
#
init_gnss_common_commands_ublox() {

    # MUST be the last command
    rplc_exec[$rplc_cmd_index]=''
    rplc__ksh[$rplc_cmd_index]=''
}

#
# Init commands which are same for eXR and Lindt
# and same for all types of GNSS receivers
#
init_gnss_common_commands() {

    rplc_exec[$rplc_cmd_index]='show gnss-receiver 0 location $ext_node_name'
    rplc__ksh[$rplc_cmd_index]='gnssmgr_show_receiver -r 0 -h $fq_nodeid'
    ((rplc_cmd_index++))

    rplc_exec[$rplc_cmd_index]='show controllers gnssea ltrace location $ext_node_name'
    rplc__ksh[$rplc_cmd_index]='show_gnss_ea_ltrace -I .* -i $fq_nodeid -T .*'
    ((rplc_cmd_index++))

    rplc_exec[$rplc_cmd_index]='show gnss-receiver trace location $ext_node_name'
    rplc__ksh[$rplc_cmd_index]='gnss_show_ltrace -i $fq_nodeid'
    ((rplc_cmd_index++))

    rplc_exec[$rplc_cmd_index]='test gnss-ea display-sl-num location $ext_node_name'
    rplc__ksh[$rplc_cmd_index]='gnss_ea_test_cmd display-sl $fq_nodeid'
    ((rplc_cmd_index++))

    # MUST be the last command
    rplc_exec[$rplc_cmd_index]=''
    rplc__ksh[$rplc_cmd_index]=''
}

#
# Init commands for ublox on exr
#
init_gnss_commands_ublox_exr() {

    # MUST be the last command
    rplc_exec[$rplc_cmd_index]=''
    rplc__ksh[$rplc_cmd_index]=''
}

#
# Init commands for ublox on lindt
#
init_gnss_commands_ublox_lindt() {

    ## GNSS ctrace
    rplc_exec[$rplc_cmd_index]='GNSS-EA ctrace'
    rplc__ksh[$rplc_cmd_index]='ctracedec -AdGHkstpNw gnss_ea'
    ((rplc_cmd_index++))

    # MUST be the last command
    rplc_exec[$rplc_cmd_index]=''
    rplc__ksh[$rplc_cmd_index]=''
}

#Collect additional files on disk into the show-tech
bundle_rplc_files() {

    #Expect files to be present either on RP or on LC as
    # timing is distributed across platforms
    if [ -d $__tar_file_directory_on_node ]; then

        #APR logs - Collect the trace file
        for src_file in $__gnss_log_files; do
            if [ -e "$src_file" ]; then
                dest_file=`basename $src_file`
                dest_file="${dest_file}_${fq_nodeid}.gz"
                gzip -c $src_file >$__tar_file_directory_on_node/$dest_file
            fi
        done
    fi
}

# ********************************
#  Main part of script starts here
# ********************************



# Note that showtech_node_name comes from show_tech_main_fragment
int_node_name=$showtech_node_name
ext_node_name=`node_conversion -E $int_node_name`
fq_nodeid=`node_conversion -i $int_node_name`

#
# display() is the function called by the show tech infrastructure
# to collect the collect/display the component specific info.
#
# Note that the same script executes on both the RP and the LCs
# concurrently.  So, below, we check to determine what kind of
# card this is and load/exec the correct commands.  exec_commands
# is a function provided by the show tech infrastructure.
#
display() {

    if [ -e /root/card_instances.txt ]; then
        set_exr_vars
    else
        set_lindt_vars
    fi

    if [ $__gnss_type == ublox ]; then
        __gnss_log_files="/misc/disk1/ublox_raw_dump.txt /tmp/gnss_driver*txt"
        __gnss_log_files="$__gnss_log_files /var/xr/disk1/ubx_gnss_saved_data.dat"
        if [ $__gnss_os == lindt ]; then
            __gnss_log_files="$__gnss_log_files /tmp/devobj_gnss_dbg_log.txt"
        fi
    fi

    print_main_heading "Running on card: $__cardtype"

    if [ $__cardtype == "DRP" ]; then
        if [ $__gnss_location == "RP" ]; then
            __gnss_location=$__cardtype;
        fi
    fi

    if [ "$__cardtype" == "SYS" ]; then

        init_sys_commands_common

        if [ $__gnss_os == exr ]; then
            init_sys_commands_exr
        elif [ $__gnss_os == lindt ]; then
            init_sys_commands_lindt
        fi

        exec_commands sys

    elif [ $__gnss_location == $__cardtype ]; then


        if [ $__gnss_type == "trimble" ]; then

            init_gnss_commands_trimble

        elif [ $__gnss_ytpe == "ublox" ]; then

            init_gnss_common_commands_ublox

            if [ $__gnss_os == "exr" ]; then
                init_gnss_commands_ublox_exr
            elif [ $__gnss_os == "lindt" ]; then
                init_gnss_commands_ublox_lindt
            fi
        fi

        #Keep it in the end, as trimble commands fetch
        #information from the module which are captured
        #in ltraces, which shall be collected now
        init_gnss_common_commands

        exec_commands rplc

        bundle_rplc_files
    fi
}

#
# show_tech_file_fragment is part of the show tech infrastructure and
# is the script that will call our display() function.  It also collects
# the output and writes it to a file if that file is specified.  It
# then compresses the file.
#
. /pkg/bin/show_tech_file_fragment

