@echo Off
if not DEFINED IS_RDC_MINIMIZED set IS_RDC_MINIMIZED=1 && start "TAC: RDC Disconnect" /min "%~dpnx0" %* && exit
echo ========================================
echo ========================================
echo =====                              =====
echo ===== Please minimize this window. =====
echo =====                              =====
echo ========================================
echo ========================================
set AGENT_FOLDER=%~dp0
echo Agent is running at: %AGENT_FOLDER%
%~d0
cd "%AGENT_FOLDER%"
md logs\agent 2> nul

REM Check for local Java in AGENT_FOLDER
set JAVA_CMD=java
if exist "%AGENT_FOLDER%java\bin\java.exe" (
    set JAVA_CMD="%AGENT_FOLDER%java\bin\java.exe"
    echo Using local Java: %JAVA_CMD%
) else (
    echo Using system Java: %JAVA_CMD%
)

set MY_SESSION_ID=unknown
echo ================================ >> logs\agent\rdc-disconnect.log
cd
echo RDC Disconnect executed at: %date%  %time% >> logs\agent\rdc-disconnect.log
query session %username% >> logs\agent\rdc-disconnect.log
for /f "tokens=3-4" %%a in ('query session %username%') do @if "%%b"=="Active" set MY_SESSION_ID=%%a
echo This RDC session is: %MY_SESSION_ID%
echo This RDC session is: %MY_SESSION_ID% >> logs\agent\rdc-disconnect.log
cd tools
set IS_RDC_MINIMIZED=
Timeout 10 && tscon %MY_SESSION_ID% /dest:console & Timeout 2 & %JAVA_CMD% -cp .;DataHandling.j4n.jar;jni4net.j-0.8.8.0.jar ScreenRes >> ..\logs\agent\rdc-disconnect.log
echo ================================ >> ..\logs\agent\rdc-disconnect.log
exit
