@echo off
ver|findstr "5\.[0-9]\.[0-9][0-9]*" > nul && (goto init)
ver|findstr "\<6\.[0-9]\.[0-9][0-9]*\> \<10\.[0-9]\.[0-9][0-9]*\>" > nul && (goto admin)

:admin
rem 申请管理员权限
%1 %2 mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :init","","runas",1)(window.close)&goto :eof

:init
title 雨禾传媒 Server v3 群控系统
color 80
echo.
echo.

rem 判断系统位宽
if /i %PROCESSOR_IDENTIFIER:~0,3%==x86 (
	goto x86
) else (
	goto x64
)
exit

:x64
set RserverPath=%windir%\SysWOW64\rserver30
set RsetupPath=%RserverPath%\rsetup64.exe
set MirrorPath=x64
goto Run
exit

:x86
set RserverPath=%windir%\System32\rserver30
set RsetupPath=%RserverPath%\rsetup.exe
set MirrorPath=x86
goto Run

:Run
rem 生成防火墙白名单规则,兼容XP和2003系统
set FirewallAdd64=netsh advfirewall firewall add rule name="Radmin Server 3" dir=in action=allow program="%RserverPath%\rserver3.exe" enable=yes profile=any
set FirewallDel64=netsh advfirewall firewall delete rule name="Radmin Server 3"
set FirewallAdd86=netsh firewall add allowedprogram program="%RserverPath%\rserver3.exe" name="Radmin Server 3" ENABLE scope=ALL profile=ALL
set FirewallDel86=netsh firewall delete allowedprogram program="%RserverPath%\rserver3.exe" profile=ALL

ver|findstr "5\.[0-9]\.[0-9][0-9]*" > nul && (set FirewallAdd=%FirewallAdd86% & set FirewallDel=%FirewallDel86% & set is32=)  
ver|findstr "\<6\.[0-9]\.[0-9][0-9]*\> \<10\.[0-9]\.[0-9][0-9]*\>" > nul && (set FirewallAdd=%FirewallAdd64% & set FirewallDel=%FirewallDel64% & set is32=/reg:32)

%~d0
cd "%~dp0"

if NOT exist %RserverPath%\rserver3.exe (goto CopyRadmin35)
echo 卸载已安装的 雨禾传媒 Server v3 群控系统 ...
%RsetupPath% /stop
%RsetupPath% /intuninstall
echo 删除防火墙规则.
%FirewallDel%
echo 删除 雨禾传媒 Server v3 群控系统 的注册表配置信息.
reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Radmin /f %is32%
echo 卸载并清空目录
START /B /WAIT rundll32 %RserverPath%\wsock32.dll,ntsclean
rd /s /q %RserverPath%

:CopyRadmin35
echo 拷贝当前版本的文件到系统目录
if not exist "%RserverPath%" (mkdir %RserverPath%)
copy rserver30\*.* %RserverPath%
copy reg\*.* %RserverPath%
copy 卸载.bat %RserverPath%
copy %MirrorPath%\*.* %RserverPath%

echo 增加到防火墙白名单...
%FirewallAdd%

echo 安装 Radmin Server 服务和虚拟显卡驱动...
%RsetupPath% /intsetup

echo 导入注册表设置...
reg import %RserverPath%\install.reg %is32%
sc failure RServer3 reset= 0 actions= restart/3000/restart/3000/restart/3000

echo 启动 Radmin Server 服务...
%RsetupPath% /start

echo 注册破解补丁
rundll32 "%RserverPath%\wsock32.dll",ntskd noreboot


rem 清理多余文件
del /f /q %RserverPath%\install.reg
