# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=VulkanSceneGraph
pkgbase=mingw-w64-${_realname,,}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname,,}"
         "${MINGW_PACKAGE_PREFIX}-${_realname,,}-docs")
pkgver=1.1.10
pkgrel=4
pkgdesc="Vulkan & C++17 based Scene Graph Project (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://www.vulkanscenegraph.org/'
msys2_repository_url='https://github.com/vsg-dev/VulkanSceneGraph'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-glslang"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-winpthreads"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-graphviz")
# The '0001-vsg-1.1.3-devendor-glslang.patch' patch
# copied from https://github.com/microsoft/vcpkg/pull/38294
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        '0002-vsg-1.1.3-fix-win32-window.patch'
        '0003-arm64-macro-clash.patch')
sha256sums=('b430132ba5454e0616ff5334a7cb9196c0e8f10a925c2106e80a78d6f24ae4b5'
            '2e6d8b0c2434a98eb67a70afb8643f109592dcea2e6afdbe2f6fe59e6783d792'
            '3f6c1b96edfc24b970287f540a252ba6685b94522a5e92fcfd8f3bc85f091960')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    0002-vsg-1.1.3-fix-win32-window.patch \
    0003-arm64-macro-clash.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -DVSG_SUPPORTS_ShaderCompiler=1 \
      -DGLSLANG_MIN_VERSION=15 \
      -DVSG_SUPPORTS_Windowing=1 \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}" --target docs
}

package_vulkanscenegraph() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname,,}/LICENSE.md"
}

package_vulkanscenegraph-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname,,}"
  cp -r "build-${MSYSTEM}/html" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname,,}/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
