REM UPDATE SCRIPT
set "LIBSVC=${component.node.name}"
set "CERTUPD=${SYRACUSE.service.passphraseupdate}"
set "UPDMODE=${UPDATEMODE}"
cd /d "${INSTALL_PATH}${FILE_SEPARATOR}syracuse"

if exist update rmdir /S /Q update\

rem @echo ====BP1========CERTUPD = %CERTUPD% UPDMOD = %UPDMODE% ===srv stop========= >>c:\temp\trace-service.log 
rem Call "${INSTALL_PATH}${FILE_SEPARATOR}syracuse${FILE_SEPARATOR}servicestop.cmd" 
@echo off

rem Syracuse service stop appel par Beforepacks.cmd
rem 

@echo ============== SyracuseService stop ... 
set EXPANDED_SERVICE_NAME=${UNINSTALL_NAME}
set SERVICE_NAME=%EXPANDED_SERVICE_NAME: =_%
@echo ============== SyracuseService stop ...%SERVICE_NAME%  
"${syracuse.dir.agent}\Agent.exe" //SS/Agent_%SERVICE_NAME%

rem @echo ====BP2========CERTUPD = %CERTUPD% UPDMOD = %UPDMODE% ===srv del========= >>c:\temp\trace-service.log 
rem Call "${INSTALL_PATH}${FILE_SEPARATOR}syracuse${FILE_SEPARATOR}servicedelete.cmd" %CERTUPD% %UPDMODE%
@echo off 
rem Syracuse service deletion appel par Beforepacks.cmd 
rem et SyracuseInstall-template.xml
 
rem sleep 3		
ping -n 3 127.0.0.1 > null	
 
if "%UPDMODE%"=="true" (
	IF "%CERTUPD%"=="false" GOTO :NO_UPDATE
)
@echo ============== Syracuse Service deletion ...  

set EXPANDED_SERVICE_NAME=${UNINSTALL_NAME}
set SERVICE_NAME=%EXPANDED_SERVICE_NAME: =_%

"${syracuse.dir.agent}\Agentw.exe" //MQ/Agent_%SERVICE_NAME%
"${syracuse.dir.agent}\Agent.exe" //DS/Agent_%SERVICE_NAME%
"${INSTALL_PATH}${FILE_SEPARATOR}syracuse${FILE_SEPARATOR}Syracuse.exe" //DS/%SERVICE_NAME%
GOTO :FIN_DEL

:NO_UPDATE 
@echo ============== No Syracuse Service deletion ...  

:FIN_DEL


call taskkill /im Agentw.exe /f /t
rem sleep 5		
ping -n 5 127.0.0.1 > null		

rem check if syracuse services are still present		
rem test running syracuse service 		
sc query "Sage_Syracuse_-_%LIBSVC%" | find /i "RUNNING" > res.tmp		
set /p f=<res.tmp		
if "%f%" NEQ "" goto failService		

rem test stopping state syracuse service 		
sc query "Sage_Syracuse_-_%LIBSVC%" | find /i "STOPPING" > res.tmp		
set /p f=<res.tmp		
if "%f%" NEQ "" goto failService		

rem test running agent service 		
sc query "Agent_Sage_Syracuse_-_%LIBSVC%" | find /i "RUNNING" > res.tmp		
set /p f=<res.tmp		
if "%f%" NEQ "" goto failService		

rem test stopping state agent service 		
sc query "Agent_Sage_Syracuse_-_%LIBSVC%" | find /i "STOPPING" > res.tmp		
set /p f=<res.tmp		
if "%f%" EQU "" goto ok		
		
:failService		
rem we still have the service running and test more than 3 times to stop it		
echo "services Sage_Syracuse_-_%LIBSVC% or Agent_Sage_Syracuse_-_%LIBSVC% can't be stopped. please stop the services manually and run again the setup" > %tmp%${FILE_SEPARATOR}exitfail.txt		
exit 1		
	
:ok		
@echo ============== del folders ... 
del /F /Q res.tmp				
rem ===SAM 111711 - delete content of streamline cache folder 		
rem ===list all cache directory should look like that pattern <cacheDir>/<user> and delete the content of that dir 		
for /D %%s in ("${INSTALL_PATH}${FILE_SEPARATOR}syracuse${FILE_SEPARATOR}cache\") do (		
	rem ===delete all file in the cache/user directory		
	del /F /S /Q "%%s\*.*"  > nul 
	rem ===delete all folder under cache/user directory	
    for /f %%b IN ('dir /b /s /a:d "%%s"') DO (
		rmdir /S /Q "%%b" 
		)		
)

cd /d "${INSTALL_PATH}${FILE_SEPARATOR}syracuse"

if exist "SAGE ERP"*.txt del /F /Q "SAGE ERP"*.txt
if exist "SAGE ERP"*.htm del /F /Q "SAGE ERP"*.htm

if not exist update_backup mkdir update_backup

set tddt=%date:~7,2%%date:~4,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%

copy /Y "${syracuse.dir.binpath}${FILE_SEPARATOR}nodelocal.js" "update_backup${FILE_SEPARATOR}nodelocal.js-%tddt%"
copy /Y "${syracuse.dir.binpath}${FILE_SEPARATOR}nodelocal.js" "update_backup${FILE_SEPARATOR}nodelocal.js"

cd /d "${INSTALL_PATH}"
echo Windows Registry Editor Version 5.00 > removereg.reg
echo. >> removereg.reg
echo [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Syracuse - %LIBSVC%] >> removereg.reg
echo. >> removereg.reg

regedit /s removereg.reg

del /F /Q removereg.reg

exit 0