#!/bin/sh
# Entry point for the RunPod worker. The pip nvidia wheels dlopen sublibraries
# (libnvrtc-builtins, cuDNN's sublibraries) by bare soname at runtime, and the
# eager preload in quber.utils.cuda_runtime does not cover those lazy loads —
# without the wheel lib dirs on LD_LIBRARY_PATH every page fails. Resolved by
# glob so the list tracks whatever the lock installs.
set -e

NVIDIA_LIBS=$(python - <<'PYEOF'
import site
from pathlib import Path

dirs: list[str] = []
for packages in site.getsitepackages():
    root = Path(packages) / "nvidia"
    if root.is_dir():
        dirs.extend(str(lib) for lib in sorted(root.glob("*/lib")) if lib.is_dir())
print(":".join(dirs))
PYEOF
)

export LD_LIBRARY_PATH="${NVIDIA_LIBS}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"

# Extra args pass through to the runpod SDK (e.g. --test_input for a local
# one-shot run, --rp_serve_api for a local API emulator).
exec python -u /app/runpod/handler.py "$@"
