@echo off


@SET WORKSPACE=%cd%
@SET STITCH_REBUILD_PATH=StitchRebuild
@SET STITCH_PATH=Stitch


@for /f "tokens=4" %%a in ('find "EC_VERSION" %WORKSPACE%\BinConfig.env') do @set EC_Version=%%a
@for /f "tokens=4" %%a in ('find "ISH_VERSION" %WORKSPACE%\BinConfig.env') do @set ISH_Version=%%a
@for /f "tokens=4" %%a in ('find "PMC_VERSION" %WORKSPACE%\BinConfig.env') do @set PMC_Version=%%a
@for /f "tokens=4" %%a in ('find "MCU_PATCH_VERSION" %WORKSPACE%\BinConfig.env') do @set Patch_Version=%%a
@for /f "tokens=4" %%a in ('find "FSP_ENABLE" %WORKSPACE%\Project.env') do @set FSP_ENABLE=%%a
@for /f "tokens=4" %%a in ('find "CSE_VERSION" %WORKSPACE%\BinConfig.env') do @set CSE_Version=%%a

@if exist %WORKSPACE%\bios.bin (
     @del /s /q %WORKSPACE%\bios.bin
)

@if exist %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin (
	@del /s /q %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
	mkdir %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
)

REM Gen NvStore
  GenNvStoreFv.exe %WORKSPACE%\FV\GEMINILAKE.fd -o %WORKSPACE%\input\NvStore.Fv
  copy %WORKSPACE%\input\NvStore.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
if "%FSP_ENABLE%" == "YES" (
  REM Gen PAD for MEU
  copy /y /b %WORKSPACE%\FV\IBBM.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy /y /b %WORKSPACE%\FV\FSP-S.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy /y /b %WORKSPACE%\FV\FVIBBL_FSP-T.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin\FVIBBL.Fv
  copy %WORKSPACE%\FV\RECOVERYFV.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\FVMAIN_COMPACT.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\NVCOMMON.fd %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  GenPad.exe %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\xml\GELKBiosFspW.xml -o %WORKSPACE%\input\PAD.bin
) else (
  REM Gen PAD for MEU
  copy %WORKSPACE%\FV\IBBFV.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\FVIBBL.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\RECOVERYFV.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\FVMAIN_COMPACT.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\NVCOMMON.fd %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  GenPad.exe %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\xml\GELKBios.xml -o %WORKSPACE%\input\PAD.bin
)
REM Gen BPM for MEU
  GenBpmLite.exe -i %WORKSPACE%\FV\FVIBBL.Fv %WORKSPACE%\FV\IBBFV.Fv %WORKSPACE%\FV\RECOVERYFV.Fv -o %WORKSPACE%\input\BPM.bin
if "%FSP_ENABLE%" == "YES" (
  copy /y /b %WORKSPACE%\FV\IBBM.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy /y /b %WORKSPACE%\FV\FSP-S.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy /y /b %WORKSPACE%\FV\FVIBBL_FSP-T.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin\FVIBBL.Fv
) else (
  copy %WORKSPACE%\FV\IBBFV.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\FVIBBL.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
)
  copy %WORKSPACE%\FV\RECOVERYFV.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\FVMAIN_COMPACT.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
  copy %WORKSPACE%\FV\NVCOMMON.fd %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin

pushd %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU

meu -f xml/OEMKeyManifest.xml -o oemkeymn2.bin -u1 . -key keys/bxt_dbg_priv_key.pem -w out
move /y oemkeymn2.bin   %WORKSPACE%\input\oemkeymn2.bin
if "%FSP_ENABLE%" == "YES" (
  meu -f %WORKSPACE%/CSE/%CSE_Version%/GELK/MEU/xml/GELKBiosFspW.xml -o bios.bin -u1 . -key keys/bxt_dbg_priv_key.pem -w out -s input
) else (
  meu -f %WORKSPACE%/CSE/%CSE_Version%/GELK/MEU/xml/GELKBios.xml -o bios.bin -u1 . -key keys/bxt_dbg_priv_key.pem -w out -s input
)

move /y bios.bin %WORKSPACE%\input\bios.bin
copy /y %WORKSPACE%\input\bios.bin %WORKSPACE%
popd

echo.
echo  ----------  Bios Bin File Complete  ----------