@echo off

cd /d %~dp0
set curpath=%cd%

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

set p=%curpath%\RaySyncServer.exe
if exist "%p%" (
	netsh advfirewall firewall delete rule name="RaySyncServer" >NUL
	netsh advfirewall firewall add rule name="RaySyncServer" dir=in action=allow program="%p%" >NUL
	echo RaySyncServer added to the firewall.
)

set q=%curpath%\proxy\RaySync-Proxy-Server.exe
if exist "%q%" (
	netsh advfirewall firewall delete rule name="RaySync-Proxy-Server" >NUL
	netsh advfirewall firewall add rule name="RaySync-Proxy-Server" dir=in action=allow program="%q%" >NUL
	echo RaySync-Proxy-Server added to the firewall.
)

set m=RaySyncServer.exe
for /f %%i in ('tasklist') do (
	if %%i=="%m%" echo RaySync has already been running, don't need to start again. & pause & exit
)
echo RaySync is starting.
"%p%"