# 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.0
pkgrel=4
pkgdesc="Generic pre- and post-processing platform for numerical simulation (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.salome-platform.org/downloads/current-version"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-tk")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=("http://files.salome-platform.org/Salome/other/${_realname}-${pkgver}.tar.gz"
        "hdf5-1.12.patch"
        "bin-dest.patch")
sha256sums=('847db5d6fbc9ce6924cb4aea86362812c9a5ef6b9684377e4dd6879627651fce'
            '6c0083bc1b52091357f039b73c9e528c225905568d5ee39008980fc9f6ad1682'
            '7ba63007a100408d7cdba106607eefcd0396f8984e84bf412e44324814cb1b04')

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

build() {
  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_BUILD_TYPE=Release \
    -DMEDFILE_BUILD_SHARED_LIBS=OFF \
    -DMEDFILE_BUILD_STATIC_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_FLAGS="-DH5_USE_110_API" \
    -DMEDFILE_BUILD_PYTHON=OFF \
    -DMEDFILE_BUILD_TESTS=OFF \
    -DMEDFILE_INSTALL_DOC=OFF \
    ../${_realname}-${pkgver}
  ninja
  
  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_BUILD_TYPE=Release \
    -DMEDFILE_BUILD_SHARED_LIBS=ON \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_FLAGS="-DH5_USE_110_API" \
    -DMEDFILE_BUILD_PYTHON=OFF \
    -DMEDFILE_BUILD_TESTS=OFF \
    -DMEDFILE_INSTALL_DOC=OFF \
    ../${_realname}-${pkgver}
  ninja
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} ninja install

  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} ninja install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
