@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SERVER=https://tvstudio.online"
set "VSDIR=%USERPROFILE%\.vs-review"
set "KEY=%VSDIR%\id_ed25519"
set "RESP=%VSDIR%\response.txt"
set "STATUS=%VSDIR%\status.txt"

where ssh-keygen >nul 2>&1 || (echo ssh-keygen not found: enable the Windows optional feature "OpenSSH Client". & exit /b 1)
where curl >nul 2>&1 || (echo curl not found. & exit /b 1)
if not exist "%VSDIR%" mkdir "%VSDIR%" || exit /b 1

if /i "%~1"=="join" goto join
if /i "%~1"=="renew" goto renew
echo Usage:
echo   %~nx0 join ^<invitation^>   register this computer and get a token
echo   %~nx0 renew                issue yourself a new token
exit /b 1

:join
if "%~2"=="" (echo Usage: %~nx0 join ^<invitation^> & exit /b 1)
if not exist "%KEY%" (
    echo Creating a key in %KEY%
    echo You may protect it with a passphrase or leave the passphrase empty.
    ssh-keygen -q -t ed25519 -C vs-review -f "%KEY%" || exit /b 1
)
curl -s -o "%RESP%" -w "%%{http_code}" -F "code=%~2" -F "pubkey=<%KEY%.pub" "%SERVER%/vs/join" >"%STATUS%"
goto result

:renew
if not exist "%KEY%" (echo No key found. Run: %~nx0 join ^<invitation^> & exit /b 1)
if not exist "%VSDIR%\user.txt" (echo No login found. Run: %~nx0 join ^<invitation^> & exit /b 1)
set /p VS_USER=<"%VSDIR%\user.txt"
curl -s -o "%VSDIR%\challenge.txt" -w "%%{http_code}" -F "user=%VS_USER%" "%SERVER%/vs/challenge" >"%STATUS%"
set "HTTP="
set /p HTTP=<"%STATUS%"
if not "%HTTP%"=="200" (
    if exist "%VSDIR%\challenge.txt" type "%VSDIR%\challenge.txt"
    echo Request failed, HTTP %HTTP%.
    exit /b 1
)
if exist "%VSDIR%\challenge.txt.sig" del "%VSDIR%\challenge.txt.sig"
ssh-keygen -q -Y sign -f "%KEY%" -n vs-review "%VSDIR%\challenge.txt" || (echo Signing failed: OpenSSH 8.1 or newer is required. & exit /b 1)
curl -s -o "%RESP%" -w "%%{http_code}" -F "user=%VS_USER%" -F "challenge=<%VSDIR%\challenge.txt" -F "signature=<%VSDIR%\challenge.txt.sig" "%SERVER%/vs/renew" >"%STATUS%"
del "%VSDIR%\challenge.txt" "%VSDIR%\challenge.txt.sig" 2>nul
goto result

:result
set "HTTP="
set /p HTTP=<"%STATUS%"
if not "%HTTP%"=="200" (
    if exist "%RESP%" type "%RESP%"
    echo Request failed, HTTP %HTTP%.
    if exist "%RESP%" del "%RESP%"
    exit /b 1
)
set "VS_USER="
set "VS_TOKEN="
set "VS_EXPIRES="
for /f "usebackq tokens=1,* delims==" %%a in ("%RESP%") do (
    if "%%a"=="VS_USER" set "VS_USER=%%b"
    if "%%a"=="VS_TOKEN" set "VS_TOKEN=%%b"
    if "%%a"=="VS_EXPIRES" set "VS_EXPIRES=%%b"
)
del "%RESP%"
if not defined VS_TOKEN (echo Unexpected server response. & exit /b 1)
>"%VSDIR%\user.txt" echo(%VS_USER%
>"%VSDIR%\token.txt" echo(%VS_TOKEN%
<nul set /p "=%VS_TOKEN%" | clip
echo.
echo Login:       %VS_USER%
echo Token:       %VSDIR%\token.txt (also copied to the clipboard)
echo Valid until: %VS_EXPIRES%
echo.
echo Open %SERVER%/ , enter the login and paste the token into the Password field.
echo When the token expires, run: %~nx0 renew
exit /b 0
