#!/bin/sh
#   flash for S5000 systems (flash BIOS, BMC, FRUSDR)
#
# Do "cat master.ac-patch |patch -p0" to patch MASTER.CFG into MASTER.TIGW1U.AC.CFG.
fwdir=$DeployPath/update
flashdir=$DeployPath/Linux/ofu
mastercfg=$fwdir/MASTER.CFG
logfile=$DeploymentStore/flash.log
chassis=`queryProperty FRU "Chassis OEM Field" - info="Basbrd Mgmt Ctlr"`
ADDLOG=addLogRecord
# ADDLOG=echo
$ADDLOG FLASH INFO "logfile=$logfile"

date >$logfile
cd $flashdir
cp -f $DeployPath/Linux/drivers/amifldrv_mod.o .  2>>$logfile
# Sometimes there is a mismatch in the flashupdt afu version, so
# copy over the afu app from the afu directory.
# cp -f $DeployPath/Linux/drivers/afu/afulnx2_32 .  2>>$logfile

# Replace the existing MASTER.CFG with one that does not prompt,
if [ "$chassis" = "SR2500LX" ]
then
    cp -f $fwdir/MASTER.SR2500LX.AC.CFG        $fwdir/MASTER.CFG
else
    cp -f $mastercfg        $fwdir/MASTER.CFG.SAVE
    setMASTER.sh          > $fwdir/MASTER.CFG
    cat  $mastercfg.SAVE >> $fwdir/MASTER.CFG
fi

# Display the flash versions
./flashupdt -i  >>$logfile 2>&1
ret=$?
if [ $ret -eq 0 ]
then
    $ADDLOG FLASH INFO "Success: flashupdt info completed"
else
    $ADDLOG FLASH ERROR "Error $ret: flashupdt info failed"
fi

# if [ -f "$DeployPath/Linux/kiratool" ]
# then
#     echo Disable the RMM2 IPMI passthru as not to interfere with HSC during flash.
#     kiratool -u admin -p password  cfg set ipmi.medium._c_ none
# fi

# Flash the BIOS, and firmware + FRU/SDR also if S5000
./flashupdt -u $fwdir | egrep -v "Reading|Updating|Preparing"  >>$logfile 2>&1
ret=$?
if [ $ret -eq 0 ]
then
  # Since flashupdt does not always set the return code on errors,
  # use grep to detect errors from the logfile.
  grep "ERROR" $logfile >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    ret=1
  fi
fi
if [ $ret -eq 0 ]
then
 $ADDLOG FLASH INFO "Success: flashupdt completed"
else
 $ADDLOG FLASH ERROR "Error $ret: flashupdt failed"
fi

# if [ -f "$DeployPath/Linux/kiratool" ]
# then
#     echo Re-enable the RMM2 IPMI passthru
#     kiratool -u admin -p password  cfg set ipmi.medium._c_ i2c
#     #REM  It may happen that the RMM2 behaves strangely after that, so
#     echo Wait 30 seconds for RMM2 to initialize, then we can reset
#     sleep 30
#     echo Reseting RMM2....
#     kiratool -u admin -p password reset
#     echo This is not an error.  RMM2 is still in the process of resetting.
#     echo waiting 3 more minutes....
#     sleep 180
#     kiratool -u admin -p password fw
#     if [ $? -eq 0 ]
#     then
#         echo RMM2 reset complete.
#     else
#         echo RMM2 reset FAILED.
#     fi
# fi

echo "flashupdt from $fwdir returned $ret"
exit $ret
