#!/bin/bash

# this script will check for X9SCM BIOS version and if 2.3 or older update the BIOS

# start of functions

cd /tmp

funcBIOSUPDATE () {
  echo "Flashing BIOS"
  # apt-get update -y && apt-get -y install build-essential libelf-dev
  # this only works for bios 2.0 and up
  # wget http://184.154.112.130/tools/bios/afu_lnx_64 && wget http://184.154.112.130/tools/bios/x9scm_23a_full.rom && wget http://184.154.112.130/tools/bios/x9scm_23a_set.rom && chmod +x afu_lnx_64 && ./afu_lnx_64 /MAKEDRV /DPC && ./afu_lnx_64 x9scm_23a_full.rom /B /P /N /K /R /ME /MER /DPC && ./afu_lnx_64 x9scm_23a_set.rom /N /B /DPC 
  
  #works for all bios versions of x9scm/x9scl
  if [ $bios == 1.1 ]; then # this is needed because version 1.1 will not update with the /MER /FDR and /OPR flags
    wget http://184.154.112.130/tools/bios/afu_lnx_64 && wget http://184.154.112.130/tools/bios/x9scm_23a_full.rom && wget http://184.154.112.130/tools/bios/x9scm_23a_set.rom && chmod +x afu_lnx_64 && ./afu_lnx_64 /MAKEDRV /DPC && ./afu_lnx_64 x9scm_23a_full.rom /B /P /N /K /DPC  && ./afu_lnx_64 x9scm_23a_set.rom /N /B /DPC
    echo "*********BIOS WAS ON VERSION 1.1*********"
    echo "*****REBOOT SERVER VIA PDU AND RUN SCRIPT AGAIN****"
    exit 0
  fi

  wget http://184.154.112.130/tools/bios/afu_lnx_64 && wget http://184.154.112.130/tools/bios/x9scm_23a_full.rom && wget http://184.154.112.130/tools/bios/x9scm_23a_set.rom && chmod +x afu_lnx_64 && ./afu_lnx_64 /MAKEDRV /DPC && ./afu_lnx_64 x9scm_23a_full.rom /B /P /N /K /ME /MER /DPC /FDR /OPR && ./afu_lnx_64 x9scm_23a_set.rom /N /B /DPC
  
  if [[ $? == 0 ]]; then
    echo "BIOS Flashed"
    biosupdated=1
  else 
    echo "BIOS Update Failed"
  fi
}

funcIPMIUPDATE () {
  echo "IPMI firmware not up to date..." 
  echo "Downloading Files..."
  wget -q -c http://184.154.112.130/tools/ipmi/x9scmf/lUpdate && 
  wget -q -c http://184.154.112.130/tools/ipmi/x9scmf/SMT_X9_352.bin
  chmod +x lUpdate
  echo "Updating IPMI Firmware"
  /root/lUpdate -f SMT_X9_352.bin -i kcs -r y  > /dev/null #output was causing mem crashes, it now sends std out to /dev/null
  sleep 120
  echo $?

if [[ $? == 0 ]]; then
    echo "IPMI Firmware Updated!"
    ipmiupdated=1
  else 
    echo "IPMI Update Failed"
  fi
  echo "*****REBOOT SERVER VIA PDU TO APPLY CHANGES****"
  echo "==============================================="
  
  
}
# start of script

biosupdated=0
ipmiupdated=0

echo ""
echo "X9SCM/X9SCL BIOS + IPMI CHECKER"
echo "Board: "`dmidecode -t 1 |grep 'Product Name' |cut -d " " -f 3` #print mobo version
motherboard=$(dmidecode -t 1 |grep 'Product Name' |cut -d " " -f 3)
if [ $motherboard != "X9SCL/X9SCM" ]; then
  echo "MOTHERBOARD IS NOT X9SCL OR X9SCM. EXITING SCRIPT!!"
  exit 0
fi
bios=$(dmidecode -t 0 |grep Version | cut -d " " -f 2) # get bios version
echo "============================================="
echo "BIOS Version: $bios"
if [[ $bios == 2.3a ]]; then
  echo "BIOS IS UP TO DATE"
else
  funcBIOSUPDATE #call the function funcBIOSUPDATE
fi

ipmi_ver=$(ipmicfg -ver | cut -d: -f2- | sed 's/^ *//' | sed 's/ //g') # get ipmi version
latest_ver=3.52
latest_ver2=03.52
if [[ $ipmi_ver == $latest_ver ]] || [[ $ipmi_ver == $latest_ver2 ]]; then
  echo "============================================="
  echo "IPMI firmware version: $ipmi_ver"
  echo "IPMI Firmware is up to date"
  echo "==============================================="
  echo "*****REBOOT SERVER VIA PDU TO APPLY CHANGES****"
  
  exit 0
elif [[ $ipmi_ver == "" ]]; then
  echo "============================================="
  echo "This server does not have IPMI"
else
  echo "============================================="
  echo "IPMI firmware version: $ipmi_ver"
  funcIPMIUPDATE #call the function funcIPMIUPDATE
fi
echo "==============================================="

if [[ $biosupdated -eq 1 ]] || [[ $ipmiupdated -eq 1 ]] ; then # check if a bios update happened. it's best practice to power cycle the server after a bios flash
  echo "*****REBOOT SERVER VIA PDU TO APPLY CHANGES****"
  echo "==============================================="
fi
