@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
echo "%p%"
if exist "%p%" (
	netsh advfirewall firewall delete rule name="RaySyncServer" >NUL
	echo RaySyncServer delete to the firewall.
)

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

set q=RaySync-Proxy-Server.exe
for /f %%i in ('tasklist') do (
	if %%i==%q% echo RaySync-Proxy-Server is stopping. & taskkill /F /IM RaySync-Proxy-Server.exe >nul 1>nul 2>nul 
)

set p=RaySyncServer.exe
for /f %%i in ('tasklist') do (
	if %%i==%p% echo RaySyncServer is stopping. & taskkill /F /IM RaySyncServer.exe >nul 1>nul 2>nul & call status.bat
)

echo "### call stop.bat @ %date% %time% ###" >> log\stop.log

echo RaySyncServer is not running, don't need to stop.

pause
