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

_realname=libxc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.0.0
pkgrel=1
pkgdesc="Library of exchange-correlation functionals for density-functional theory. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://libxc.gitlab.io'
msys2_repository_url="https://gitlab.com/libxc/libxc"
msys2_references=(
  'aur: libxc'
)
license=('spdx:MPL-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: Python bindings")
if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
  optdepends+=("${MINGW_PACKAGE_PREFIX}-gcc-libgfortran: Fortran bindings")
fi
source=("https://gitlab.com/libxc/libxc/-/archive/${pkgver}/${_realname}-${pkgver}.tar.bz2")
sha256sums=('e9ae69f8966d8de6b7585abd9fab588794ada1fab8f689337959a35abbf9527d')
noextract=("${_realname}-${pkgver}.tar.bz2")

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.bz2..."
  tar -xjf ${_realname}-${pkgver}.tar.bz2 || true

  cd ${_realname}-${pkgver}
}

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=;-DNAMESPACE_INSTALL_INCLUDEDIR=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DNAMESPACE_INSTALL_INCLUDEDIR="/libxc" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DENABLE_GENERIC=ON \
      -DENABLE_FORTRAN=ON \
      -DENABLE_PYTHON=ON \
      -DENABLE_XHOST=OFF \
      -DBUILD_TESTING=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}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}"${MINGW_PREFIX}
}
