set ftmurl=%baseurl%/ostremote/v2/EnterFTMCommand

echo Obtaining FTM Commands...
:Defining FIH Build Version
fb2 -s %psn% oem getversions 2>&1 | findstr software > %temp%\swver.txt
for /f "tokens=1* delims==" %%i in ( %temp%\swver.txt ) do set cswver=%%j
del "%temp%\swver.txt"

curl -L -s -X POST -H ""User-Agent": "OSTRemote2-%mainsum:~0,4%%compsum:~0,4%/1.0"" -H "Content-Type: application/json" -H ""Auth": "%otp%"" -d {\"CSWVer\": \"%cswver%\",\"platform\":\"Win32\",\"psn\":\"%psn%\"}" %ftmurl% -o %temp%\output.txt

jq-win32 -r ".status" %temp%\output.txt > %temp%\onlinestatus.txt
set /p onlinestatus=<%temp%\onlinestatus.txt
if not "%onlinestatus%"=="Fail" goto passcheck
jq-win32 -r ".code" %temp%\output.txt > %temp%\onlinestatus.txt
set /p errorcode=<%temp%\onlinestatus.txt
if defined errorcode goto EOF

:passcheck

jq-win32 -r ".signature" %temp%\output.txt > %temp%\rawbase64.txt
>nul certutil -f -decode %temp%\rawbase64.txt %temp%\encuid_sec4-%prjcode%-%uid%.bin
del %temp%\rawbase64.txt
:reuse
fb2 -s %psn% flash encUID %temp%\encuid_sec4-%prjcode%-%uid%.bin 2>&1 | findstr FAILED > nul
fb2 -s %psn% oem selectKey service 2>nul
fb2 -s %psn% oem doKeyVerify 2>&1 | findstr FAILED > nul

rem if %errorlevel%==0 (
rem echo %t0043%
rem pause>nul
rem del %temp%\encuid_sec4-%prjcode%-%uid%.bin
rem goto retry_sec4u
rem )

fb2 -s %psn% oem allport 2>nul
echo %t0053%
goto EOF

:EOF
