# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=uvatlas
_rn=UVAtlas
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
# Version from CMakeLists.txt
pkgver=1.9.1
_tag=jul2025
pkgrel=1
pkgdesc="UVAtlas Isochart Atlas Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/Microsoft/UVAtlas'
msys2_changelog_url='https://github.com/microsoft/UVAtlas/blob/main/CHANGELOG.md'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-directx-headers"
         "${MINGW_PACKAGE_PREFIX}-directxmath"
         "${MINGW_PACKAGE_PREFIX}-directxmesh"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-spectra")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-directxtex")
source=("https://github.com/microsoft/UVAtlas/archive/${_tag}/${_realname}-${_tag}.tar.gz")
sha256sums=('156827c1d8029901b9bbf55ad7a1f9f5862e3895ce9e384a92b3bb55bcc55c81')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

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

  if [[ "${MSYSTEM_CARCH}" == "aarch64" ]]; then
    extra_config+=("-DDIRECTX_ARCH=arm64")
  fi

  #DBUILD_DX11 requires fxc.exe but that's not available.
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_TOOLS=ON \
      -DENABLE_USE_EIGEN=ON \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      ../${_rn}-${_tag}

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${_tag}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
