# 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=2.0
pkgrel=1
_tag=46d5887030419861cd68bf6b8f7c96c9f18c551a
pkgdesc="Complex error functions, Dawson, Faddeeva, and Voigt function (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://jugit.fz-juelich.de/mlz/libcerf'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
source=("https://jugit.fz-juelich.de/mlz/libcerf/-/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "libcerf-fix-installation-dirs.patch")
sha256sums=('90f4c4d1051b5d881b36b01f8d7fcd1a7e10947a979ecad0ab9710551abd8ad8'
            '5bc4e6faf093703209c94cf22d9fa9b2400da74622220076a8bc270c3a5eb400')

prepare() {
  cd "${srcdir}/${_realname}-v${pkgver}-${_tag}"
  patch -Np1 -i "${srcdir}/libcerf-fix-installation-dirs.patch"
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=ON \
      -DLIB_INSTALL=ON \
      ../${_realname}-v${pkgver}-${_tag}

  make
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check || true
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

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