#!/bin/bash
# ------------------------------------------------------------------
# Nov 2019, Sureshkumar Ganesan (sureshga@cisco.com)
#
# Copyright (c) 2019-2023 by Cisco Systems, Inc.
# All rights reserved.
# ------------------------------------------------------------------

FPD_COOKIE_PATH="/misc/disk1"
FPD_COOKIE_FILENAME="auto-fpd-upgrade-config.bin"
FPD_COOKIE_FILE="$FPD_COOKIE_PATH/$FPD_COOKIE_FILENAME"

#
# Platform-specific setup
#
if [ -f /misc/scratch/bootup_fpd_upgrade_log ]; then
    rm /misc/scratch/bootup_fpd_upgrade_log*
fi

function prog_exec_bootup_auto_fpd_upgrade
{
    platform_log_choose_log_file
    platform_log "exec: $*"
    platform_log "    : in cwd" `pwd`

    $* >&2 1>> $PLATFORM_LOG_FILE

    return ${PIPESTATUS[0]}
}

log_bootup_fpd_upgrade() {
    declare -F platform_log &>/dev/null && platform_log "$*"
}

log_bootup_fpd_upgrade_exec() {
    declare -F platform_log_exec &>/dev/null && platform_log_exec "$*"
}

if [ -f /etc/rc.d/init.d/pd-functions ]
then
    source /etc/rc.d/init.d/pd-functions
fi


# Determine if this is the active RP or not
#
# Returns 0 if active RP
# Returns 1 if not active RP
function is_active_rp {
    local _result

    get_board_type
    if [ "${BOARDTYPE}" == "LC" ]; then
        return 1
    fi

    # (fpga_get_rp_hw_arb_result and RP_HW_ARB_RESULT_ACTIVE come from 
    # fpga-functions)
    fpga_get_rp_hw_arb_result _result

    if [ "$_result" -eq "${RP_HW_ARB_RESULT_ACTIVE}" ]; then
        return 0
    else
        return 1
    fi
}

log_bootup_fpd_upgrade "Starting"

PROCNAME=$0

# Sync Auto FPD config cookie from Active RP to across nodes on 
# multi node system
pd_is_multi_xr_node
multi_node=$?
if [ $multi_node -eq 0 ]; then
    # Single node system
    log_bootup_fpd_upgrade "Single node: Auto FPD config cookie sync is not required"
else
    # Multi node system
    log_bootup_fpd_upgrade "Multi node: Auto FPD config cookie sync is required"

    if is_active_rp; then
        # Auto FPD config cookie on Active RP
        log_bootup_fpd_upgrade "Auto FPD config on Active RP"
    else
        # Always rely cookie from Active RP.
        # Hence delete the local cookie if it is available
        if [ -f $FPD_COOKIE_FILE ]; then
            rm $FPD_COOKIE_FILE
        fi
        # Get Auto FPD config cookie from Active RP.
        log_bootup_fpd_upgrade "Auto FPD config syncing from Active RP"
        pd_get_active_rp_ip_addr active_rp_ip_addr
        for i in {1..10}
        do
            exit_code=0
            log_bootup_fpd_upgrade_exec timeout 12s wget --tries 1 --timeout 10 http://${active_rp_ip_addr}:82/${FPD_COOKIE_FILENAME} -O /tmp/$FPD_COOKIE_FILENAME || exit_code=$?
            if [ $exit_code -eq 0 ]; then
                log_bootup_fpd_upgrade_exec mv /tmp/$FPD_COOKIE_FILENAME $FPD_COOKIE_FILE
                log_bootup_fpd_upgrade "Auto FPD config synced from Active RP"

                # Make the cookie available in the Standby RP www repo,
                # so that it can be served when requested after RPFO.
                get_board_type
                if [ "${BOARDTYPE}" == "RP" ]; then
                    ln -sf $FPD_COOKIE_FILE /var/www/auto-fpd-upgrade-config.bin
                fi

                break
            else
                # Sleep for 5 seconds and retry if cookie is not found in
                # remote RP
                sleep 5
            fi
        done
    fi
fi

if [ -f /pkg/etc/xr_startup_envs.sh ]; then
    source /pkg/etc/xr_startup_envs.sh
fi

if [ -f /pkg/bin/bootup_fpd_tool ]; then
    platform_info_setup
    #check if running on hardware and then execute the auto upgrade tool
    if [ "$SIM" == "true" ]; then
        log_bootup_fpd_upgrade "SIM: Auto FPD upgrade is not supported" 
        if [ -f /pkg/bin/bootup_fpd_tool_test ]; then
            prog_exec_bootup_auto_fpd_upgrade bootup_fpd_tool_test -c upgrade
        fi
    else
        prog_exec_bootup_auto_fpd_upgrade bootup_fpd_tool -c upgrade
        log_bootup_fpd_upgrade "Auto FPD upgrade Started"
        log_bootup_fpd_upgrade "Auto FPD upgrade Completed" 
    fi
fi

