# Maintainer: Denis Benato <benato.denis96 [at] gmail dot com>
# Maintainer: Chris Lane <aur at chrislane dot com>
# Maintainer: DexterHaxxor <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Laurent Carlier <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Felix Yan <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Reza Jahanbakhshi <reza.jahanbakhshi at gmail dot com>
# Contributor: Lone_Wolf <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Armin K. <krejzi at email dot com>
# Contributor: Kristian Klausen <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Egon Ashrafinia <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Tavian Barnes <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Jan de Groot <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Andreas Radke <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
# Contributor: Thomas Dziedzic < gostrc at gmail >
# Contributor: Antti "Tera" Oja <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>

pkgbase=mesa-amdonly-gaming-git
pkgver=24.3.0_devel.195384.fd11bbbb90e.d41d8cd
options=(!lto) # LTO is bad for mesa, makes random applications crash on my system
pkgname=(
  'amdonly-gaming-vulkan-mesa-layers-git'
  'amdonly-gaming-opencl-clover-mesa-git'
  'amdonly-gaming-opencl-rusticl-mesa-git'
  'amdonly-gaming-vulkan-radeon-git'
  'amdonly-gaming-vulkan-swrast-git'
  'amdonly-gaming-libva-mesa-driver-git'
  'amdonly-gaming-mesa-vdpau-git'
  'amdonly-gaming-mesa-git'
)
pkgrel=1
pkgdesc="An open-source implementation of the OpenGL specification"
url="https://www.mesa3d.org/"
arch=('x86_64')
license=('LicenseRef-mesa')  # Ensure this matches your LICENSE file path
makedepends=(
  'expat'
  'libdrm'
  'libelf'
  'libglvnd'
  'libva'
  'libvdpau'
  'libx11'
  'libxdamage'
  'libxml2'
  'libxrandr'
  'libxshmfence'
  'libxxf86vm'
  'llvm'
  'lm_sensors'
  'rust'
  'spirv-llvm-translator'
  'spirv-tools'
  'wayland'
  'xcb-util-keysyms'
  'zstd'

  # shared between mesa and lib32-mesa
  'git'
  'clang'
  'cmake'
  'elfutils'
  'glslang'
  'libclc'
  'meson'
  'python-mako'
  'python-ply'
  'python-yaml'
  'rust-bindgen'
  'wayland-protocols'
  'xorgproto'

  # d3d12 deps
  'directx-headers'
)
source=(
  'https://gitlab.freedesktop.org/mesa/mesa/-/raw/main/docs/license.rst'  # Fetch the license from the repo directly
)
b2sums=('SKIP'
        'cc60238726b35133b5b729fb4ed1e76e04136588533615d84b4a54656d5b41727d5e7ff06ef4de3eb102eed6669d6c5c5cb8ac9fbdf6fc25aa477877c5c3ba87')

pkgver() {
  cd "mesa"
  local _ver
  _ver=$(<VERSION)

  local _patchver
  local _patchfile
  for _patchfile in "${source[@]}"; do
    _patchfile="${_patchfile%%::*}"
    _patchfile="${_patchfile##*/}"
    [[ $_patchfile = *.patch ]] || continue
    _patchver="${_patchver}$(md5sum ${srcdir}/${_patchfile} | cut -c1-32)"
  done
  _patchver="$(echo -n $_patchver | md5sum | cut -c1-7)"

  echo ${_ver/-/_}.$(git rev-list --count HEAD).$(git rev-parse --short HEAD).${_patchver}
}

prepare() {
  if [  -d _build ]; then
    rm -rf _build
  fi

  local _patchfile
  for _patchfile in "${source[@]}"; do
    _patchfile="${_patchfile%%::*}"
    _patchfile="${_patchfile##*/}"
    [[ $_patchfile = *.patch ]] || continue
    echo "Applying patch $_patchfile..."
    patch --directory=mesa --forward --strip=1 --input="${srcdir}/${_patchfile}"
  done
}

build() {
  local meson_options=(
    -D android-libbacktrace=disabled
    -D b_ndebug=true
    -D b_lto=false
    -D egl=enabled
    -D gallium-drivers=radeonsi,llvmpipe,softpipe,zink
    -D gallium-extra-hud=true
    -D gallium-nine=true
    -D gallium-opencl=icd
    -D gallium-rusticl=true
    -D gallium-va=enabled
    -D gallium-vdpau=enabled
    -D gbm=enabled
    -D gles1=disabled
    -D gles2=enabled
    -D glvnd=enabled
    -D glx=dri
    -D intel-rt=disabled
    -D libunwind=disabled
    -D llvm=enabled
    -D lmsensors=enabled
    -D microsoft-clc=disabled
    -D osmesa=true
    -D platforms=x11,wayland
    -D rust_std=2021
    -D shared-glapi=enabled
    -D opencl-spirv=true
    -D valgrind=disabled
    -D video-codecs=all
    -D vulkan-drivers=amd,swrast
    -D vulkan-layers=device-select,overlay
    -D vulkan-beta=true
    -D gallium-xa=disabled  # Disable gallium-xa feature
    --wrap-mode=nofallback
    -D sysconfdir=/etc
  )

  arch-meson mesa build "${meson_options[@]}"
  meson compile $NINJAFLAGS -C build
  DESTDIR="${srcdir}/fakeinstall" meson install -C build
}

_install() {
  local src f dir
  for src; do
    f="${src#fakeinstall/}"
    dir="${pkgdir}/${f%/*}"
    install -m755 -d "${dir}"
    mv -v "${src}" "${dir}/"
  done
}

_libdir=usr/lib

package_amdonly-gaming-vulkan-mesa-layers-git() {
  pkgdesc="Mesa's Vulkan layers"
  depends=(
    'libdrm'
    'libxcb'
    'wayland'
    'python'
  )
  conflicts=(
    'vulkan-mesa-layers'
    'vulkan-mesa-layer'
  )
  provides=(
    'vulkan-mesa-layers'
  )

  _install fakeinstall/usr/share/vulkan/explicit_layer.d
  _install fakeinstall/usr/share/vulkan/implicit_layer.d
  _install fakeinstall/usr/lib/*.so*  # Adjusted this line
  
  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}

package_amdonly-gaming-opencl-clover-mesa-git() {
  pkgdesc="OpenCL implementation for AMD GPUs"
  depends=(
    'libdrm'
    'llvm'
    'zlib'
    'zstd'
    'libclc'
  )
  optdepends=('opencl-headers: headers necessary for OpenCL development')
  provides=(
    'opencl-driver'
    'opencl-rusticl-mesa'
  )
  conflicts=(
    'opencl-mesa'
    'opencl-rusticl-mesa'
  )

  _install fakeinstall/etc/OpenCL/vendors/mesa.icd
  _install fakeinstall/$_libdir/libMesaOpenCL*
  _install fakeinstall/$_libdir/gallium-pipe

  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}

package_amdonly-gaming-opencl-rusticl-mesa-git() {
  pkgdesc="OpenCL Rust implementation for AMD GPUs"
  depends=(
    'libdrm'
    'llvm'
    'zlib'
    'zstd'
    'libclc'
  )
  optdepends=('opencl-headers: headers necessary for OpenCL development')
  provides=(
    'opencl-driver'
    'opencl-mesa'
  )
  conflicts=(
    'opencl-mesa'
    'opencl-rusticl-mesa'
  )

  _install fakeinstall/etc/OpenCL/vendors/rusticl.icd
  _install fakeinstall/$_libdir/libMesaOpenCLRusticl*
  
  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}

package_amdonly-gaming-vulkan-radeon-git() {
  pkgdesc="Radeon Vulkan driver"
  depends=(
    'libdrm'
    'vulkan-icd-loader'
  )
  conflicts=('vulkan-radeon')
  provides=('vulkan-radeon')

  _install fakeinstall/$_libdir/libvulkan_radeon.so*
  _install fakeinstall/$_libdir/libvulkan.so*
  
  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}

package_amdonly-gaming-vulkan-swrast-git() {
  pkgdesc="Mesa swrast Vulkan driver"
  depends=('libdrm' 'vulkan-icd-loader')
  conflicts=('vulkan-swrast')
  provides=('vulkan-swrast')

  _install fakeinstall/$_libdir/libvulkan_swrast.so*
  
  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}

package_amdonly-gaming-libva-mesa-driver-git() {
  pkgdesc="VA-API implementation for Mesa"
  depends=('libdrm' 'libva')
  conflicts=('libva-mesa-driver')
  provides=('libva-mesa-driver')

  _install fakeinstall/$_libdir/libva*
  
  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}

package_amdonly-gaming-mesa-vdpau-git() {
  pkgdesc="VDPAU implementation for Mesa"
  depends=('libdrm' 'libvdpau')
  conflicts=('mesa-vdpau')
  provides=('mesa-vdpau')

  _install fakeinstall/$_libdir/libvdpau*
  
  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}

package_amdonly-gaming-mesa-git() {
  pkgdesc="Mesa 3D graphics library"
  depends=(
    'libdrm'
    'libx11'
    'libxdamage'
    'libxrandr'
    'libxshmfence'
    'libxxf86vm'
    'wayland'
  )

  _install fakeinstall/$_libdir/*.so*
  
  install -m644 -Dt "${pkgdir}/usr/share/licenses/${pkgname}" LICENSE
}