#!/bin/sh

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2019-present Team LibreELEC (https://libreelec.tv)

GPUINFO=/sys/kernel/debug/mali/version
[ ! -f "$GPUINFO" ] && GPUINFO=$(find /sys/devices/platform -name gpuinfo)

echo "GPUINFO: $GPUINFO"
[ ! -f "$GPUINFO" ] && exit

GPU=$(cat $GPUINFO)
echo "GPU: $GPU"

case $GPU in
  Mali-400*) LIB="libmali-utgard-400-*.so" ;;
  Mali-450*) LIB="libmali-utgard-450-*.so" ;;
  Mali-T60x*) LIB="libmali-midgard-t60x-*.so" ;;
  Mali-T62x*) LIB="libmali-midgard-t620-*.so" ;;
  Mali-T72x*) LIB="libmali-midgard-t720-*.so" ;;
  Mali-T76x*r0p0*) LIB="libmali-midgard-t76x-*-r0p0-*.so" ;;
  Mali-T76x*r1p0*) LIB="libmali-midgard-t76x-*-r1p0-*.so" ;;
  Mali-T82x*) LIB="libmali-midgard-t82x-*.so" ;;
  Mali-T83x*) LIB="libmali-midgard-t83x-*.so" ;;
  Mali-T86x*) LIB="libmali-midgard-t86x-*.so" ;;
  Mali-T88x*) LIB="libmali-midgard-t88x-*.so" ;;
  Mali-G31*) LIB="libmali-bifrost-g31-*.so" ;;
  Mali-G51*) LIB="libmali-bifrost-g51-*.so" ;;
  Mali-G52*) LIB="libmali-bifrost-g52-*.so" ;;
  Mali-G71*) LIB="libmali-bifrost-g71-*.so" ;;
  Mali-G72*) LIB="libmali-bifrost-g72-*.so" ;;
  Mali-G76*) LIB="libmali-bifrost-g76-*.so" ;;
esac

echo "LIB: $LIB"
[ -z "$LIB" ] && exit

LIBMALI=$(find /usr/lib -name $LIB)

echo "LIBMALI: $LIBMALI"
[ ! -f "$LIBMALI" ] && exit

mkdir -p /var/lib/libmali
ln -sf $LIBMALI /var/lib/libmali/libmali.so
