@echo off

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

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

	echo TWS4 install directory not found
	pause
	exit

)


:: Go to the script path

cd /D %~dp0


:: Set Patch Version
set PATCH_VERSION=4.3.1821.105e
set PATCH_ADDON_VERSION=431950.r1439

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

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

net stop TWS4$TWS_BrowserServices 2>NUL
net stop TWS4$TWS_AppSharingRouterServices
net stop TWS4$TWS_ConferenceServices
net stop TWS4$TWS_CSTAServices
::net stop TWS4$TWS_Database
net stop TWS4$TWS_EventServices
net stop TWS4$TWS_FlashServices
net stop TWS4$TWS_GenericServices
net stop TWS4$TWS_MediaServices
net stop TWS4$TWS_ScriptServices
net stop TWS4$TWS_ToolkitWebServices
net stop TWS4$TWS_VTIXMLServices
net stop TWS4$TWS_WebServices

taskkill /F /IM TWP_BrowserServices.exe 2>NUL
taskkill /F /IM TWS_AppSharingRouterServices.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_ScriptServices.exe 2>NUL
taskkill /F /IM TWS_ToolkitWebServices.exe 2>NUL
taskkill /F /IM TWS_VTIXMLServices.exe 2>NUL
taskkill /F /IM TWS_WebServices.exe 2>NUL

IF EXIST "%CD%\data\TWS_Web\TWS_Applications\Launcher" del /q %INSTALLDIR%\TWS_Web\TWS_Applications\Launcher\*.msi
IF EXIST "%CD%\data\TWS_Web\TWS_Applications\Launcher" del /q %INSTALLDIR%\TWS_Web\TWS_Applications\Launcher\*.exe

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_WebServices\*.dll
del /F /Q %INSTALLDIR%\TWS_Services\TWS_ToolkitWebServices\*.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 TWS4$TWS_GenericServices


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

pause
