@echo off       
setlocal EnableDelayedExpansion

set ROOT=%~dp0%

set OPENSSL_BIN="c:\OpenSSL-Win64\bin\openssl.exe"
set PRIVATE_KEY="c:\src\cert\blockstorage_dev_private.pem"
set PUBLIC_KEY="%ROOT%blockstorage_dev_public.pem"

set SED_BIN=%ROOT%bin\sed.exe
set GREP_BIN=%ROOT%bin\grep.exe
set B64_BIN=%ROOT%bin\b64.exe
set CURL_BIN=%ROOT%bin\curl\bin\curl.exe
set JQ_BIN=%ROOT%bin\jq-win64.exe

set ERROR_EXIT=0

pushd "%ROOT%"
  for /f "tokens=*" %%i in ('%GREP_BIN% -m1 -a version package.json ^| %SED_BIN% -e "s/""""//g" ^| %SED_BIN% -e "s/version: //g" -e "s/,//g"') do (
    set APP_VER=%%i
  )
  set OUT_FILE=repertory-ui_%APP_VER%_win.exe

  call npm run dist && (
    pushd dist
      (certutil -hashfile "%OUT_FILE%" SHA256 | %SED_BIN% -e "1d" -e "$d" -e "s/\ //g") > "%OUT_FILE%.sha256" || (call :ERROR "Create sha-256 failed")
      "%OPENSSL_BIN%" dgst -sha256 -sign "%PRIVATE_KEY%" -out "%OUT_FILE%.sig" "%OUT_FILE%" || (call :ERROR "Create signature failed")
      "%B64_BIN%" -e "%OUT_FILE%.sig" "%OUT_FILE%.sig.b64" || (call :ERROR "Create base64 failed")
      for /f "delims=" %%i in ('type %OUT_FILE%.sig.b64') do set APP_SIG=!APP_SIG!%%i
      for /f "delims=" %%i in ('type %OUT_FILE%.sha256') do set APP_SHA256=!APP_SHA256!%%i

      del /q upload_response.json 1>NUL 2>&1

      ("%CURL_BIN%" -F name="%OUT_FILE%" -F anonymous=true -F file="@%OUT_FILE%" https://pixeldrain.com/api/file > upload_response.json) || (
        call :PIXEL_RESPONSE 0
      )
      call :PIXEL_RESPONSE 1
      set PIXEL_LOCATION=https://pixeldrain.com/api/file/!PIXEL_ID!

      del /q releases.json 1>NUL 2>&1

      ("%JQ_BIN%" ".Versions.win32|=(.+ ["""%APP_VER%"""]|unique)" ..\releases.json>releases_temp.json && move /Y releases_temp.json releases.json 1>NUL 2>&1) || (call :ERROR "Update releases.json Versions failed")
      ("%JQ_BIN%" ".Locations.win32."""%APP_VER%""".sig="""!APP_SIG!"""" releases.json>releases_temp.json && move /Y releases_temp.json releases.json 1>NUL 2>&1) || (call :ERROR "Update releases.json sig failed")
      ("%JQ_BIN%" ".Locations.win32."""%APP_VER%""".sha256="""!APP_SHA256!"""" releases.json>releases_temp.json && move /Y releases_temp.json releases.json 1>NUL 2>&1) || (call :ERROR "Update releases.json sha256 failed")
      ("%JQ_BIN%" ".Locations.win32."""%APP_VER%""".urls=["""!PIXEL_LOCATION!"""]" releases.json>releases_temp.json && move /Y releases_temp.json releases.json 1>NUL 2>&1) || (call :ERROR "Update releases.json URL failed")
    popd
  ) || (
    call :ERROR "Create dist failed"
  )
popd
goto :END
        
:NO_QUOTES
  set %~1=!%~1:"=!
goto :EOF 

:PIXEL_RESPONSE
  set PIXEL_RESPONSE=
  if %1==1 (
    for /f "delims=" %%i in ('%JQ_BIN% .success upload_response.json') do (
      if "%%i" == "false" (
        for /f "delims=" %%i in ('%JQ_BIN% .message upload_response.json') do (
          set PIXEL_RESPONSE=!PIXEL_RESPONSE!%%i
        )
        call :ERROR "Upload to pixeldrain failed: !PIXEL_RESPONSE!"
      ) else (
        for /f "delims=" %%i in ('%JQ_BIN% .id upload_response.json') do (
          set PIXEL_ID=%%i
          call :NO_QUOTES PIXEL_ID
        )
      )
    )
  ) else (
    for /f "delims=" %%i in ('type upload_response.json') do (
      set PIXEL_RESPONSE=!PIXEL_RESPONSE!%%i
    )
    call :ERROR "Upload to pixeldrain failed: !PIXEL_RESPONSE!"
  )
goto :EOF

:ERROR
  echo %1
  set ERROR_EXIT=1
  if "%DISABLE_PAUSE%" NEQ "1" (
    pause
  )
goto :END 

:END
  if "!ERROR_EXIT!" NEQ "0" (
    exit !ERROR_EXIT!
  )