#!/bin/sh
set -e

# Ensure every shared-library dependency (NEEDED soname) of the tool and of
# the runtime library resolves on the installed system. A missing soname here
# would mean a broken or under-specified dependency. GPU-independent.

check_resolved() {
    obj="$1"
    echo "== ldd $obj"
    ldd "$obj"
    if ldd "$obj" | grep -q "not found"; then
        echo "unresolved shared-library dependency in $obj" >&2
        exit 1
    fi
}

bin=$(command -v nvidia-container-cli)
check_resolved "$bin"

# Resolve the path to the runtime library the CLI is linked against and
# verify its dependencies too.
lib=$(ldd "$bin" | awk '/libnvidia-container/ {print $3; exit}')
if [ -n "$lib" ] && [ -e "$lib" ]; then
    check_resolved "$lib"
fi
