#!/bin/sh

# HOW TO RUN:
# 1) Interactive mode: sh run_archive_migration_import.sh
# 2) Non-interactive mode: sh run_archive_migration_import.sh --tenant_aapi_url=<value> --aapi_token="<value>" --export_files_folder=<value> --python_exe=<optional>
# Example: sh run_archive_migration_import.sh --tenant_aapi_url=https://example.com --aapi_token="TOKEN123==" --export_files_folder=/tmp/tars
# NOTE: Quote aapi_token values to preserve special characters

current_dir=$(cd "$(dirname "$0")" && pwd)
python_script_path="$current_dir/scripts/archive_import.py"

# Parse command line arguments (supports both --param=value and --param value formats)
while [ $# -gt 0 ]; do
    case "$1" in
        --tenant_aapi_url=*)
            tenant_aapi_url="${1#*=}"
            shift
            ;;
        --tenant_aapi_url)
            shift
            tenant_aapi_url="$1"
            shift
            ;;
        --aapi_token=*)
            aapi_token="${1#*=}"
            shift
            ;;
        --aapi_token)
            shift
            aapi_token="$1"
            shift
            ;;
        --export_files_folder=*)
            export_files_folder="${1#*=}"
            shift
            ;;
        --export_files_folder)
            shift
            export_files_folder="$1"
            shift
            ;;
        --python_exe=*)
            python_exe="${1#*=}"
            shift
            ;;
        --python_exe)
            shift
            python_exe="$1"
            shift
            ;;
        *)
            shift
            ;;
    esac
done

# Get tenant_aapi_url if not provided
if [ -z "$tenant_aapi_url" ]; then
    printf "Type your SaaS Control-M Automation API endpoint: "
    read tenant_aapi_url
    if [ -z "$tenant_aapi_url" ]; then
        echo "ERROR: Control-M SaaS Automation API Endpoint is required."
        exit 1
    fi
fi

# Get aapi_token if not provided
if [ -z "$aapi_token" ]; then
    printf "Type your SaaS Control-M Automation API Token: "
    read aapi_token
    if [ -z "$aapi_token" ]; then
        echo "ERROR: Control-M Automation API Token is required."
        exit 1
    fi
fi

# Get export_files_folder if not provided
if [ -z "$export_files_folder" ]; then
    printf "Type the full path to the local or network directory where the compressed Workload Archiving data files were exported: "
    read export_files_folder
    if [ -z "$export_files_folder" ]; then
        echo "ERROR: Export files folder path is required."
        exit 1
    fi
fi

# Determine python executable if not provided via --python_exe
if [ -z "$python_exe" ]; then
    if [ -z "$EM_HOME" ]; then
        python_exe="python3"
    else
        python_exe="bmcpython"
    fi
    
    # Test if the default python works
    if ! $python_exe --version >/dev/null 2>&1; then
        # Try alternative
        if [ -z "$EM_HOME" ]; then
            python_exe="python"
        else
            python_exe="python3"
        fi
        
        if ! $python_exe --version >/dev/null 2>&1; then
            # Both failed, ask user
            printf "Type the name of your Python interpreter: "
            read python_exe
            if [ -z "$python_exe" ]; then
                echo "ERROR: Python interpreter is required."
                exit 1
            fi
        fi
    fi
fi

# echo "Using Python: $python_exe"
echo "Running: \"$python_exe\" \"$python_script_path\" --tenant_aapi_url=\"$tenant_aapi_url\" --aapi_token=\"$aapi_token\" --export_files_folder=\"$export_files_folder\""
"$python_exe" "$python_script_path" --tenant_aapi_url="$tenant_aapi_url" --aapi_token="$aapi_token" --export_files_folder="$export_files_folder" 2>&1
exit_code=$?

# Handle exit codes
if [ $exit_code -ne 0 ] && [ $exit_code -ne 2 ]; then
    echo ""
    echo "========================================"
    echo "ERROR: Script execution failed with exit code: $exit_code"
    echo "If you see \"ModuleNotFoundError: No module named 'requests'\","
    echo "please run: $python_exe -m pip install requests"
    echo "========================================"
    echo ""
fi

exit $exit_code
