@echo off
rem The exit codes returned by this script.
set SUCCESS_NO_PENDING=0
set SUCCESS_PENDING=30
set SERVICE_ALREADY_STOPPED=35
set SERVICE_NOT_INSTALLED=40
set SERVICE_NOT_RUNNING=45
set SUSPEND_ALL_FAILURE=50
set SUSPEND_NOW_ALL_FAILURE=55
set STOP_FAILURE=60

rem Get SFN path to install directory.
rem A bug in some versions of Windows makes %~dsp0 append the filename, so use this workaround.
rem Note: correct to append .. instead of \ since %~dsp0 ends with \
for %%i in (%~sf0) do set INSTALL_DIR=%%~dspi..

set SCRIPT_DIR=%INSTALL_DIR%\bin
set MULE_APP=apia

rem Check if IA service is installed.
echo Checking that Integration Agent service (%MULE_APP%) is running...
sc query %MULE_APP% | find "does not exist" >NUL

rem If the search succeeded, then the service is not installed.
IF %ERRORLEVEL% EQU 0 GOTO SERVICE_NOT_INSTALLED

rem Get the IA service's current state.
FOR /F "tokens=3" %%G IN ('sc query %MULE_APP% ^| find "STATE"') DO SET SERVICE_STATE=%%G

rem Nothing to do if the service is already stopped.
IF %SERVICE_STATE% EQU 1 GOTO SERVICE_ALREADY_STOPPED

rem Only continue if the service is running.
IF %SERVICE_STATE% NEQ 4 GOTO SERVICE_NOT_RUNNING

rem Default to SUCCESS_NO_PENDING, which will be updated if there are pending requests.
set SUCCESS_CODE=%SUCCESS_NO_PENDING%

rem Suspend all Integration Services.
echo Suspending all Integration Services...
CALL %SCRIPT_DIR%\iadmin.bat suspend all

rem Notify of failure of suspend all
IF %ERRORLEVEL% EQU 0 GOTO STOP_SERVICE
IF %ERRORLEVEL% EQU 30 GOTO WAIT_FIRST
GOTO SUSPEND_ALL_FAILURE

:WAIT_FIRST
rem Wait 30 seconds, but allow keystroke to interrupt.
echo Waiting 30 seconds to allow pending requests to end.
timeout.exe 30

rem Update success code to indicate that pending requests were interrupted.
set SUCCESS_CODE=%SUCCESS_PENDING%

rem Interrupt pending requests.
echo Interrupting all remaining pending requests...
CALL %SCRIPT_DIR%\iadmin suspend-now all

rem Notify of failure of suspend-now all
IF %ERRORLEVEL% EQU 0 GOTO STOP_SERVICE
IF %ERRORLEVEL% EQU 30 GOTO STOP_SERVICE
GOTO SUSPEND_NOW_ALL_FAILURE

:STOP_SERVICE
echo Stopping Integration Agent service (%MULE_APP%)...
net stop %MULE_APP%

rem Notify of failure to stop service
IF %ERRORLEVEL% NEQ 0 GOTO STOP_FAILURE

echo Successfully shut down Integration Agent service (%MULE_APP%).
EXIT /B %SUCCESS_CODE%

rem *
rem * Error handlers
rem *

:SERVICE_NOT_INSTALLED
echo The Integration Agent service (%MULE_APP%) is not installed.
EXIT /B %SERVICE_NOT_INSTALLED%

:SERVICE_ALREADY_STOPPED
echo The Integration Agent service (%MULE_APP%) is already stopped.
EXIT /B %SERVICE_ALREADY_STOPPED%

:SERVICE_NOT_RUNNING
echo The Integration Agent service (%MULE_APP%) can only be stopped if it is currently running.
EXIT /B %SERVICE_NOT_RUNNING%

:SUSPEND_ALL_FAILURE
echo Failed to issue "suspend all" command via IA Admin CLI.
echo Consult the script output and IA Admin CLI log files for details.
EXIT /B %SUSPEND_ALL_FAILURE%

:SUSPEND_NOW_ALL_FAILURE
echo Failed to issue "suspend-now all" command via IA Admin CLI.
echo Consult the script output and IA Admin CLI log files for details.
EXIT /B %SUSPEND_NOW_ALL_FAILURE%

:STOP_FAILURE
echo Unable to stop Integration Agent service (%MULE_APP%) due to "net stop" error code %ERRORLEVEL%
echo Consult the script output, system event log, or Integration Agent logs for details.
EXIT /B  %STOP_FAILURE%
