#!/usr/bin/env bash
# mtt-accel — toggle/inspect system-wide MTT S80 32-bit GL/Vulkan acceleration.
#
# 64-bit apps + the desktop use the S80 natively (MT's glvnd mtgpu vendor) with no
# help from this tool. This wires the 32-bit half: a per-arch preload injector
# (mtt-gl32-env.so, via /etc/ld.so.preload) routes 32-bit OpenGL through Mesa zink
# over the Venus 32->64 bridge -> 64-bit MUSA Vulkan -> hardware S80. It uses the
# BARE SONAME in /etc/ld.so.preload (ld.so resolves it per-arch from /usr/lib/<triplet>)
# so setuid tools like sudo stay warning-free. The bridge (mtt-vk32bridge.service)
# must be running for 32-bit accel.
#
# Usage:  mtt-accel {on|off|status|test}   (on/off need root; they edit /etc)
set -uo pipefail

PRELOAD=/etc/ld.so.preload
SONAME=libmtt-gl32-env.so   # 'lib' prefix REQUIRED so ldconfig caches it (sudo/setuid resolve preloads via the cache only)
SERVICE=mtt-vk32bridge.service
GLX64=/usr/bin/glxinfo.x86_64-linux-gnu
GLX32=/usr/bin/glxinfo.i386-linux-gnu

renderer() { local b="$1"; [ -x "$b" ] || { echo "(no $b)"; return; }
             "$b" -B 2>/dev/null | sed -n 's/^OpenGL renderer string: //p' | head -1; }

cmd_on() {
  [ -f /usr/lib/x86_64-linux-gnu/$SONAME ] && [ -f /usr/lib/i386-linux-gnu/$SONAME ] \
    || { echo "mtt-accel: injector libs not installed (reinstall the -Experimental driver)." >&2; exit 1; }
  ldconfig 2>/dev/null || true
  [ -f "$PRELOAD" ] && sed -i "/mtt-gl32-env/d" "$PRELOAD"      # heal any stale entry
  touch "$PRELOAD"; printf '%s\n' "$SONAME" >> "$PRELOAD"
  local err; err=$(/bin/true 2>&1 >/dev/null || true)
  if [ -n "$err" ]; then
    sed -i "/mtt-gl32-env/d" "$PRELOAD"
    echo "mtt-accel: preload self-test failed on this system; reverted. Use mtt-gl32/mtt-vk32 per-app." >&2
    exit 1
  fi
  echo "mtt-accel: ON (system-wide). Reboot or log out/in so running apps inherit it."
  echo "  bridge:  systemctl --user enable --now $SERVICE"
}

cmd_off() {
  [ -f "$PRELOAD" ] && { sed -i "/mtt-gl32-env/d" "$PRELOAD"; ldconfig 2>/dev/null || true; }
  echo "mtt-accel: OFF. Log out/in to clear it from running apps."
}

cmd_status() {
  echo "== mtt-accel status =="
  printf "  ld.so.preload: %s\n" "$(grep -q "$SONAME" "$PRELOAD" 2>/dev/null && echo present || echo absent)"
  printf "  bridge svc:    %s\n" "$(systemctl --user is-active "$SERVICE" 2>/dev/null || echo inactive)"
  printf "  bridge sock:   %s\n" "$([ -S /tmp/.virgl_test ] && echo up || echo down)"
  printf "  64-bit GL:     %s\n" "$(renderer "$GLX64")"
  printf "  32-bit GL:     %s\n" "$(renderer "$GLX32")"
}

cmd_test() {
  echo "== render matrix (current system state) =="
  printf "  64-bit GL (native):       %s\n" "$(renderer "$GLX64")"
  printf "  32-bit GL (zink->bridge): %s\n" "$(renderer "$GLX32")"
}

case "${1:-}" in
  on|off)
    if [ "$(id -u)" -ne 0 ]; then exec sudo -- "$0" "$@"; fi
    [ "$1" = on ] && cmd_on || cmd_off ;;
  status) cmd_status ;;
  test)   cmd_test ;;
  *) echo "usage: mtt-accel {on|off|status|test}"; exit 1 ;;
esac
