@echo off

:: Set INSTALLDIR
set INSTALLDIR="D:\TWS"

IF NOT EXIST %INSTALLDIR% set INSTALLDIR="%ProgramFiles%\TWS"
IF NOT EXIST %INSTALLDIR% set INSTALLDIR="%ProgramFiles(x86)%\TWS"
IF NOT EXIST %INSTALLDIR% (

	echo TWS install directory not found
	pause
	exit

)


:: Go to the script path

cd /D %~dp0


:: Set Patch Version
set PATCH_VERSION=5.1.2143.108b
::set PATCH_ADDON_VERSION=51XXXX.rXXX

echo -----------------------------------------
echo            PATCH %PATCH_VERSION%
echo -----------------------------------------
echo.
pause

echo.
echo ------------------ APPLYING PATCH ...
echo.

net stop TWS$TWS_BrowserServices 2>NUL
net stop TWS$TWS_AppSharingRouterServices
net stop TWS$TWS_CloudServices
net stop TWS$TWS_ConferenceServices
net stop TWS$TWS_CSTAServices
::net stop TWS$TWS_Database
net stop TWS$TWS_EventServices
net stop TWS$TWS_FlashServices
net stop TWS$TWS_GenericServices
net stop TWS$TWS_MediaServices
net stop TWS$TWS_MediaSIPServices
net stop TWS$TWS_PJSIPServices
net stop TWS$TWS_UCCServices
net stop TWS$TWS_ScriptServices
net stop TWS$TWS_ToolkitWebServices
net stop TWS$TWS_VTIXMLServices
net stop TWS$TWS_WebAPIServices
net stop TWS$TWS_WebServices

taskkill /F /IM TWS_BrowserServices.exe 2>NUL
taskkill /F /IM TWS_AppSharingRouterServices.exe 2>NUL
taskkill /F /IM TWS_CloudServices.exe 2>NUL
taskkill /F /IM TWS_ConferenceServices.exe 2>NUL
taskkill /F /IM TWS_CSTAServices.exe 2>NUL
taskkill /F /IM TWS_EventServices.exe 2>NUL
taskkill /F /IM TWS_FlashServices.exe 2>NUL
taskkill /F /IM TWS_GenericServices.exe 2>NUL
taskkill /F /IM TWS_MediaServices.exe 2>NUL
taskkill /F /IM TWS_MediaSIPServices.exe 2>NUL
taskkill /F /IM TWS_PJSIPServices.exe 2>NUL
taskkill /F /IM TWS_UCCServices.exe 2>NUL
taskkill /F /IM TWS_ScriptServices.exe 2>NUL
taskkill /F /IM TWS_ToolkitWebServices.exe 2>NUL
taskkill /F /IM TWS_VTIXMLServices.exe 2>NUL
taskkill /F /IM TWS_WebAPIServices.exe 2>NUL
taskkill /F /IM TWS_WebServices.exe 2>NUL

del /F /Q %INSTALLDIR%\TWS_Services\TWS_GenericServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_EventServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_FlashServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_WebAPIServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_WebServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_ToolkitWebServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_CloudServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_CSTAServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_VTIXMLServices\*.dll

:: Copy files
xcopy "%CD%\data\*" %INSTALLDIR%\ /S /Y /Q
::xcopy "%CD%\tools\*" %INSTALLDIR%\TWS_Common\ /S /Y /Q

::Change version
ChangeVersion XmlServer %INSTALLDIR% %PATCH_VERSION%
::ChangeVersion XmlAddon %INSTALLDIR% %PATCH_ADDON_VERSION%

::CorFlags.exe /32BITREQ+ %INSTALLDIR%\TWS_Services\TWS_VTIXMLServices\TWS_VTIXMLServices.exe

iisreset

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

net start TWS$TWS_GenericServices


echo.
echo ------------------ PATCH APPLIED
echo.

pause
