#!/bin/bash

source /usr/bin/next_boot_mount_point

UPGRADE_OR_ROLLBACK_FILE="/var/lib/cumulus/image-upgrade/upgrade-or-rollback"
FW_TOP_LEVEL="/usr/share/mellanox/firmware/"

function upgradeFirmwareFromTargetPartition()
{
    local mountpoint="/mnt/next_partition"

    createMountPointForNextBootPartition $mountpoint

    target_fw_top_level=$mountpoint$FW_TOP_LEVEL
    target_fw_path=$target_fw_top_level$(ls $target_fw_top_level)

    echo "Upgrading firmware from the next boot partition from path $target_fw_path"
    /usr/bin/mlxfwmanager -u -D $target_fw_path/Spectrum -f
}

if [ -f $UPGRADE_OR_ROLLBACK_FILE ] && grep -q "upgrade" $UPGRADE_OR_ROLLBACK_FILE;
then
    echo "This is ISSU with A/B partitions. So, upgrading firmware from the target partition"
    upgradeFirmwareFromTargetPartition
fi