@echo off

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

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
  echo %OUT_FILE%

  npm run dist && (
    pushd dist
      echo Signing Installation [%OUT_FILE%]
      (certutil -hashfile "%OUT_FILE%" SHA256 | "%SED_BIN%" -e "1d" -e "$d" -e "s/\ //g") > "%OUT_FILE%.sha256"
      "%OPENSSL_BIN%" dgst -sha256 -sign "%PRIVATE_KEY%" -out "%OUT_FILE%.sig" "%OUT_FILE%"
      "%B64_BIN%" -e "%OUT_FILE%.sig" "%OUT_FILE%.sig.b64"
    popd
  ) || (
    echo Failed
  )
popd