@echo off
if !%1==! goto help
if !%2==! goto help

set BESZELKEYPREFIX=%1
set BESZELKEY=%2
set SERVICENAME=BeszelAgent
set DESTINATIONDIR=%ProgramFiles%\%SERVICENAME%
set AGENTEXE=%DESTINATIONDIR%\agent.exe

winget install nssm
rem Refresh the environment to start nssm from %PATH%
rem Call RefreshEnv.cmd from Chocolatey:
call .\RefreshEnv.cmd

md "%DESTINATIONDIR%"
copy .\agent.exe "%DESTINATIONDIR%\"
netsh advfirewall firewall add rule name="%SERVICENAME%" dir=in action=allow program="%AGENTEXE%" enable=yes
nssm install %SERVICENAME% "%AGENTEXE%"
nssm set %SERVICENAME% AppEnvironmentExtra "KEY=%BESZELKEYPREFIX% %BESZELKEY%"
nssm start %SERVICENAME%
nssm dump %SERVICENAME%
goto end

:help
echo You have to provide a Beszel ssh key, e.g. 'install-beszel-service.cmd ssh-ed25519 AAAAC3L3lIpn94iu2TIpdmneXsEC1TDvrg4HCNoM1aiC683WMZfGOVoatrJSkysaQB9K

:end