# Maintainer: Alexander F. Rodseth <xyproto@archlinux.org>
# Contributor: Guilhem Saurel <guilhem@saurel.me>
# Contributor: Michel Zou
# Contributor: Oliver Goethel
# Contributor: Rafal Brzegowy <3rav@hotmail.com>

_realname=med
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=4.1.1
pkgrel=9
pkgdesc="Generic pre- and post-processing platform for numerical simulation (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.salome-platform.org/"
license=('spdx:LGPL-3.0-or-later AND GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-hdf5"
         $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-tk")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* && ${MSYSTEM} != *64 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-fc")
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("http://files.salome-platform.org/Salome/other/${_realname}-${pkgver}.tar.gz"
        "hdf5-1.14.patch"
        "bin-dest.patch")
sha256sums=('dc2b5d54ebf0666e3ff2e974041d2ab0da906061323537023ab165d573389dd0'
            'd2d711adc32083041c85906e84592873b1ab6c54b02670b57678a33edce10fa9'
            '75f078af79b58d41ccc6f4a2ccf1a0ab4aa50365600d279ef2466456a3bfa67f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}_SRC"
  apply_patch_with_msg \
    hdf5-1.14.patch \
    bin-dest.patch
  sed -i 's/if H5_VERS_MINOR > 10/if 0/g' src/ci/MEDfileCompatibility.c
}

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

  CFLAGS+=" -DH5_USE_110_API -Wno-implicit-function-declaration"

  # Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    "${_extra_config[@]}" \
    -DMEDFILE_BUILD_SHARED_LIBS=OFF \
    -DMEDFILE_BUILD_STATIC_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DMEDFILE_BUILD_PYTHON=OFF \
    -DMEDFILE_BUILD_TESTS=OFF \
    -DMEDFILE_INSTALL_DOC=OFF \
    ../${_realname}-${pkgver}_SRC
  "${MINGW_PREFIX}"/bin/cmake --build .

  # Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    "${_extra_config[@]}" \
    -DMEDFILE_BUILD_SHARED_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DMEDFILE_BUILD_PYTHON=OFF \
    -DMEDFILE_BUILD_TESTS=OFF \
    -DMEDFILE_INSTALL_DOC=OFF \
    ../${_realname}-${pkgver}_SRC
  "${MINGW_PREFIX}"/bin/cmake --build .
}

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

  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

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