# 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=5.0.0
pkgrel=2
pkgdesc="Generic pre- and post-processing platform for numerical simulation (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/chennes/med"
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"))
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/chennes/med/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "hdf5-1.14.patch"
        "gcc-15.patch"
        "bin-dest.patch")
sha256sums=('8701f142087b87e8b74958fd0432498eadf28011b20ad05cf56bf911be081888'
            '64e6e35d0f2bf71d9742b90759ab1e1ff579240790143a91bec31a0b28c23494'
            '8067c81ee29df85817e78963e64ac45e5118ace7338593a3033cf1f3beb8cf0e'
            '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}"
  apply_patch_with_msg \
    hdf5-1.14.patch \
    gcc-15.patch \
    bin-dest.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

  CFLAGS+=" -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}
  "${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}
  "${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}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LESSER" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
}
