@echo off & setlocal

SET ps=Add-Type -AssemblyName PresentationFramework; $files = @(
SET ps=%ps% [pscustomobject]@{'msg'='Please extract FLHubTool_v18.0.15.0.7z'; 'f'='FLHubTool.exe'; 'd'=''};
SET ps=%ps% [pscustomobject]@{'msg'='Please put FIRMWARE_Merged_FL5801_1U5D_V13.bin in current folder'; 'f'='FIRMWARE_Merged_FL5801_1U5D_V13.bin'; 'd'=''};
SET ps=%ps% [pscustomobject]@{'msg'='Please extract Thunderbolt FwUpdateTool.rar'; 'f'='DeviceFWUTool.exe'; 'd'=''};
SET ps=%ps% [pscustomobject]@{'msg'='Please put GoodWay_Gatkex_GR_EP_B1_4C_REV39_SEC3_sign.bin in current folder'; 'f'='GoodWay_Gatkex_GR_EP_B1_4C_REV39_SEC3_sign.bin'; 'd'=''};
SET ps=%ps% [pscustomobject]@{'msg'='Please extract Gatkex_Creek_PD_v3_3_1_112_v3_0_1_DMC_v3_3_0_1746_v1_3_22_Composite_0_0_0_5_password_ifx123.zip'; 'f'='ezpd_dockupdatefw.exe'; 'd'=''};
SET ps=%ps% [pscustomobject]@{'msg'='Please extract Gatkex_Creek_PD_v3_3_1_112_v3_0_1_DMC_v3_3_0_1746_v1_3_22_Composite_0_0_0_5_password_ifx123.zip'; 'f'='5_fw_update.bat'; 'd'=''});
SET ps=%ps% $curDir = (Get-Location); $correct = 0; Foreach ($item in $files) {
SET ps=%ps%   $i=(Get-ChildItem -Path (Get-Location) -Filter $item.f -Recurse -ErrorAction SilentlyContinue -Force);
SET ps=%ps%   if ([bool]($i)) { $correct = $correct + 1; $item.d = $i[0].Directory.FullName } else { [System.Windows.MessageBox]::Show($item.msg); break; } }
SET ps=%ps% if ($correct -eq $files.count) { Write-Host ($files[0].d+'/'+$files[1].d+'/'+$files[2].d+'/'+$files[3].d+'/'+$files[4].d) } else { Write-Host 'Error' }

for /f "usebackq delims=" %%i in (`
  powershell -c "%ps%"
`) do set "var=%%i"

if "%var%" == "Error" (goto end)

SET ps=Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('Start to Update');
powershell -c "%ps%"

for /f "tokens=1,2,3,4,5 delims=/" %%a in ("%var%") do (
   set var1=%%a\FLHubTool.exe
   set var2=%%b\FIRMWARE_Merged_FL5801_1U5D_V13.bin
   set var3=%%c\DeviceFWUTool.exe
   set var4=%%d\GoodWay_Gatkex_GR_EP_B1_4C_REV39_SEC3_sign.bin
   set var5=%%e
)

set local=%cd%

"%var3%" /q %var4%

TIMEOUT 10

"%var1%" -dev_0 -firmware_5KG2 -update %var2%

cd %var5%
call 5_fw_update.bat
cd %local%

:end:

@echo on

