@echo off

rem
rem Setup.
rem ----------------------------------------------------------------------------

set originalDir=%cd%
set stationManagerExe=Qlogic.StationManager.exe


rem
rem Abort if already running new suite.
rem ----------------------------------------------------------------------------

echo Checking if new suite is running...
tasklist /fi "ImageName eq %stationManagerExe%" /fo csv 2>NUL | find /i "%stationManagerExe%">NUL
if errorlevel 1 (
    echo Proceeding since new suite is not running...
) else (
    echo Aborting because new suite is running...
    goto Cleanup
)


rem
rem Stop old package's processes
rem ----------------------------------------------------------------------------

rem Kill processes that are launched on login.
taskkill /f /im QAeroPrep.exe
taskkill /f /im UACheck.exe

rem Kill InfoConnect's main executable, causing GoUAL.exe's cleanup to run.
taskkill /f /im accmgr32.exe

rem Pause 10 seconds, allowing GoUAL's cleanup to proceed.
if errorlevel 1 (
    taskkill /f /im goual.exe
) else (
    echo Stopping InfoConnect's main process...
    ping 127.0.0.1 -n 11 > nul
)

rem Kill any processes related to InfoConnect that might still be running.
echo Stopping other processes related to InfoConnect...
taskkill /f /im guarddog.exe
taskkill /f /im ptrstray.exe
taskkill /f /im pt32.exe
taskkill /f /im accmgr32.exe
taskkill /f /im accsmngr.exe
taskkill /f /im spltsrvc.exe
taskkill /f /im hllwatch.exe
taskkill /f /im passport.exe
taskkill /f /im co-msr.exe
taskkill /f /im cokeys.exe
taskkill /f /im cokeys2.exe
taskkill /f /im goual.exe

rem Kill apps that are automatically launched.
echo Stopping other apps...
taskkill /f /im PRSAttachmateHelper.exe
taskkill /f /im PassportReaderCommonUse.exe
taskkill /f /im UASRDEVU.exe
taskkill /f /im QAeroPrep.exe
taskkill /f /im UACheck.exe

rem Kill any other apps that could be running.
taskkill /f /im Aero.exe
taskkill /f /im AirportApps.exe
taskkill /f /im GateReader.exe
taskkill /f /im UAGRDEVU.exe


rem
rem Free up disk space.
rem ----------------------------------------------------------------------------

rem Remove data in old suite that can be safely deleted.
rmdir /s /q C:\Users\UA\AERO
rmdir /s /q C:\Users\UA\AeroCaches\AeroRollbackCache


rem
rem Start new suite in another console instance.
rem ----------------------------------------------------------------------------

echo Starting UA-Suite in another window...
cd /d %cusappfilepath%UA
start startua.cmd


rem
rem Cleanup.
rem ----------------------------------------------------------------------------

:Cleanup
cd /d %originalDir%
