@echo on
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"

Powershell.exe -Executionpolicy remotesigned -File  "${BEFORE_UPDATE_SCRIPT_PS_PATH}" -InstallPath  "${INSTALL_PATH}"

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 

if exist update rmdir /S /Q update\

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)
			
		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 "service ${syracuse.service.name} can't be stopped. We kill them manually ...%size%..." > %tmp%${FILE_SEPARATOR}exitfail.txt
	taskkill /f /fi "Services eq  ${syracuse.service.name}" 
)

if exist "SAGE ERP"*.tmp del /F /Q res.tmp				
rem 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 

del *.cmd
del Syr*.exe
rmdir /S /Q "agent\"


setlocal enabledelayedexpansion

cd /d "${INSTALL_PATH}${FILE_SEPARATOR}syracuse"
rem try to move the bin folder to bin2 if it works we remove the bin2 folder else we raise an error 

for  %%v in ( 10 9 8 7 6 5 4 3 2 1) do (
  move "${syracuse.dir.binpath}"  "${syracuse.dir.binpath}2" 
  if !ERRORLEVEL! equ 0 goto :STEP1
  echo wait 5s more : service starts
  ping -n 5 127.0.0.1 >nul
)
echo "can't move ${syracuse.dir.binpath} directory, please check if a process or syracuse services are still running or use this directory, and run again the setup" > %tmp%${FILE_SEPARATOR}exitfail.txt
exit /b 1


:STEP1
rem Waiting 5s before verify bin deleted 
for  %%v in (20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1) do (
	if not exist "${syracuse.dir.binpath}" goto :DELBIN
	echo wait 5s more : service starts
	ping -n 20 127.0.0.1 >nul
)
move "${syracuse.dir.binpath}2\*" "${syracuse.dir.binpath}\"
echo "can't delete ${syracuse.dir.binpath} directory, please check if a process or syracuse services are still running or use this directory, and run again the setup" > %tmp%${FILE_SEPARATOR}exitfail.txt
exit /b 2

:DELBIN
rem else
mkdir vide
rem rmdir /S /Q "${syracuse.dir.binpath}2\"
robocopy /mir vide  "${syracuse.dir.binpath}2" >nul 2>&1
rmdir /S /Q "${syracuse.dir.binpath}2\"
rmdir /S /Q vide
for  %%v in (20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1) do (
	if not exist "${syracuse.dir.binpath}2" goto :DELBIN2
	echo wait 5s more : service starts
	ping -n 5 127.0.0.1 >nul
)
echo "can't delete ${syracuse.dir.binpath}2 directory, please check if a process or syracuse services are still running or use this directory, and run again the setup" > %tmp%${FILE_SEPARATOR}exitfail.txt
exit /b 3

:DELBIN2
rem normal behavior exit 0
exit 0



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


