@echo off
setlocal EnableDelayedExpansion

rem HOW TO RUN:
rem   1) cd to run_saas_migration_import.bat script location
rem   2) interactive mode:     run_saas_migration_import.bat
rem   3) non-interactive mode: run_saas_migration_import.bat --tenant_aapi_url=<mandatory> --aapi_token=<mandatory> --export_file_location=<mandatory> [optional flags]
rem
rem --export_file_location must be a package TAR bundling an EM export and
rem zero-or-more CTM (Control-M/Server) exports:
rem   em/<any-name>.tar.gz
rem   servers/<ctmName>/<any-name>.tar.gz
rem
rem Single-CTM variants:
rem   --ctm_server=<name> uses the per-server TAR at servers/<name>/*.tar.gz
rem                       inside the package TAR (EM is skipped).
rem   --em_ctm=<name>     uses ONLY the inner EM TAR scoped to one
rem                       Control-M/Server (no servers/<name>/*.tar.gz required).

rem Check if running interactively
set "args=%*"
if "!args!"=="" (
    set "interactive=true"
) else (
    set "interactive=false"
)

set "current_dir=%~dp0"
set "python_script_path=%current_dir%scripts\saas_migration_import.py"
set "logs_dir=%current_dir%logs"


if "!interactive!"=="false" (
    echo Non-interactive mode
    rem Non-interactive session
    set "python_exe=python3"
    set "python_script_args=!args! --logs_dir="!logs_dir!""
    set "is_py_exe_flag_found=false"

    for %%a in (!args!) do (
        set "arg=%%a"
        if "!arg:~0,12!"=="--python_exe" (
            set "is_py_exe_flag_found=true"
        )
        if "!is_py_exe_flag_found!"=="true" (
            set "python_exe=!arg!"
        )
    )

    echo running: "!python_exe!" "!python_script_path!" !python_script_args!
    "!python_exe!" "!python_script_path!" !python_script_args!
    exit /b 0
)


rem Read user inputs
set /p "tenant_aapi_url=Type your Control-M SaaS Automation API endpoint: "
if not defined tenant_aapi_url (
    echo Control-M SaaS Automation API Endpoint cannot be empty.
    exit /b 1
)

set /p "aapi_token=Type your Control-M Automation API token: "
if not defined aapi_token (
    echo Control-M Automation API Token cannot be empty.
    exit /b 1
)

set /p "export_file_location=Type the full path and filename of the compressed export file: "
if not defined export_file_location (
    echo File path and filename cannot be empty.
    exit /b 1
)

rem Check if em bmcpython is available by checking output contains "Python"
set "python_exe="
for /f "tokens=*" %%i in ('em bmcpython --version 2^>^&1') do (
    echo %%i | findstr /i "Python" >nul && set "python_exe=em bmcpython"
)

if defined python_exe (
    echo Using em bmcpython as Python interpreter.
) else (
    set /p "custom_python_exe=Type the name of your Python interpreter (press ENTER to accept python3 as default): "
    if defined custom_python_exe (
        set "python_exe=!custom_python_exe!"
    ) else (
        set "python_exe=python3"
    )
)

rem Execute the python script
echo running: !python_exe! "!python_script_path!" --tenant_aapi_url !tenant_aapi_url! --aapi_token !aapi_token! --export_file_location "!export_file_location!"
!python_exe! "!python_script_path!" --tenant_aapi_url !tenant_aapi_url! --aapi_token !aapi_token! --export_file_location "!export_file_location!"

exit /b 0
