#!/bin/sh

# HOW TO RUN:
# 1) cd to run_archive_migration_report.sh script location
# 2) sh run_archive_migration_report.sh [--tenant_aapi_url=<value>] [--aapi_token="<value>"] [--report_history_days=<value>] [--python_exe=<value>]
# Example: sh run_archive_migration_report.sh --tenant_aapi_url=https://example.com --aapi_token="TOKEN123==" --report_history_days=1
# NOTE: Quote aapi_token values to preserve special characters

# Parse command-line arguments
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
            ;;
        --report_history_days=*)
            report_history_days="${1#*=}"
            shift
            ;;
        --report_history_days)
            shift
            report_history_days="$1"
            shift
            ;;
        --python_exe=*)
            python_exe="${1#*=}"
            shift
            ;;
        --python_exe)
            shift
            python_exe="$1"
            shift
            ;;
        *)
            echo "Unknown parameter: $1"
            exit 1
            ;;
    esac
done

# Prompt for mandatory parameters 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 "Control-M SaaS Automation API Endpoint cannot be empty."
        exit 1
    fi
fi

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

# Set default report_history_days if not provided
if [ -z "$report_history_days" ]; then
    report_history_days="7"
fi

# Detect Python interpreter if not provided
if [ -z "$python_exe" ]; then
    if [ -z "$EM_HOME" ]; then
        python_exe="python3"
    else
        python_exe="bmcpython"
    fi
    
    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
            printf "Type the name of your Python interpreter (press ENTER to accept python3): "
            read custom_python_exe
            if [ -n "$custom_python_exe" ]; then
                python_exe="$custom_python_exe"
            else
                python_exe="python3"
            fi
        fi
    fi
fi

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

echo "Running: \"$python_exe\" \"$python_script_path\" --tenant_aapi_url=\"$tenant_aapi_url\" --aapi_token=\"$aapi_token\" --report_history_days=$report_history_days"

"$python_exe" "$python_script_path" --tenant_aapi_url="$tenant_aapi_url" --aapi_token="$aapi_token" --report_history_days="$report_history_days" 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
