# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=mctc-lib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.3.2
pkgrel=2
pkgdesc="Modular computation tool chain library. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://grimme-lab.github.io/mctc-lib/'
msys2_repository_url='https://github.com/grimme-lab/mctc-lib'
license=('spdx:Apache-2.0')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         #"${MINGW_PACKAGE_PREFIX}-json-fortran"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/grimme-lab/mctc-lib/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-install-dll.patch"
        "002-fix-build-with-flang.patch")
sha256sums=('b18b06f80e6274b353dd091c12b3a83217033ce0bd80471b54cf486cc60c0251'
            '810bf81b5f26d605b7b833b7d7be102943506833558e83a9b8c2052ed566f4c1'
            'f4db7a7443735fd795fe81fb5629b097eec1c0a05e0b32bd97df5155a6bf0222')

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

  patch -Np1 -i "${srcdir}"/001-fix-install-dll.patch
  patch -Np1 -i "${srcdir}"/002-fix-build-with-flang.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_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DWITH_OpenMP=ON \
      -DWITH_JSON=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

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

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