REM UPDATE SCRIPT
rem prepare variable to test filesize
set file=%temp%\res.tmp
set /A size=1
set /A sizenull=0


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

if exist update rmdir /S /Q update\

Call "${INSTALL_PATH}${FILE_SEPARATOR}syracuse${FILE_SEPARATOR}servicestop.cmd"  >nul 2>&1 
Call "${INSTALL_PATH}${FILE_SEPARATOR}syracuse${FILE_SEPARATOR}servicedelete.cmd"  >nul 2>&1 
call taskkill /im Agentw.exe /f /t  >nul 2>&1 

for /L %%n in (1,1,5) do {
	if %size% GTR %sizenull% {
		rem wait 5s more....
		rem sleep 5		
		ping -n 5 127.0.0.1 > nul		

		rem check if syracuse services are still present		
		rem test running syracuse service 		
		sc query "${syracuse.service.name}" | find /i "RUNNING" > %file%			
		FOR /F "usebackq" %%A IN ('%file%') DO set size1=%%~zA
				
		rem test stopping state syracuse service 		
		sc query "${syracuse.service.name}" | find /i "STOP" > %file%		
		FOR /F "usebackq" %%A IN ('%file%') DO set size2=%%~zA
			
		rem test running agent service 		
		sc query "${agent.service.name}" | find /i "RUNNING" > %file%			
		FOR /F "usebackq" %%A IN ('%file%') DO set size3=%%~zA
				
		rem test stopping state agent service 		
		sc query "${agent.service.name}" | find /i "STOP" > %file%			
		FOR /F "usebackq" %%A IN ('%file%') DO set size4=%%~zA
			
		set /A  size = %size1%+%size2%+%size3%+%size4%		
	}
}

if %size% GTR %sizenull% {
	rem we still have the service running and test more than 3 times to stop it		
	echo "services ${syracuse.service.name} or ${agent.service.name} can't be stopped. We kill them manually ...%size%..." > %tmp%${FILE_SEPARATOR}exitfail.txt
	taskkill /f /fi "Services eq  ${syracuse.service.name}" 
	taskkill /f /fi "Services eq  ${agent.service.name}"		
}

if exist "SAGE ERP"*.tmp 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

cd /d "${INSTALL_PATH}${FILE_SEPARATOR}syracuse"
if not exist update_backup mkdir update_backup

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)

copy /Y "${syracuse.dir.binpath}${FILE_SEPARATOR}nodelocal.js" "update_backup${FILE_SEPARATOR}nodelocal.js.%mydate%_%mytime%"  >nul 2>&1 
copy /Y "${syracuse.dir.binpath}${FILE_SEPARATOR}nodelocal.js" "update_backup${FILE_SEPARATOR}nodelocal.js"  >nul 2>&1 
cd /d "${INSTALL_PATH}"

echo Windows Registry Editor Version 5.00 > %tmp%${FILE_SEPARATOR}removereg.reg
echo. >> %tmp%${FILE_SEPARATOR}removereg.reg
echo [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Syracuse - ${component.node.name}] >> %tmp%${FILE_SEPARATOR}removereg.reg
echo [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Sage Syracuse - ${component.node.name}] >> %tmp%${FILE_SEPARATOR}removereg.reg
echo [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Sage EM Syracuse Server - ${component.node.name}] >> %tmp%${FILE_SEPARATOR}removereg.reg
echo. >> %tmp%${FILE_SEPARATOR}removereg.reg

regedit /s %tmp%${FILE_SEPARATOR}removereg.reg

del /F /Q %tmp%${FILE_SEPARATOR}removereg.reg

exit 0