#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd)"
OS_VERSION=$(uname -r | awk -F"el" '{print $2}' | awk -F"." '{print $1}')
BIOS_VERSION=$(dmidecode -t 0 | awk '/Version/{print $NF}' | tr -d '[:space:]')
PRODUCT_NAME=$(ipmitool fru print 0 | awk -F":" '/Product Name/{print $NF}' | sed 's/^\s*//g')
CPU_MODEL_INFO=$(lscpu | awk -F': +' '/Model name:/{gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2}')
CPU_LAST_DIGIT=$(echo "$CPU_MODEL_INFO" | awk '{for(i=NF; i>0; i--) if($i ~ /^[0-9]+$/) {print substr($i, length($i)); exit}}')

if [ -z "$CPU_MODEL_INFO" ]; then
    echo "No model number found."
    exit 1
fi

update_bios_config() {
    local config_file=$1
    if [[ "${OS_VERSION}" == "7" && ("${BIOS_VERSION}" == "T1ADG03.00" || "${BIOS_VERSION}" == "5.66") ]]; then
        ./SCELNX_64_5.05.01.0002_2021 /i /s "$config_file"
    else
        echo "BIOS update not applicable for this system configuration."
        exit 1
    fi
}

if [[ "${PRODUCT_NAME}" == "GPU SERVER" ]] && [[ "$CPU_LAST_DIGIT" == "2" ]]; then
    update_bios_config "LEXUN_20240417_bios.cfg"
elif [[ "${PRODUCT_NAME}" == "R5300 G5" ]]; then
    update_bios_config "R5300_G5_20250513_EFI.cfg"
else
    echo "This ${PRODUCT_NAME} does not have a BIOS configuration file."
    exit 1
fi
