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

_realname=quantum-espresso
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.5
pkgrel=2
#_d3q_commit=de4718351e7bbb9d1d12aad2b7ca232d06775b83
_devxlib_commit=a6b89ef77b1ceda48e967921f1f5488d2df9226d
_fox_commit=3453648e6837658b747b895bb7bef4b1ed2eac40
_pw2qmcpack_commit=f72ab25fa4ea755c1b4b230ae8074b47d5509c70
#_qe_gipaw_commit=490046d300ec23444504bfad0562baa1c71f9345
pkgdesc="opEn-Source Package for Research in Electronic Structure, Simulation, and Optimization (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='http://www.quantum-espresso.org/'
msys2_repository_url="https://gitlab.com/QEF/q-e"
msys2_references=(
  'aur: quantum-espresso'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libmbd"
         #"${MINGW_PACKAGE_PREFIX}-libxc"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-wannier90"
             #"${MINGW_PACKAGE_PREFIX}-environ"
             "git")
source=("https://gitlab.com/QEF/q-e/-/archive/qe-${pkgver}/q-e-qe-${pkgver}.tar.bz2"
        #"d3q::git+https://github.com/anharmonic/d3q#commit=${_d3q_commit}"
        "devxlib::git+https://gitlab.com/max-centre/components/devicexlib#commit=${_devxlib_commit}"
        "fox::git+https://github.com/pietrodelugas/fox#commit=${_fox_commit}"
        "pw2qmcpack::git+https://github.com/QMCPACK/pw2qmcpack#commit=${_pw2qmcpack_commit}"
        #"qe-gipaw::git+https://github.com/dceresoli/qe-gipaw#commit=${_qe_gipaw_commit}"
        "001-use-srand-instead-of-srandom.patch"
        "002-fix-build-devxlib-on-mingw.patch"
        "003-fix-install-c-libraries.patch"
        "004-fix-executable-suffix-on-mingw.patch")
sha256sums=('aba3ab4dd9e9ebc6611c6f3017b98a8f0e2be9455e356d85df84f9a36b844d98'
            'ef02c13a1ea73f38f0b81edd281c991c25bf045307ceb305f5c0f96b743d1556'
            '8625181b53f2b616a0f704507b4f2381f7b9fabe02078dfb74ab41ee11f5c260'
            'f0f5ebece3f0142e0d0b476db9547d8501de80259bc6409e9b108b887e4cc058'
            '2e18a13c194328516176c2bc2429ac230c9fc585381aa238e586b6034a7df0cb'
            'faaf147678e0c79a6678c5463dd5a466769257968275ef562f40ad1a0c2aacc6'
            '30f2fd93980f09baf07c810df1bcbd60594e54a156c5dae49a1888cf28619421'
            'a729ab4c6fc9e47baa05d4383568e271a5560c981e4bf0d9cb56a499c7d6e435')
noextract=("q-e-qe-${pkgver}.tar.bz2")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

  for _ext_prj in devxlib fox pw2qmcpack; do
    mv ${_ext_prj} q-e-qe-${pkgver}/external/
  done

  cd "${srcdir}"/q-e-qe-${pkgver}
  apply_patch_with_msg \
    001-use-srand-instead-of-srandom.patch \
    002-fix-build-devxlib-on-mingw.patch \
    003-fix-install-c-libraries.patch \
    004-fix-executable-suffix-on-mingw.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

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export FFLAGS=" -Wno-missing-include-dirs -w"
  fi

  CFLAGS+=" -Wno-incompatible-pointer-types" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DQE_ENABLE_FOX=ON \
      -DQE_ENABLE_HDF5=ON \
      -DQE_ENABLE_OPENMP=ON \
      -DQE_ENABLE_ENVIRON=OFF \
      -DQE_ENABLE_LIBXC=OFF \
      -DQE_ENABLE_MPI=OFF \
      -DQE_ENABLE_TEST=OFF \
      -DQE_ENABLE_PLUGINS="pw2qmcpack;legacy" \
      -DQE_DEVICEXLIB_INTERNAL=ON \
      -DQE_FOX_INTERNAL=ON \
      -DQE_MBD_INTERNAL=OFF \
      -DQE_WANNIER90_INTERNAL=OFF \
      -DQE_FFTW_VENDOR="FFTW3" \
      -DBLA_VENDOR=OpenBLAS \
      -S q-e-qe-${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}"/q-e-qe-${pkgver}/License \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/qe/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Rename cp to qe_cp, to not be used instead of /usr/bin/cp to copy files
  mv "${pkgdir}${MINGW_PREFIX}"/bin/{cp,qe_cp}.exe
}
