set sec8url=%baseurl%/ostremote/v2/GetSignature8
for /f %%a in ( 'guidgen -c 1' ) do set sessioncode=%%a

:retry_sec8v
echo %t0007%%secver%
echo %t0004%%psn%

:Defining Project Code
fb2 -s %psn% oem getProjectCode 2>&1 | findstr getProjectCode > %temp%\prjcode.txt
For /f "tokens=1* delims= " %%A in ( %temp%\prjcode.txt ) Do set prjcode=%%B
if "%psn:~0,3%"=="EAG" set prjcode=EAG
if "%psn:~0,3%"=="RHD" set prjcode=RHD
del %temp%\prjcode.txt
echo %t0006%%prjcode%

:Defining Brand Code
fb2 -s %psn% oem getBrandCode 2>&1 | findstr getBrandCode > %temp%\brandcode.txt
For /f "tokens=1* delims= " %%A in ( %temp%\brandcode.txt ) Do set brandcode=%%B
del %temp%\brandcode.txt
echo %t0026%%brandcode%

:Defining Veracity Challenge Code
fb2 -s %psn% oem dm-veracity 2>&1 | findstr veracity > %temp%\veracity.txt
For /f "tokens=1* delims= " %%A in ( %temp%\veracity.txt ) Do set veracity=%%B
echo %veracity%>%temp%\veracity.txt
xxd -r -ps %temp%\veracity.txt %temp%\veracity.bin
openssl base64 -e -in %temp%\veracity.bin -out %temp%\veracity.txt
set /p digest=<%temp%\veracity.txt
del %temp%\veracity.bin
del %temp%\veracity.txt
echo %t0041% %veracity%
rem echo %z0002%
echo.

:Input_Encoding_MSG
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 "{\"prjcode\":\"%prjcode%\",\"psn\":\"%psn%\",\"imei1\":\"%imei1%\",\"imei2\":\"%imei2%\",\"meid\":\"%meid%\",\"brand\":\"%brandcode%\",\"digest\":\"%digest%\",\"type\":\"v\",\"skuid\":\"%targetskuid%\",\"mlffile\":\"%mlffilename%\",\"frp\":\"%erase_frp%\"}" %sec8url% -o %temp%\output.txt

set resulterrorcode=%errorlevel%
if not "%resulterrorcode%"=="0" (
echo %t0094% %resulterrorcode%
set errorserverdown=1
goto EOF
)

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

set /p onlinestatus=<%temp%\onlinestatus.txt
jq-win32 -r ".signature" %temp%\output.txt > %temp%\rawbase64.txt
openssl base64 -d -in %temp%\rawbase64.txt -out %temp%\veracity_sec8-%brandcode%-%prjcode%-%psn%-%veracity%.bin
del %temp%\rawbase64.txt
fb2 -s %psn% flash veracity %temp%\veracity_sec8-%brandcode%-%prjcode%-%psn%-%veracity%.bin 2>&1 | findstr FAILED > nul

rem if %errorlevel%==0 (
rem echo %t0043%
rem pause>nul
rem goto retry_sec8v
rem )

del %temp%\veracity_sec8-%brandcode%-%prjcode%-%psn%-%veracity%.bin
echo %t0053%
goto EOF

:EOF