@echo off
setlocal

set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\BMC Software\Control-M/Server\SYSPRM
set VALUE_NAME=CONTROLM_SERVER

for /F "tokens=3*" %%A IN ('reg query "%KEY_NAME%" /reg:64 /v %VALUE_NAME%') do (
  set CTMSERVER=%%A %%B
)

set SCRIPT_DIR=%~dp0
if exist "%SCRIPT_DIR%ctmsaasexport.py" (
    set PY_SCRIPT=%SCRIPT_DIR%ctmsaasexport.py
) else (
    set PY_SCRIPT=%CTMSERVER%\scripts\ctmsaasexport.py
)

"%CTMSERVER%\scripts\run_python.bat" "%PY_SCRIPT%" %*
exit /b %errorlevel%
