#!/bin/bash

# 功能: 更新 storcli工具并升级 RAID卡固件
# 依赖: 当前目录需包含 storcli RPM 包和 .rom 固件文件

CPWD=$(cd "$(dirname "$0")" && pwd)
cd "$CPWD" || exit 1

RPM_FILE="storcli-007.3205.0000.0000-1.noarch.rpm"
ROM_FILE="9540-8i_NOPAD.rom"
STORCLI_PATH="/opt/MegaRAID/storcli/storcli64"
CONTROLLER_ID="/c0"  # Default controller ID

# Check storcli RPM package
storcli_version=$(rpm -q storcli)

if [[ ${storcli_version} != "storcli-007.3205.0000.0000-1.noarch" ]]; then
    if ! rpm -Uvh "$RPM_FILE"; then
        echo >&2 "Error: RPM installation failed. Check permissions or dependencies."
        exit 1
    fi
fi


# Verify storcli64 executable
if [[ ! -x "$STORCLI_PATH" ]]; then
    echo >&2 "Error: storcli64 not found or not executable at $STORCLI_PATH"
    exit 1
fi

# Verify storcli version
if ! "$STORCLI_PATH" -V; then
    echo >&2 "Warning: Failed to get storcli version. Environment might be unstable."
fi

# Flash firmware to controller
if ! "$STORCLI_PATH" "$CONTROLLER_ID" download file="$ROM_FILE"; then
    echo >&2 "Error: Firmware flash failed. Check controller status or ROM compatibility."
    exit 1
fi

echo "Firmware update completed successfully."
