#!/usr/bin/env bash
# Install AMD Ryzen AI:
#   1. C++ runtime from .deb package
#   2. Python wheels from a URL or local directory
#
# Usage:
#   ./setup_ryzen_ai.sh -a yes -d <path-to-deb> [options]
#
# Options:
#   -a <yes>        Agree to EULA (required)
#   -d <path>       Path to the ryzen-ai-runtime .deb file (required)
#   -p <path>       Path for the virtual environment (default: ~/ryzen-ai-venv)
#   -c <path>       Custom C++ install path (uses dpkg-deb -x instead of sudo dpkg -i)
#   -w <dir>        Install from a local wheels directory
#   -i <url>        PyPI index or URL containing Ryzen AI wheels (default: https://pypi.amd.com/acas/ryzenai/<VERSION>/linux/simple/)
#   -n <name>       Prompt name for the venv (default: ryzen-ai)
#   -y              Skip confirmation prompts
#   -h              Show this help
#
# Examples:
#   # Standard install (needs sudo for dpkg -i):
#   ./setup_ryzen_ai.sh -a yes -d ./ryzen-ai-runtime_2.0.0-1_amd64.deb
#
#   # Custom install path (no sudo needed):
#   ./setup_ryzen_ai.sh -a yes -d ./ryzen-ai-runtime_2.0.0-1_amd64.deb -c ~/ryzen-ai-runtime
#
#   # Install from local wheels directory:
#   ./setup_ryzen_ai.sh -a yes -d ./ryzen-ai-runtime_2.0.0-1_amd64.deb -w ./wheels

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Ryzen AI version — stamped at build time by build_linux_installer.sh.
_RYZENAI_VERSION="1.9.0"
if [ "${_RYZENAI_VERSION}" = "1.9.0" ]; then
    _RYZENAI_VERSION="1.9.0"
fi

_REQUIRED_XRT_VERSION="2.26.89"

# =========================================================================
# Defaults
# =========================================================================
AGREE_EULA=""
DEB_FILE=""
VENV_PATH="${HOME}/ryzen-ai-venv"
CPP_INSTALL_PATH=""
WHEELS_DIR=""
PYPI_INDEX="https://pypi.amd.com/acas/ryzenai/${_RYZENAI_VERSION}/linux/simple/"
PROMPT_NAME="ryzen-ai"
AUTO_YES=false

RYZEN_AI_PACKAGES=(
    onnxruntime-vitisai
    voe
    flexml
    flexmlrt
    vitis-mllib
    flexml-metadata
    llvm-aie
    ryzenai-dynamic-dispatch
    vaitrace
    cardano
    ryzenai-onnx-utils
    onnxruntime-providers-ryzenai
    aianalyzer
    hlsip
    mldebug-xdp
    mlprofilerengine
    vaianalyze
    vitis-aie-essentials
)

MIN_DISK_GB=20

# =========================================================================
# Parse arguments
# =========================================================================
usage() {
    sed -n '2,/^$/p' "$0" | sed 's/^# \?//'
    exit 0
}

# Show help when no arguments are given
if [ $# -eq 0 ]; then
    usage
fi

while getopts "a:d:p:c:w:i:n:yh" opt; do
    case "${opt}" in
        a) AGREE_EULA="${OPTARG}" ;;
        d) DEB_FILE="${OPTARG}" ;;
        p) VENV_PATH="${OPTARG}" ;;
        c) CPP_INSTALL_PATH="${OPTARG}" ;;
        w) WHEELS_DIR="${OPTARG}" ;;
        i) PYPI_INDEX="${OPTARG}" ;;
        n) PROMPT_NAME="${OPTARG}" ;;
        y) AUTO_YES=true ;;
        h) usage ;;
        *) usage ;;
    esac
done

# =========================================================================
# EULA check
# =========================================================================
if [ "${AGREE_EULA}" != "yes" ]; then
    echo "Please read the EULAs at:"
    echo ""
    echo "  * AMD EULA and Third Party Notices - https://ryzenai.docs.amd.com/en/latest/licenses.html"
    echo ""
    echo "If you agree, run with: $0 -a yes ..."
    exit 1
fi

echo "By using this script, you agree to the AMD EULA and Third Party Notices."
echo ""

# =========================================================================
# Resolve relative paths to absolute (without creating directories)
# =========================================================================
if [ -n "${DEB_FILE}" ]; then
    DEB_FILE="$(realpath -m "${DEB_FILE}")"
fi
if [ -n "${CPP_INSTALL_PATH}" ]; then
    CPP_INSTALL_PATH="$(realpath -m "${CPP_INSTALL_PATH}")"
fi
if [ -n "${WHEELS_DIR}" ]; then
    WHEELS_DIR="$(realpath -m "${WHEELS_DIR}")"
fi
VENV_PATH="$(realpath -m "${VENV_PATH}")"

# Auto-detect index type once for use in preflight display and install.
# PEP 503 simple index uses --extra-index-url; plain directory listing
# (e.g. Artifactory storage URL) uses --find-links.
PYPI_HOST=$(echo "${PYPI_INDEX}" | sed -E 's|https?://([^/]+).*|\1|')
_TRUSTED_HOST_ARGS=()
if echo "${PYPI_INDEX}" | grep -qE '/simple/?$'; then
    _INDEX_FLAG="--extra-index-url"
    [ -n "${PYPI_HOST}" ] && _TRUSTED_HOST_ARGS=("--allow-insecure-host" "${PYPI_HOST}")
elif echo "${PYPI_INDEX}" | grep -qE '^file://'; then
    _INDEX_FLAG="--find-links"
else
    _INDEX_FLAG="--find-links"
    [ -n "${PYPI_HOST}" ] && _TRUSTED_HOST_ARGS=("--allow-insecure-host" "${PYPI_HOST}")
fi

# =========================================================================
# Preflight checks
# =========================================================================
echo "==> Preflight checks..."

# OS check
if [ -f /etc/os-release ]; then
    . /etc/os-release
    echo "  OS: ${PRETTY_NAME:-${NAME} ${VERSION_ID}}"
    case "${ID:-}" in
        ubuntu)
            if [ "${VERSION_ID%%.*}" -lt 22 ] 2>/dev/null; then
                echo "  WARNING: Ubuntu ${VERSION_ID} detected. Ubuntu 22.04+ is recommended."
            fi
            ;;
    esac
fi

if [ -z "${DEB_FILE}" ]; then
    echo "ERROR: .deb file path is required. Use -d <path>"
    exit 1
fi

if [ ! -f "${DEB_FILE}" ]; then
    echo "ERROR: .deb file not found: ${DEB_FILE}"
    exit 1
fi

if ! command -v python3.12 &>/dev/null; then
    echo "ERROR: python3.12 not found. Install with: sudo apt install python3.12 python3.12-venv"
    exit 1
fi

# Check python3.12-venv is available
if ! python3.12 -c "import ensurepip" 2>/dev/null; then
    echo "ERROR: python3.12-venv package not installed. Install with: sudo apt install python3.12-venv"
    exit 1
fi

PYTHON_VERSION=$(python3.12 -c 'import platform; print(platform.python_version())')
echo "  Python: ${PYTHON_VERSION}"

# Disk space check — walk up to nearest existing directory for df
_install_target="${CPP_INSTALL_PATH:-/opt/amd}"
while [ ! -d "${_install_target}" ]; do _install_target="$(dirname "${_install_target}")"; done
_avail_gb=$(df -BG --output=avail "${_install_target}" | tail -1 | tr -dc '0-9')
if [ "${_avail_gb}" -lt "${MIN_DISK_GB}" ] 2>/dev/null; then
    echo "ERROR: Insufficient disk space. Need at least ${MIN_DISK_GB} GB, only ${_avail_gb} GB available at ${_install_target}."
    exit 1
fi
echo "  Disk: ${_avail_gb} GB available at ${_install_target}"

# RAM check
CPU_COUNT=$(nproc)
RAM_SIZE=$(free -g | awk '/^Mem:/{print $2}')
echo "  System: ${CPU_COUNT} CPUs, ${RAM_SIZE} GB RAM"
if [ "${RAM_SIZE}" -lt 16 ] 2>/dev/null; then
    echo "  WARNING: System has ${RAM_SIZE} GB RAM. At least 16 GB recommended."
fi

# XRT detection — try xrt-smi first (shipped with current drivers), then
# fall back to /opt/xilinx/xrt (older installs).  On Ubuntu 26.04+ the
# driver is upstreamed and xrt-smi lives in /usr/bin, so /opt/xilinx/xrt
# may not exist at all.
_XRT_SMI=""
if command -v xrt-smi &>/dev/null; then
    _XRT_SMI="xrt-smi"
elif [ -x /opt/xilinx/xrt/bin/xrt-smi ]; then
    _XRT_SMI="/opt/xilinx/xrt/bin/xrt-smi"
fi

if [ -n "${_XRT_SMI}" ]; then
    _xrt_out=$("${_XRT_SMI}" examine 2>/dev/null || true)
    XRT_VERSION=$(echo "${_xrt_out}" | awk '/^XRT/{f=1} f && /Version/{print $NF; exit}')
    if [ -n "${XRT_VERSION}" ]; then
        echo "  XRT: ${XRT_VERSION} (recommended minimum: ${_REQUIRED_XRT_VERSION})"
        if [ "${XRT_VERSION}" != "${_REQUIRED_XRT_VERSION}" ]; then
            echo "  WARNING: XRT version ${XRT_VERSION} does not match recommended minimum ${_REQUIRED_XRT_VERSION}."
            echo "           Running models on the NPU may not work. Model compilation is unaffected."
        fi
    else
        echo "  XRT: installed (version could not be determined)"
        echo "           Run 'xrt-smi examine' to check your XRT setup."
    fi
elif [ -f /opt/xilinx/xrt/setup.sh ]; then
    echo "  XRT: installed (xrt-smi not found; version could not be determined)"
    echo "           Run '/opt/xilinx/xrt/bin/xrt-smi examine' to check your XRT setup."
else
    echo "  WARNING: XRT/NPU driver not detected."
    echo "           XRT is required for running models on the NPU."
    echo "           Model compilation does not require XRT."
fi

echo "  .deb: $(basename "${DEB_FILE}")"
if [ -n "${WHEELS_DIR}" ]; then
    shopt -s nullglob
    _wheels=("${WHEELS_DIR}"/*.whl)
    if [ "${#_wheels[@]}" -eq 0 ]; then
        echo "ERROR: No .whl files found in '${WHEELS_DIR}'"
        exit 1
    fi
    echo "  Wheels: ${#_wheels[@]} file(s) in '${WHEELS_DIR}'"
else
    if [ "${_INDEX_FLAG}" = "--extra-index-url" ]; then
        echo "  PyPI index: ${PYPI_INDEX}"
    else
        echo "  Wheels URL: ${PYPI_INDEX}"
    fi
fi

# Venv overwrite prompt — but do NOT delete yet (deletion happens after .deb succeeds)
_VENV_EXISTS=false
if [ -d "${VENV_PATH}" ]; then
    if [ ! -f "${VENV_PATH}/pyvenv.cfg" ]; then
        echo "ERROR: '${VENV_PATH}' exists but is not a Python venv (no pyvenv.cfg). Refusing to overwrite."
        exit 1
    fi
    echo ""
    echo "WARNING: Virtual environment already exists at '${VENV_PATH}'"
    if [ "${AUTO_YES}" != true ]; then
        read -rp "  Overwrite? [y/N] " _confirm
        if [[ ! "${_confirm}" =~ ^[Yy] ]]; then
            echo "Aborted."
            exit 0
        fi
    fi
    _VENV_EXISTS=true
fi

# =========================================================================
# Step 1: Install C++ runtime from .deb
# =========================================================================
echo ""
echo "==> Installing C++ runtime from .deb..."

if [ -n "${CPP_INSTALL_PATH}" ]; then
    echo "  Using dpkg-deb -x to custom path: ${CPP_INSTALL_PATH}"
    mkdir -p "${CPP_INSTALL_PATH}"
    dpkg-deb -x "${DEB_FILE}" "${CPP_INSTALL_PATH}"
    CPP_PATH="${CPP_INSTALL_PATH}/opt/amd/ryzenai/${_RYZENAI_VERSION}"

    if [ ! -d "${CPP_PATH}/deployment/lib" ]; then
        echo "ERROR: .deb extraction failed — deployment/lib not found at ${CPP_PATH}"
        exit 1
    fi
    echo "  C++ runtime extracted to: ${CPP_PATH}"
else
    echo "  Using sudo dpkg -i (installs to /opt/amd/ryzenai/${_RYZENAI_VERSION})"
    # EULA + TPN already accepted via -a yes — record it so the .deb preinst does not prompt again
    printf '%s\n' \
        "ryzen-ai-runtime ryzen-ai-runtime/accepted-amd-eula boolean true" \
        | sudo debconf-set-selections
    sudo dpkg -i "${DEB_FILE}"
    CPP_PATH="/opt/amd/ryzenai/${_RYZENAI_VERSION}"

    if [ ! -d "${CPP_PATH}/deployment/lib" ]; then
        echo "ERROR: .deb install failed — deployment/lib not found at ${CPP_PATH}"
        exit 1
    fi
    echo "  C++ runtime installed to: ${CPP_PATH}"
fi

# =========================================================================
# Step 2: Create virtual environment
# =========================================================================

# Now safe to delete old venv — .deb step succeeded
if [ "${_VENV_EXISTS}" = true ]; then
    rm -rf "${VENV_PATH}"
fi

echo ""
echo "==> Creating Python 3.12 virtual environment at '${VENV_PATH}'..."
python3.12 -m venv "${VENV_PATH}" --copies --prompt "${PROMPT_NAME}"

# shellcheck disable=SC1091
source "${VENV_PATH}/bin/activate"

pip install --upgrade pip
pip install uv

# Use the system's native TLS certificate store so uv trusts corporate CAs
# (uv bundles Mozilla roots by default, which lack internal/corporate CAs).
export UV_SYSTEM_CERTS=true

# =========================================================================
# Step 3: Install wheels
# =========================================================================

# Embedded constraints — stamped by build_linux_installer.sh at build time.
# The placeholder is replaced with actual pins (h5py, onnx).
_CONSTRAINTS_CONTENT="h5py==3.15.1
onnx>=1.21,<1.23
numpy==2.5.3
setuptools<71"
CONSTRAINT_ARGS=()
_CONSTRAINTS_FILE=""
if [ "${_CONSTRAINTS_CONTENT}" != "__XMC_CONSTRAINTS__" ]; then
    _CONSTRAINTS_FILE="$(mktemp)"
    echo -e "${_CONSTRAINTS_CONTENT}" > "${_CONSTRAINTS_FILE}"
    CONSTRAINT_ARGS=("--constraints" "${_CONSTRAINTS_FILE}")
    echo "  Using embedded constraints"
fi

if [ -n "${WHEELS_DIR}" ]; then
    echo ""
    echo "==> Installing Ryzen AI wheels from '${WHEELS_DIR}'..."

    # Extract torch/torchvision version pins from wheel METADATA so we install
    # the exact +cpu build the wheels were compiled against.
    _TORCH_PKGS=()
    for _whl in "${WHEELS_DIR}"/flexml-*.whl "${WHEELS_DIR}"/voe-*.whl; do
        [ -f "${_whl}" ] || continue
        while IFS= read -r _line; do
            case "${_line}" in
                *torch==*+cpu*|*torchvision==*+cpu*)
                    _pkg=$(echo "${_line}" | sed 's/.*: //; s/ *;.*//')
                    _TORCH_PKGS+=("${_pkg}")
                    ;;
            esac
        done < <(python3.12 -c "
import zipfile, sys
with zipfile.ZipFile('${_whl}') as z:
    for n in z.namelist():
        if n.endswith('METADATA'):
            for line in z.read(n).decode().splitlines():
                if 'torch' in line and 'Requires-Dist' in line:
                    print(line)
            break
" 2>/dev/null)
    done
    # Deduplicate
    if [ "${#_TORCH_PKGS[@]}" -gt 0 ]; then
        mapfile -t _TORCH_PKGS < <(printf '%s\n' "${_TORCH_PKGS[@]}" | sort -u)
    fi
    # Fallback if we couldn't extract pins
    [ "${#_TORCH_PKGS[@]}" -eq 0 ] && _TORCH_PKGS=("torch")

    echo "  Installing PyTorch (CPU): ${_TORCH_PKGS[*]}"
    uv pip install \
        --index-url https://download.pytorch.org/whl/cpu \
        --allow-insecure-host download.pytorch.org \
        "${_TORCH_PKGS[@]}"

    uv pip install \
        "${WHEELS_DIR}"/*.whl \
        --find-links "${WHEELS_DIR}" \
        --prerelease=allow \
        --extra-index-url https://pypi.org/simple \
        "${CONSTRAINT_ARGS[@]}"

    echo ""
    echo "==> Ensuring onnxruntime-vitisai is the active ORT provider..."
    uv pip uninstall onnxruntime onnxruntime-vitisai 2>/dev/null || true
    uv pip install --no-deps \
        --find-links "${WHEELS_DIR}" \
        --prerelease=allow \
        onnxruntime-vitisai

    if compgen -G "${WHEELS_DIR}/onnxruntime_genai_ryzenai*.whl" > /dev/null; then
        echo "==> Installing onnxruntime-genai-ryzenai..."
        uv pip install --no-deps \
            --find-links "${WHEELS_DIR}" \
            --prerelease=allow \
            onnxruntime-genai-ryzenai
    fi

else
    echo ""
    echo "==> Installing Ryzen AI wheels from ${PYPI_INDEX}..."

    # Install torch+cpu from the PyTorch index first — it must come from
    # download.pytorch.org (pypi.org doesn't carry +cpu variants).
    # Doing this separately avoids --index-strategy unsafe-best-match,
    # which would open all packages to dependency confusion via pypi.org.
    # The placeholder is stamped at build time by build_linux_installer.sh.
    _TORCH_INDEX_PKGS="torch==2.10.0+cpu torchvision==0.25.0+cpu"
    if [ "${_TORCH_INDEX_PKGS}" = "__TORCH_PACKAGES__" ]; then
        _TORCH_INDEX_PKGS="torch"
    fi
    # shellcheck disable=SC2086
    echo "  Installing PyTorch (CPU): ${_TORCH_INDEX_PKGS}"
    uv pip install \
        --index-url https://download.pytorch.org/whl/cpu \
        --allow-insecure-host download.pytorch.org \
        ${_TORCH_INDEX_PKGS}

    _UV_TOML="$(mktemp)"
    cat > "${_UV_TOML}" << UVTOML
[[index]]
name = "ryzenai"
url = "${PYPI_INDEX}"
ignore-error-codes = [403]

[[index]]
name = "pypi"
url = "https://pypi.org/simple"
default = true
UVTOML

    uv pip install \
        --config-file "${_UV_TOML}" \
        "${_TRUSTED_HOST_ARGS[@]}" \
        --prerelease=allow \
        "${CONSTRAINT_ARGS[@]}" \
        "${RYZEN_AI_PACKAGES[@]}"
    rm -f "${_UV_TOML}"

    echo ""
    echo "==> Ensuring onnxruntime-vitisai is the active ORT provider..."
    uv pip uninstall onnxruntime onnxruntime-vitisai 2>/dev/null || true
    uv pip install --no-deps \
        ${_INDEX_FLAG} "${PYPI_INDEX}" \
        "${_TRUSTED_HOST_ARGS[@]}" \
        --prerelease=allow \
        onnxruntime-vitisai

    uv pip install --no-deps \
        ${_INDEX_FLAG} "${PYPI_INDEX}" \
        "${_TRUSTED_HOST_ARGS[@]}" \
        --prerelease=allow \
        onnxruntime-genai-ryzenai 2>/dev/null \
        || echo "  onnxruntime-genai-ryzenai not available — skipping (internal-only package)."
fi

# Install extra requirements (stamped at build time by build_linux_installer.sh).
# These are third-party packages (onnxruntime_extensions, accelerate, etc.) that
# are not part of our wheels but are needed for the full Ryzen AI environment.
_REQUIREMENTS_EXTRAS="accelerate==1.12.0
datasets==4.5.0
evaluate==0.4.6
onnxruntime_extensions==0.15.2
transformers==5.19.0
zstandard==0.25.0"
if [ "${_REQUIREMENTS_EXTRAS}" != "__REQUIREMENTS_EXTRAS__" ]; then
    echo ""
    echo "==> Installing extra requirements..."
    _EXTRAS_FILE="$(mktemp)"
    echo "${_REQUIREMENTS_EXTRAS}" > "${_EXTRAS_FILE}"
    uv pip install -r "${_EXTRAS_FILE}" --prerelease=allow "${CONSTRAINT_ARGS[@]}"
    rm -f "${_EXTRAS_FILE}"
fi

[ -n "${_CONSTRAINTS_FILE}" ] && rm -f "${_CONSTRAINTS_FILE}"

# =========================================================================
# Step 4: Configure environment in activate script
# =========================================================================
echo ""
echo "==> Configuring Ryzen AI environment variables in activate script..."

SITE_PACKAGES=$(python3.12 -c "import sysconfig; print(sysconfig.get_paths()['purelib'])")

# Patch deactivate to restore all Ryzen AI env vars
sed -i '/^deactivate ()/,/^}/ {
    /^}/ i\
    if [ -n "${_OLD_VIRTUAL_PYTHONPATH+x}" ]; then\
        if [ -n "${_OLD_VIRTUAL_PYTHONPATH}" ]; then\
            export PYTHONPATH="${_OLD_VIRTUAL_PYTHONPATH}"\
        else\
            unset PYTHONPATH\
        fi\
        unset _OLD_VIRTUAL_PYTHONPATH\
    fi\
    if [ -n "${_OLD_LD_LIBRARY_PATH+x}" ]; then\
        if [ -n "${_OLD_LD_LIBRARY_PATH}" ]; then\
            export LD_LIBRARY_PATH="${_OLD_LD_LIBRARY_PATH}"\
        else\
            unset LD_LIBRARY_PATH\
        fi\
        unset _OLD_LD_LIBRARY_PATH\
    fi\
    if [ -n "${_OLD_XILINX_XRT+x}" ]; then\
        if [ -n "${_OLD_XILINX_XRT}" ]; then\
            export XILINX_XRT="${_OLD_XILINX_XRT}"\
        else\
            unset XILINX_XRT\
        fi\
        unset _OLD_XILINX_XRT\
    fi\
    unset RYZEN_AI_PATH RYZEN_AI_INSTALLATION_PATH\
    unset XILINX_VITIS XILINX_VITIS_AIETOOLS
}' "${VENV_PATH}/bin/activate"


cat >> "${VENV_PATH}/bin/activate" << RYZENAI_ENV

# --- Ryzen AI environment (deb + pip flow) ---

# Save ALL current env vars before any modifications so deactivate
# restores to exactly the pre-activate state.
export _OLD_VIRTUAL_PYTHONPATH="\${PYTHONPATH:-}"
export _OLD_LD_LIBRARY_PATH="\${LD_LIBRARY_PATH:-}"
export _OLD_XILINX_XRT="\${XILINX_XRT:-}"

# Clear PYTHONPATH to prevent system packages from leaking into the venv
unset PYTHONPATH

# Source XRT if available (required for NPU access).
# XRT prepends to PATH, LD_LIBRARY_PATH, and sets PYTHONPATH and XILINX_XRT.
if [ -f /opt/xilinx/xrt/setup.sh ]; then
    . /opt/xilinx/xrt/setup.sh
fi

# C++ runtime paths
export RYZEN_AI_PATH="${CPP_PATH}"
export RYZEN_AI_INSTALLATION_PATH="${CPP_PATH}"

# Shared library paths: XRT (if present) stays at the front, Ryzen AI appended.
_RYZENAI_LIB_PATH="${SITE_PACKAGES}/flexml/flexml_extras/lib:${SITE_PACKAGES}/onnxruntime/capi:${SITE_PACKAGES}/voe/lib:${CPP_PATH}/deployment/lib:${CPP_PATH}/onnxruntime/lib"
if [ -n "\${LD_LIBRARY_PATH}" ]; then
    export LD_LIBRARY_PATH="\${LD_LIBRARY_PATH}:\${_RYZENAI_LIB_PATH}"
else
    export LD_LIBRARY_PATH="\${_RYZENAI_LIB_PATH}"
fi
unset _RYZENAI_LIB_PATH

# Vitis AI tools
export XILINX_VITIS="${SITE_PACKAGES}"
export XILINX_VITIS_AIETOOLS="${SITE_PACKAGES}"
RYZENAI_ENV

# Re-activate to pick up the new env vars
deactivate
# shellcheck disable=SC1091
source "${VENV_PATH}/bin/activate"

# =========================================================================
# Step 5: Verify installation
# =========================================================================
echo ""
echo "==> Verifying installation..."
python3 -c "
import onnxruntime
providers = onnxruntime.get_available_providers()
print(f'  ONNX Runtime version: {onnxruntime.__version__}')
print(f'  Available providers: {providers}')
if 'VitisAIExecutionProvider' in providers:
    print('  VitisAI EP: available')
else:
    print('  WARNING: VitisAI EP not in available providers.')
    print('           Check that XRT is installed and LD_LIBRARY_PATH is correct.')
"

# =========================================================================
# Done
# =========================================================================
echo ""
echo "================================================================"
echo " Ryzen AI installation complete!"
echo ""
echo " Activate with:"
echo "   source ${VENV_PATH}/bin/activate"
echo ""
echo " Run quicktest:"
echo "   cd ${CPP_PATH}/quicktest"
echo "   python3 quicktest.py"
echo "================================================================"
