#!/bin/sh

# HOW TO RUN:
# 1) cd to run_saas_migration_import.sh script location
# 2) interactive mode: sh run_saas_migration_import.sh
# 3) non-interactive mode: sh run_saas_migration_import.sh --tenant_aapi_url=<mandatory_value> --aapi_token=<mandatory_value> --export_file_location=<mandatory_value> --python_exe=<optional_value> (optional: --skip_archive_rules=true) (optional: --skip_push_definition=true) (optional: --skip_ctm_migration=true) (optional: --ctm_server=<name>) (optional: --em_ctm=<name>)
#
# --export_file_location must be a package TAR bundling an EM export and
# zero-or-more CTM (Control-M/Server) exports. Only the folder layout is
# constant; the inner TAR file names may be anything:
#   em/<any-name>.tar.gz
#   servers/<ctmName>/<any-name>.tar.gz
# The legacy EM-only export TAR (top-level EMExportForSaaS/ directory) is not
# supported. Use --skip_ctm_migration=true to run only the EM portion.
#
# Single-CTM variants:
#   --ctm_server=<name> uses the per-server TAR at servers/<name>/*.tar.gz
#                       inside the package TAR (EM is skipped).
#   --em_ctm=<name>     uses ONLY the inner EM TAR scoped to one
#                       Control-M/Server (no servers/<name>/*.tar.gz required).

# Check if running interactively
if [ -z "$1" ]; then
    interactive=true
else
    interactive=false
fi

current_dir=$(cd "$(dirname "$0")" && pwd)
python_script_path="$current_dir/scripts/saas_migration_import.py"
logs_dir="--logs_dir=$current_dir/logs"

# Prompt the user for input
if [ "$interactive" = true ]; then
    echo "Type your Control-M SaaS Automation API endpoint: "
    read tenant_aapi_url
    tenant_aapi_url="--tenant_aapi_url=$tenant_aapi_url"
    if [ -z "$tenant_aapi_url" ]; then
        echo "Control-M SaaS Automation API Endpoint cannot be empty."
        exit 1
    fi

    echo "Type your Control-M Automation API token: "
    read aapi_token
    aapi_token="--aapi_token=$aapi_token"
    if [ -z "$aapi_token" ]; then
        echo "Control-M Automation API Token cannot be empty."
        exit 1
    fi

    echo "Type the full path and filename of the compressed export file: "
    read export_file_location
    export_file_location="--export_file_location=$export_file_location"
    if [ -z "$export_file_location" ]; then
        echo "File path and filename cannot be empty."
        exit 1
    fi

    # Set default python em bmcpython if EM_HOME is set, otherwise ask for python path or use defult use python3
    if command em bmcpython --version &>/dev/null; then
        python_exe="em bmcpython"
    else
        echo "Type the name of your Python interpreter (press ENTER to accept python3 as the default): "
        read custom_python_exe
        python_exe="${custom_python_exe:-python3}"
    fi

    echo running: $python_exe "$python_script_path" "$tenant_aapi_url" "$aapi_token" "$export_file_location" "$logs_dir"

    # Trigger the Import script with arguments
    $python_exe "$python_script_path" "$tenant_aapi_url" "$aapi_token" "$export_file_location" "$logs_dir"
else
    # Non-interactive mode (provide default or use command-line arguments)
    if command em bmcpython --version &>/dev/null; then
        python_exe="em bmcpython"
    else
	    python_exe="python3"
    fi
    python_script_args="$@"
    concatenated_args=""

    # Iterate through the input arguments
    first_iteration=true
    for arg in "$@"; do
      if [ "${arg%%=*}" = "--python_exe" ]; then
          python_exe="${arg#*=}"
      else
          if [ "$first_iteration" = true ]; then
              concatenated_args="$arg"
              first_iteration=false
          else
              concatenated_args="$concatenated_args $arg"
          fi
      fi
    done

    echo
    concatenated_args="$concatenated_args $logs_dir"
    echo running: $python_exe "$python_script_path" $concatenated_args

    # Trigger the Import script with arguments
    $python_exe "$python_script_path" $concatenated_args
fi
