# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=libcerf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.3
pkgrel=1
epoch=1
pkgdesc="Complex error functions, Dawson, Faddeeva, and Voigt function (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://jugit.fz-juelich.de/mlz/libcerf'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
            # "${MINGW_PACKAGE_PREFIX}-ninja") failed to run pod2man command
source=("https://jugit.fz-juelich.de/mlz/libcerf/-/archive/v${pkgver}/${_realname}-v${pkgver}.tar.gz"
        "libcerf-fix-installation-dirs.patch"
        "002-enable-CERF_C-on-mingw.patch")
sha256sums=('ea2910085448e269b5f0f0fabe51ffd846ac68881904be99669d94dc0cc09765'
            '156077eae465a6e69e05c5270bbb499cf35977223702185f292c9ed9670e5108'
            '0bce12c492237ee2c2552325fd84e434acb4196a6695f70c08405f59203f9f80')

prepare() {
  cd "${srcdir}/${_realname}-v${pkgver}"
  patch -Np1 -i "${srcdir}/libcerf-fix-installation-dirs.patch"
  patch -Np1 -i "${srcdir}/002-enable-CERF_C-on-mingw.patch"
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DPEDANTIC=OFF \
      "${_extra_config[@]}" \
      ../${_realname}-v${pkgver}

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

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  PATH="${srcdir}"/build-${MSYSTEM}/lib:"${srcdir}"/build-${MSYSTEM}/libcpp:${PATH} \
  ${MINGW_PREFIX}/bin/ctest || true
}

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

  # License
  install -Dm644 "${srcdir}"/${_realname}-v${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
