@echo off

REN AFUDOSU.SMC AFUDOSU.EXE

IF "%1"=="" goto usage
IF NOT EXIST %1 goto file_err

:: check SMBIOS need save/restore or not
DMItool.exe s
if errorlevel == 0 set DMI_Restore=1
if errorlevel == 1 set DMI_Restore=0

:: check ME need update or not
set ME_Update=0
BIOStool.exe %1
if errorlevel == 1 set ME_Update=1

:: SMBIOS don't need save/restore, afudos /R is working
if %DMI_Restore%==0 afudosu %1 /P /B /N /K /R
:: SMBIOS need save/restore by tool, don't use /K 
if %DMI_Restore%==1 afudosu %1 /P /B /N /K


if %DMI_Restore% == 0 goto Update_ME

echo @DMItool.exe R > AMI2.BAT
echo "#################################################################"
echo "###  Please restart your system and boot to DOS run AMI2.BAT  ###"
if %ME_Update%==0 echo "###  to restore SMBIOS                                        ###"
if %ME_Update%==1 echo "###  to restore SMBIOS and Update ME                          ###"
echo "###  ex:    a:\> AMI2.BAT                                     ###"
echo "#################################################################"
if %ME_Update%==0 goto End

echo @REN AFUDOSU.SMC AFUDOSU.EXE >> AMI2.BAT
echo @afudosu.exe %1 /FDT /MER /OPR >>AMI2.BAT
echo @REN AFUDOSU.EXE AFUDOSU.SMC >> AMI2.BAT
goto End

:Update_ME
if %ME_Update%==0 goto End
echo "#################################################################"
echo "###  Please restart your system and boot to DOS run AMI2.BAT  ###"
echo "###  to Update ME                                             ###"
echo "###  ex:    a:\> AMI2.BAT                                     ###"
echo "#################################################################"

echo @REN AFUDOSU.SMC AFUDOSU.EXE > AMI2.BAT
echo @afudosu.exe %1 /FDT /MER /OPR >>AMI2.BAT
echo @REN AFUDOSU.EXE AFUDOSU.SMC >> AMI2.BAT
goto End

:file_err
echo "-= %1 is wrong file name =-"

:usage
echo usage AMI.BAT  BIOSFILE.BIN

:End
REN AFUDOSU.EXE AFUDOSU.SMC
