@echo off

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    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" )
    pushd "%CD%" 
    CD /D "%~dp0"

echo Network Optix Media Server will stop, do you need to continue? [Continue by pressing Y key,stop install by pressing other keys.]
set /p input=
if /i %input%==Y (
	net stop defaultMediaServer
) else (
	exit
)

set analyticsPluginDir="C:\Program Files\Network Optix\Nx Witness\MediaServer\plugins\milesight_analytics_plugin\"
set originPluginPath="C:\Program Files\Network Optix\Nx Witness\MediaServer\plugins\milesight_analytics_plugin.dll"
set originPluginBakPath="C:\Program Files\Network Optix\Nx Witness\MediaServer\plugins_optional"

if exist %analyticsPluginDir% (
	rd /s /q %analyticsPluginDir%
)
if not exist %originPluginPath% (
	goto copyLib
)

echo Detected the original plugin of version 1.0.1.Coexistence may consume performance and storage space.Do you want to delete it? [Delete by pressing Y key, do not delete by pressing other keys.]
set /p input=
if /i %input%==Y (
	move /y %originPluginPath% %originPluginBakPath%
	echo We have moved the original plugin file to %originPluginBakPath%. If you no longer need it, you can delete it.
)

:copyLib
if exist "milesight_analytics_plugin" (
	xcopy /y "milesight_analytics_plugin\*.dll" %analyticsPluginDir%
)
net start defaultMediaServer

pause