@echo off

title RFBD install batch script
echo ---------------------------------------
echo "|  __ \     /\   | \ | |  ____/ ____|"
echo "| |__) |   /  \  |  \| | |__ | (___  "
echo "|  _  /   / /\ \ | . ` |  __| \___ \ "
echo "| | \ \  / ____ \| |\  | |    ____) |"
echo "|_|  \_\/_/    \_\_| \_|_|   |_____/ "
echo ---------------------------------------

echo Check permissions ....
rd "%WinDir%\system32\test_permissions" >NUL 2>NUL
md "%WinDir%\System32\test_permissions" 2>NUL
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Request admin rights...
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B

:gotAdmin
rd "%WinDir%\System32\test_permissions" 2>NUL

cd /d %~dp0
echo Uninstall service ....
call uninstall.bat /b >NUL 2>NUL

echo Enable Firewall ...
for /f "tokens=1,2* delims==" %%i in ( 'rfbd -q -p' ) do if "%%i"=="nbd_server_port" set nbd_server_port=%%j >NUL
for /f "tokens=1,2* delims==" %%i in ( 'rfbd -q -p' ) do if "%%i"=="http_server_port" set http_server_port=%%j >NUL
netsh firewall set portopening protocol=TCP port=%nbd_server_port% name=RFBD-NBD mode=ENABLE scope=SUBNET profile=CURRENT >NUL
netsh firewall set portopening protocol=TCP port=%http_server_port% name=RFBD-HTTP mode=ENABLE scope=SUBNET profile=CURRENT >NUL
echo Install rfbd service ...
rfbd -i
net start rfbd

pause

