#!/usr/bin/bash
# virgl_test_server wrapper for non-cx4 GPUs (Moore Threads MTT, Intel, AMD, ...):
# stock GLX path + LD_PRELOAD virgl_gpu_compat.so, a GPU-agnostic compatibility
# shim (version-retry, compat-profile, #version bump, extension softening, GL-error
# suppression -- all no-ops where unneeded).  Also normalises --socket-path.
SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
# Bundled virglrenderer 1.1.0 first -- see virgl_test_server_cx4 for why the
# host's own virgl-server cannot be trusted (0.8.2 on Deepin 25 rejects the args).
if [ -n "${VIRGL_REAL_SERVER:-}" ]; then
  REAL="$VIRGL_REAL_SERVER"
elif [ -x "$SELF_DIR/virgl_test_server" ]; then
  REAL="$SELF_DIR/virgl_test_server"
else
  REAL=/usr/bin/virgl_test_server
fi
args=()
while [ $# -gt 0 ]; do case "$1" in
  --socket-path) args+=("--socket-path=$2"); shift 2;;
  *) args+=("$1"); shift;; esac; done
export LD_PRELOAD="${LD_PRELOAD:+$LD_PRELOAD:}$SELF_DIR/virgl_gpu_compat.so"
exec "$REAL" "${args[@]}"
