@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" )

cd /d %SystemDrive%\Users\%username%\AppData\Local\Temp
dir /b /a:d /s "_MEI*" >nul 1>temp.txt 2>nul
for /f "delims=" %%s in (temp.txt) do (
	rd /q /s "%%s" >nul 1>nul 2>nul
)
del /f/s/q temp.txt >nul 1>nul 2>nul
cd /d %~dp0
set curpath=%cd%

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

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 p5=%curpath%\RayFileServer.exe
if exist "%p5%" (
	netsh advfirewall firewall delete rule name="RayFileServer" >NUL
	netsh advfirewall firewall add rule name="RayFileServer" dir=in action=allow program="%p5%" >NUL
	echo RayFileServer 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 m1=monitor.exe
for /f %%i in ('tasklist') do (
	if %%i==%m1% echo monitor has already been running, don't need to start again. & pause & exit
)
echo monitor is starting.
mshta vbscript:CreateObject("WScript.Shell").Run("monitor",0)(window.close)


REM set pwd_pan_ret=%~d0
REM set pwd_ret=%cd%
REM set startMenuFolder_pan=%SystemDrive%
REM set startMenuFolder=%SystemDrive%\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
REM echo %pwd_pan_ret% >> Raysync.bat
REM echo cd %pwd_ret% >> Raysync.bat
REM echo %pwd_ret%\start.bat >> Raysync.bat
REM move /Y "%pwd_ret%\Raysync.bat" "%startMenuFolder%\Raysync.bat" >nul 1>nul 2>nul & exit


