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

pkgname=('mangohud' 'lib32-mangohud')
pkgver=0.6.8.r140.g1b3f8b2
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' 'glew' 'glfw-x11' 'python-numpy' 'python-matplotlib')
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.89.9.tar.gz::https://github.com/ocornut/imgui/archive/refs/tags/v1.89.9.tar.gz"
        "imgui_1.89.9-1_patch.zip::https://wrapdb.mesonbuild.com/v2/imgui_1.89.9-1/get_patch"
        "spdlog-1.13.0.tar.gz::https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.tar.gz"
        "spdlog_1.13.0-1_patch.zip::https://wrapdb.mesonbuild.com/v2/spdlog_1.13.0-1/get_patch"
        "nlohmann_json-3.10.5.zip::https://github.com/nlohmann/json/releases/download/v3.10.5/include.zip"
        "vulkan-headers-1.2.158.tar.gz::https://github.com/KhronosGroup/Vulkan-Headers/archive/v1.2.158.tar.gz"
        "vulkan-headers-1.2.158-2-wrap.zip::https://wrapdb.mesonbuild.com/v2/vulkan-headers_1.2.158-2/get_patch"
        "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'
            '1acc27a778b71d859878121a3f7b287cd81c29d720893d2b2bf74455bf9d52d6'
            '9b21290c597d76bf8d4eeb3f9ffa024b11d9ea6c61e91d648ccc90b42843d584'
            '534f2ee1a4dcbeb22249856edfb2be76a1cf4f708a20b0ac2ed090ee24cfdbc9'
            '556b539cf582a46673ede4202ac037b891328dd5ea76862ffe05b060fc4f4775'
            'b94997df68856753b72f0d7a3703b7d484d4745c567f3584ef97c96c25a5798e'
            "53361271cfe274df8782e1e47bdc9e61b7af432ba30acbfe31723f9df2c257f3"
            "860358cf5e73f458cd1e88f8c38116d123ab421d5ce2e4129ec38eaedd820e17"
            "24f772c688f6b8a6e19d7efc10e4923a04a915f13d487b08b83553aa62ae1708"
            "1c6b1462066a5452fa50c1da1dd47fed841f28232972c82d778f2962936568c7"
            )

_build_args="-Dappend_libdir_mangohud=false -Dwith_xnvctrl=disabled -Dmangoapp_layer=true -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.89.9" subprojects
  ln -sv "$srcdir/spdlog-1.13.0" subprojects
  mkdir  subprojects/nlohmann_json-3.10.5
  ln -sv "$srcdir/include" subprojects/nlohmann_json-3.10.5/
  ln -sv "$srcdir/single_include" subprojects/nlohmann_json-3.10.5/
  ln -sv "$srcdir/LICENSE.MIT" subprojects/nlohmann_json-3.10.5/
  ln -sv "$srcdir/meson.build" subprojects/nlohmann_json-3.10.5/
  ln -sv "$srcdir/Vulkan-Headers-1.2.158" subprojects
  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"
}
