#!/bin/sh

exec_if_exists() {
    if [ -x "$1" ]; then
        exec "$@"
    fi
}

die() {
    echo "$@" >&2
    exit 1
}

case "$(uname -m)" in
    x86_64)
        NSYS_HOST_DIR=host-linux-x64
        ;;
    aarch64)
        NSYS_HOST_DIR=host-linux-armv8
        ;;
    ppc64le)
        die "Error: Nsight Systems GUI is not supported on $(uname -m). Please use the command-line tool: nsys"
        ;;
    *)
        die "Error: Nsight Systems is not supported on $(uname -m)"
        ;;
esac

DIR=$(dirname "$(readlink -f -- "$0")")
CUDA_INSTALL_DIR=$(cd "$DIR/.." && pwd)
exec_if_exists "$CUDA_INSTALL_DIR"/nsight-systems-2023.4.4/"${NSYS_HOST_DIR}"/nsys-ui "$@"
exec_if_exists /opt/nvidia/nsight-systems/2023.4.4/"${NSYS_HOST_DIR}"/nsys-ui "$@"

die "Error: Nsight Systems 2023.4.4 hasn't been installed with CUDA Toolkit 12.4"
