# Maintainer: Francis Giraldeau <francis.giraldeau@nrc-cnrc.gc.ca>

_realname=cgns
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.4.0
pkgrel=2
pkgdesc="CFD General Notation System library and tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://cgns.github.io"
license=('spdx:Zlib')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-hdf5")
source=(https://github.com/CGNS/CGNS/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz)
sha256sums=('3b0615d1e6b566aa8772616ba5fd9ca4eca1a600720e36eadd914be348925fe2')

build() {
  mkdir -p build-${MSYSTEM} && cd 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 [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    extra_config+=("-DCGNS_ENABLE_FORTRAN=OFF")
  else
    extra_config+=("-DCGNS_ENABLE_FORTRAN=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    ${extra_config[@]} \
    -DCGNS_ENABLE_HDF5=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}

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

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

  # license
  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/license.txt \
  "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
