@ECHO OFF 
CD "%0\.." 
SET SPAWNING_BAT_DIR=%CD% 
CD /D "C:\Program Files\ESNR OBM\aua\bin" 
@ECHO OFF

IF %USERNAME%==SYSTEM (
  EXIT
)

REM ###############################################
REM ## DEFINING LOCATION VAR
SET AUA_BIN_HOME=%CD%
CD ..
SET AUA_HOME=%CD%
CD ..
SET APP_HOME=%CD%
SET APP_BIN_HOME=%APP_HOME%\bin
SET APP_UTIL_BIN_HOME=%APP_HOME%\util\bin
REM ###############################################
REM SPAWN ANOTHER FILE FOR THE PROCESS

IF "%1" == "run" GOTO RUN
IF "%1" == "" GOTO SPAWN

:SPAWN
SET BATNAME=%0
SET APP_USER_HOME_DIR=%USERPROFILE%\.obm
SET SPAWNING_BAT_DIR=%APP_USER_HOME_DIR%\aua\bin
SET SPAWNING_BAT_FILENAME=spawning.bat
SET SPAWNING_BAT_FILEPATH=%SPAWNING_BAT_DIR%\%SPAWNING_BAT_FILENAME%

IF NOT EXIST "%SPAWNING_BAT_DIR%" (
  MD "%SPAWNING_BAT_DIR%"
)

CD "%AUA_BIN_HOME%"

REM /// ... Combine these things in the new file "spawning.bat".
ECHO @ECHO OFF > "%SPAWNING_BAT_FILEPATH%"
ECHO CD "%%0\.." >> "%SPAWNING_BAT_FILEPATH%"
ECHO SET SPAWNING_BAT_DIR=%%CD%% >> "%SPAWNING_BAT_FILEPATH%"
ECHO CD /D "%AUA_BIN_HOME%" >> "%SPAWNING_BAT_FILEPATH%"
TYPE "%BATNAME%" >> "%SPAWNING_BAT_FILEPATH%"

REM COPY %BATNAME% spawning.bat
CD /D "%SPAWNING_BAT_DIR%"
START /MIN .\%SPAWNING_BAT_FILENAME% run
EXIT

:RUN
REM ###############################################
REM ## DEFINING OTHER VAR
REM # Max sleep time is 10 mins
SET SLEEP_TIME=5
SET FLAG_PATH=..\ipc\SystemTray
SET FLAG_NAME=shutdown
SET MSG_FINISH=Restart SystemTray now
SET COUNT=0
SET COUNT_MAX=120
REM ###############################################
REM ## DEFAULT WORKING DIR
CD "%AUA_BIN_HOME%"
REM ###############################################

REM /// ... REASON: "Sleep.exe" has been moved to other location.
.\Sleep.exe 5
REM /// ... "cd" back to default working directory.
CD "%AUA_BIN_HOME%"

:WAIT_FLAG
SET /A COUNT=COUNT + 1
REM /// ... REASON: "Sleep.exe" has been moved to other location.
.\Sleep.exe %SLEEP_TIME%
CD "%AUA_BIN_HOME%"
IF %COUNT%==%COUNT_MAX% GOTO STARTUP_SYSTEMTRAY
IF EXIST %FLAG_PATH%\%FLAG_NAME% GOTO WAIT_FLAG ELSE GOTO STARTUP_SYSTEMTRAY

:STARTUP_SYSTEMTRAY
REM /// ... REASON: DIFFERERT CPU ARCHITECTURE HAVE DIFFERENT "SystemTray*.exe" to run.
CD "%APP_BIN_HOME%"
IF %PROCESSOR_ARCHITECTURE%==AMD64 (
  START SystemTray64.exe
  GOTO INSTALLDRIVERS
) ELSE (
  IF %PROCESSOR_ARCHITECTURE%==x64 (
    START SystemTray64.exe
    GOTO INSTALLDRIVERS
  ) ELSE (
      START SystemTray.exe
  )
)
EXIT

REM Only install in Windows 2008 R2, Windows 2012, Windows 2012 R2
:INSTALLDRIVERS
net accounts | find /i "WORKSTATION" > nul
 
if errorlevel = 1 goto next
if errorlevel = 0 goto :eof
 
:next
ver | find "6.1"
if errorlevel = 1 goto next1
if errorlevel = 0 goto install
 
:next1
ver | find "6.2" > nul
if errorlevel = 1 goto next2
if errorlevel = 0 goto install
 
:next2
ver | find "6.3" > nul
if errorlevel = 1 goto :eof
if errorlevel = 0 goto install
 
:install
CMD /C InstallFSRedirector.bat
CMD /C InstallCBTFilter.bat

EXIT

@ECHO ON