#!/bin/bash
# This script is used to gather information about the HW and SW of the system, in addition to information about the AI Manager.
# The information is stored in a directory located in ~/nxai_troubleshooting and then compressed into a file named ~/nxai_troubleshooting.tgz.
# To get support, please attach the compressed file to your support request.
# NOTE: No sensitive information is collected, only the basic system information and the AI Manager information.
# Enable debug mode
# set -x

############################### Configuration
nxai_api_host="api.sclbl.nxvms.com"
nxai_cdn_host="cdn.sclbl.nxvms.com"
nxai_artifactory_host="artifactory.nxvms.dev"

# Create directory where the information will be stored
current_dir=$(pwd)
info_dir=~/nxai_troubleshooting
rm -rf $info_dir >/dev/null 2>&1
rm -rf $info_dir.tgz >/dev/null 2>&1
mkdir -p $info_dir

# Redirect all output and error streams to a log file
log_file="$info_dir/nxai_troubleshooting.log"
exec > >(tee -a "$log_file") 2>&1

# Run a command with a 30-second timeout, write output to a file, and log any error.
run_cmd() {
    local out="$1"; shift
    timeout 30 "$@" >"$out" 2>&1
    local code=$?
    if [ $code -eq 124 ]; then
        echo "ERROR: '$*' timed out after 30 seconds" >>"$out"
    elif [ $code -ne 0 ]; then
        echo "ERROR: '$*' exited with code $code" >>"$out"
    fi
}

############################### Basic System Information
lsb_release -a >$info_dir/lsb_release.txt
uname -a >$info_dir/uname.txt
lscpu >$info_dir/lscpu.txt
lspci >$info_dir/lspci.txt
df -h >$info_dir/df.txt
ldd --version >$info_dir/ldd_version.txt

############################### Check if the mediaserver is installed
plugins_dir=""
install_dir=""
if [ -d /opt/networkoptix-metavms/mediaserver/bin/plugins/ ]; then
    install_dir="/opt/networkoptix-metavms/mediaserver"
    plugins_dir="$install_dir/bin/plugins/"
elif [ -d /opt/networkoptix/mediaserver/bin/plugins/ ]; then
    install_dir="/opt/networkoptix/mediaserver"
    plugins_dir="$install_dir/bin/plugins/"
    echo "This is not a Meta installation"
else
    echo "Mediaserver is not installed."
fi

# get Mediaserver installed version
cat $install_dir/build_info.txt >$info_dir/mediaserver_info.txt

############################### Check if AI Plugin is installed
if [ -f $plugins_dir/nx_ai_manager_plugin/libnx_ai_manager_plugin.so ]; then
    echo "AI Plugin is installed."
else
    echo "AI Plugin is not installed."
fi
libnxai_plugin_dir=$plugins_dir/nx_ai_manager_plugin/

# Check file permissions
ls -lR $libnxai_plugin_dir/ >"$info_dir/nxai_plugin_permissions.txt"

# Check if tree is installed
if command -v tree >/dev/null 2>&1; then
    echo "Using tree to list files in the AI Plugin directory"
    tree -h --du "$libnxai_plugin_dir/" >"$info_dir/nxai_plugin_tree.txt"
else
    echo "Using du to list file sizes in the AI Plugin directory"
    du -ah "$libnxai_plugin_dir/" >"$info_dir/nxai_plugin_du.txt"
fi

# Set AI manager root directory
nxai_manager_dir=$install_dir/var/nx_ai_manager/nxai_manager/

############################### Check if AI Manager is installed
if [ -d $nxai_manager_dir/bin ]; then
    echo "AI Manager is installed in $nxai_manager_dir"
else
    echo "AI Manager is not installed."
fi

bin_dir=$nxai_manager_dir/bin/

# Gather all log files in the AI Manager directory and its parent dir (nxai_plugin logs are written there)
mkdir -p $info_dir/log
echo "Collecting log files from $nxai_manager_dir"
find "$nxai_manager_dir" "$(dirname "$nxai_manager_dir")" -name "*.log*" -exec cp {} "$info_dir/log" \;

############################### Check AI Manager configuration
if [ -f $bin_dir/installed_runtime.txt ]; then
    echo "Runtime might be installed."
else
    echo "Runtime not installed."
fi

# Get the installed runtime information
cp $bin_dir/installed_runtime.txt $info_dir/installed_runtime.txt

# Get all settings files
mkdir -p $info_dir/json
echo "Collecting json files from $nxai_manager_dir"
find $nxai_manager_dir -name "*.json" -exec cp {} $info_dir/json \;

############################### Check if AI Manager is running
# get running processes
ps aux | grep nxai >>$info_dir/nxai_manager_ps_aux.txt

############################### Check connectivity to the AI Manager Cloud
# Check if curl or wget is available
echo "Downloading test files from the AI Manager Cloud to measure download speed..."
if command -v curl >/dev/null 2>&1; then
    echo "Using curl"
    # check if AI Manager Cloud is reachable
    curl -s https://$nxai_api_host/dev/ >$info_dir/nxai_cloud_connectivity.txt
    # Download a file from the AI Manager Cloud to measure the download speed
    curl -s -m 20 "https://$nxai_cdn_host/benchmark.bin?size=10" -o /dev/null -w "%{speed_download}" |
        awk '{print "Model download speed: " $1/1048576 " MB/sec"}' \
            >$info_dir/nxai_cloud_download_speed_of_model.txt
    curl -s -m 20 "https://$nxai_artifactory_host/artifactory/nxai_open/files/23MB.bin" -o /dev/null -w "%{speed_download}" |
        awk '{print "Runtime download speed: " $1/1048576 " MB/sec"}' \
            >$info_dir/nxai_cloud_download_speed_of_runtime.txt
elif command -v wget >/dev/null 2>&1; then
    echo "Using wget"
    wget -q -O "$info_dir/nxai_cloud_connectivity.txt" https://$nxai_api_host/dev/
    wget --timeout=20 "https://$nxai_cdn_host/benchmark.bin?size=10" -O /dev/null >$info_dir/nxai_cloud_download_speed_of_model.txt 2>&1
    wget --timeout=20 "https://$nxai_artifactory_host/artifactory/nxai_open/files/23MB.bin" -O /dev/null >$info_dir/nxai_cloud_download_speed_of_runtime.txt 2>&1
else
    echo "ERROR: Neither curl nor wget is installed."
fi
# Get the latency to the AI Manager Cloud
ping -c 10 "$nxai_api_host" >$info_dir/nxai_cloud_ping.txt

# Get information about DEEPX if available
if command -v dxrt-cli >/dev/null 2>&1; then
    echo "dxrt-cli is installed."
    run_cmd "$info_dir/dxrt_cli_version.txt" dxrt-cli -s
else
    echo "dxrt-cli is not installed."
fi

# Get information about Nvidia GPU if available
if command -v nvidia-smi >/dev/null 2>&1; then
    echo "nvidia-smi is installed."
    run_cmd "$info_dir/nvidia_smi.txt" nvidia-smi
else
    echo "nvidia-smi is not installed."
fi

# Get information about Intel NPU if available
if lsmod | grep -qi "ivpu\|intel_vpu" 2>/dev/null; then
    echo "Intel NPU kernel module is loaded."
    lsmod | grep -i "ivpu\|intel_vpu" >"$info_dir/intel_npu_modules.txt" 2>&1
else
    echo "Intel NPU kernel module is not loaded."
fi

# List accelerator devices (Intel NPU etc. appear under /dev/accel/)
if [ -d /dev/accel ]; then
    echo "Accelerator devices found."
    ls -la /dev/accel/ >"$info_dir/accel_devices.txt" 2>&1
else
    echo "No accelerator devices found at /dev/accel."
fi

# Get information about Hailo NPU if available
if command -v hailortcli >/dev/null 2>&1; then
    echo "hailortcli is installed."
    run_cmd "$info_dir/hailo_info.txt" hailortcli fw-control identify
else
    echo "hailortcli is not installed."
fi

# Check for EdgeCortix SAKURA-2 NPU
if command -v edgecortix-cli >/dev/null 2>&1; then
    echo "edgecortix-cli is installed."
    run_cmd "$info_dir/edgecortix_info.txt" edgecortix-cli --version
elif ls /dev/sakura* >/dev/null 2>&1; then
    echo "EdgeCortix SAKURA device found."
    ls -la /dev/sakura* >"$info_dir/edgecortix_info.txt" 2>&1
else
    echo "EdgeCortix SAKURA not found."
fi

# Check for Memryx MX3 NPU
if ls /dev/mxa* >/dev/null 2>&1; then
    echo "Memryx MXA device found."
    ls -la /dev/mxa* >"$info_dir/memryx_info.txt" 2>&1
elif command -v memryx-cli >/dev/null 2>&1; then
    echo "memryx-cli is installed."
    run_cmd "$info_dir/memryx_info.txt" memryx-cli info
else
    echo "Memryx accelerator not found."
fi

# Check for Axelera Metis NPU
if ls /dev/axelera* >/dev/null 2>&1; then
    echo "Axelera Metis device found."
    ls -la /dev/axelera* >"$info_dir/axelera_info.txt" 2>&1
elif command -v axelera-cli >/dev/null 2>&1; then
    echo "axelera-cli is installed."
    run_cmd "$info_dir/axelera_info.txt" axelera-cli info
else
    echo "Axelera Metis not found."
fi

# Check for Sima.ai MLSoC NPU
if ls /dev/sima* >/dev/null 2>&1; then
    echo "Sima.ai device found."
    ls -la /dev/sima* >"$info_dir/sima_info.txt" 2>&1
elif command -v simactl >/dev/null 2>&1; then
    echo "simactl is installed."
    run_cmd "$info_dir/sima_info.txt" simactl info
else
    echo "Sima.ai accelerator not found."
fi

############################### tar compress the information
cd $info_dir/..
tar -cvf $info_dir.tgz "$(basename $info_dir)" >/dev/null || echo "ERROR: Failed to compress the information."
rm -rf $info_dir >/dev/null 2>&1

echo "System information gathering complete."
echo "The collected information is stored in $info_dir.tgz"
echo "Please attach this archive to your support request."
cd "$current_dir"