# Maintainer: Simon Hallsten <flightlessmangoyt@gmail.com>

pkgname=('mangohud' 'lib32-mangohud')
pkgver=0.8.3.r2.gee672429
pkgrel=1
pkgdesc="Vulkan and OpenGL overlay to display performance information"
arch=('x86_64')
makedepends=('dbus' 'gcc' 'meson' 'python-mako' 'libx11' 'lib32-libx11' 'git' 'pkgconf' 'vulkan-headers')
depends=('glslang' 'libglvnd' 'lib32-libglvnd' 'glfw' 'python-numpy' 'python-matplotlib'
         'libxrandr' 'libxkbcommon' 'lib32-libxkbcommon')
replaces=('vulkan-mesa-layer-mango')
license=('MIT')
source=(
        "mangohud"::"git+https://github.com/flightlessmango/MangoHud.git#branch=master"
        "mangohud-minhook"::"git+https://github.com/flightlessmango/minhook.git"
        "imgui-1.91.6.tar.gz::https://github.com/ocornut/imgui/archive/refs/tags/v1.91.6.tar.gz"
        "spdlog-1.14.1.tar.gz::https://github.com/gabime/spdlog/archive/refs/tags/v1.14.1.tar.gz"
        "spdlog_1.14.1-1_patch.zip::https://wrapdb.mesonbuild.com/v2/spdlog_1.14.1-1/get_patch"
        "vulkan-headers-1.4.346.tar.gz::https://github.com/KhronosGroup/Vulkan-Headers/archive/v1.4.346.tar.gz"
        "vulkan-utility-libraries-1.4.346.tar.gz::https://github.com/KhronosGroup/Vulkan-Utility-Libraries/archive/v1.4.346.tar.gz"
        "implot-0.16.zip::https://github.com/epezent/implot/archive/refs/tags/v0.16.zip"
        "implot_0.16-1_patch.zip::https://wrapdb.mesonbuild.com/v2/implot_0.16-1/get_patch"
        )

sha256sums=(
            'SKIP'
            'SKIP'
            'c5fbc5dcab1d46064001c3b84d7a88812985cde7e0e9ced03f5677bec1ba502a'
            '1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b'
            'ae878e732330ea1048f90d7e117c40c0cd2a6fb8ae5492c7955818ce3aaade6c'
            "5bb77f5d7b460e255a9e51affc00d64354986b55cf577d8eab28529cad01fc80"
            "372eb525103ecb4e3a04d030b2a9778ebc67853bbdc82ce6747de3757d432ad9"
            "24f772c688f6b8a6e19d7efc10e4923a04a915f13d487b08b83553aa62ae1708"
            "1c6b1462066a5452fa50c1da1dd47fed841f28232972c82d778f2962936568c7"
            )

_build_args="-Dappend_libdir_mangohud=false -Dwith_xnvctrl=disabled -Dtests=disabled"

pkgver() {
  cd "$srcdir/mangohud"
  git describe --tags | sed -r 's/^v//;s/([^-]*-g)/r\1/;s/-/./g'
}

prepare() {
  cd "${srcdir}/mangohud"
  git submodule init
  git config submodule.modules/minhook.url "$srcdir/mangohud-minhook"
  git -c protocol.file.allow=always submodule update

  # meson subprojects
  ln -sv "$srcdir/imgui-1.91.6" subprojects
  cp -rvt "subprojects/imgui-1.91.6" -- "subprojects/packagefiles/imgui-1.91.6/"*
  ln -sv "$srcdir/spdlog-1.14.1" subprojects
  ln -sv "$srcdir/Vulkan-Headers-1.4.346" subprojects
  cp -rvt "subprojects/Vulkan-Headers-1.4.346" -- "subprojects/packagefiles/vulkan-headers/"*
  ln -sv "$srcdir/Vulkan-Utility-Libraries-1.4.346" subprojects
  cp -rvt "subprojects/Vulkan-Utility-Libraries-1.4.346" -- "subprojects/packagefiles/vulkan-utility-libraries/"*
  ln -sv "$srcdir/implot-0.16" subprojects
}

build() {
  arch-meson mangohud build64 \
    ${_build_args} -Dmangoapp=true -Dmangohudctl=true

  ninja -C build64
  export CC="${CC:-gcc} -m32"
  export CXX="${CXX:-g++} -m32"
  export PKG_CONFIG_PATH="/usr/lib32/pkgconfig:/usr/lib/i386-linux-gnu/pkgconfig:/usr/lib/pkgconfig:${PKG_CONFIG_PATH_32}"
  export LLVM_CONFIG="/usr/bin/llvm-config32"

  arch-meson mangohud build32 \
    --libdir=lib32 \
    -Dmangoapp=false \
    -Dmangohudctl=false \
    ${_build_args}

  ninja -C build32
}

package_mangohud() {
  provides=("mangohud")
  conflicts=('mangohud-common')
  DESTDIR="${pkgdir}" ninja -C build64 install
}

package_lib32-mangohud() {
  provides=("lib32-mangohud")
  DESTDIR="${pkgdir}" ninja -C build32 install
  rm -rf "$pkgdir/usr/bin"
  rm -rf "$pkgdir/usr/share"
  install -m644 -Dt "$pkgdir/usr/share/vulkan/implicit_layer.d" "$srcdir/build32/src/MangoHud.x86.json"
}
