@echo on
setlocal enabledelayedexpansion
REM FW_Client version will be %1 passed as a launch argument

REM Adding removal of log files from previous upgrades...the MSI can be unable to be deleted sometimes, but can write over it
del c:\temp\upgradeclient.log
del c:\temp\filewaveclient.log
echo " " > c:\temp\filewaveclient.log

REM remove the old upgrade software if it is still there
REM the fake delimiter is just to get the whole string in the variable
Set Value="nothing"
for /f "delims=;" %%a in ('powershell "&{(Get-Package -Name 'FileWave Custom Client Upgrade (from 10.0.X)').name}"') do Set "Value="%%a""
if %Value% == "FileWave Custom Client Upgrade (from 10.0.X)" (
	call :WriteOut "Uninstalling the FileWave Client upgrader..."
	start /wait msiexec /l*v FileWaveClientUpgraderUninstall.log /qn /norestart /x "{E3DC560D-C698-41DF-8B6C-EEA0BEFC44EF}" > NUL
	call :WriteOut "Done Uninstalling the FileWave Client upgrader, exit code %ERRORLEVEL%"
)

call :WriteOut "Installer version %1"

REM is the version right?  if FW client is not showing in add/remove, it will return no value, so setting a default
set version=none
for /f "delims=;" %%b in ('powershell "&{(Get-Package -Name 'FileWave Client').version}"') do Set "version=%%b"

if %version% EQU %1 (
	call :WriteOut "FileWave version %Version% already installed."
	call :WriteOut "Exit 0 - upgrade complete."
	exit /b 0
) else (
    call :WriteOut "FileWave version %Version% installed. Upgrading..."
)

REM overwrite any previous scheduled task and recreate if needed
schtasks /create /tn "FileWave Client Upgrade" /xml "c:\temp\FileWave Client Upgrade.xml" /F 

REM Confirm scheduled task
schtasks /Query /TN "FileWave Client Upgrade"

if %ERRORLEVEL% equ 0 (
	call :WriteOut "Scheduled task confirmed created."
) else (
	call :WriteOut "Scheduled task not created. Aborting process!"
	exit 1
)

REM We don't want to loop this process more than a few times, so we'll track it with a version-specific tracking file
echo 1 >> %1_loop.txt

REM just count the lines to see how many times we have looped
for /f "delims=;" %%c in ('powershell "&{(Get-Content %1_loop.txt).Length}"') do Set "attempts=%%c"
if %attempts% GEQ 4 (
	call :WriteOut "Retried %attempts% times. Manual investigation recommended"
)

REM the scheduled task should run on its own when it is created, but it doesn't always seem to do so, launching deliberately
schtasks /run /tn "FileWave Client Upgrade" /i
		
REM Stop the fwclient totally so that it does not complete this activation (important for fileset status reporting purposes)
REM Technically the activation script would run twice for a client actually upgrading, always being interupted the first time
call :WriteOut "Stop filewavewinclient service. This will stop the script, status will be 'Activating...' until scheduled task runs and upgrade is done "
sc stop filewavewinclient
REM hang out here for a bit so that activation script 100% can't finish from this loop
call :WriteOut "Wait 180 seconds to let time to stop the service, just in case."
timeout 180 
exit /b 0

::----------------------------------------------------------------------------------------
:WriteOut <Message>
Set "Message=%~1"
echo "%DATE% %TIME% == %Message%" >> upgradeClient.log
exit /b
::----------------------------------------------------------------------------------------
