# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=hdf5
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_ver=1.12.0
patch=
pkgver=${_ver}${patch//-/.}
pkgrel=4
pkgdesc="General purpose library and file format for storing scientific data (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=("custom")
url="https://www.hdfgroup.org/HDF5/"
source=("https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-${_ver%.*}/hdf5-${_ver}${patch}/src/hdf5-${_ver}${patch}.tar.bz2"
        "hdf5-default-import-suffix.patch"
        "hdf5-proper-library-names-mingw.patch"
        "hdf5-fix-find-szip.patch"
        "hdf5-build-warnings.patch"
        "hdf-pkgconfig-without-versioning.patch")
sha256sums=('97906268640a6e9ce0cde703d5a71c9ac3092eded729591279bf2e3ca9765f61'
            'fb265cd2b0181a77bc1322362cac03870d21016809f01b0891b7b43efffcf80f'
            '7b5595ee9903e14f147f7b4615648cad6a0f5a76f8299461ebe91d739e750476'
            '2f4438d780c897e06481231688a61251738ee84550dde7e0153a3d09b9ede1f7'
            '92322234b82fb47ee1b2a9998955f3e5d944fd0290a7b0016b6c037671571a1e'
            '76995487009646600c8953e7c568d9050f084924f9a3a3ad7c784d1002d2c5fc')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-libaec"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "patch")
options=('staticlibs' 'strip')

prepare() {
  cd "${srcdir}/${_realname}-${_ver}${patch}"
  patch -p1 -i "${srcdir}/hdf5-default-import-suffix.patch"
  patch -p1 -i "${srcdir}/hdf5-proper-library-names-mingw.patch"
  patch -p1 -i "${srcdir}/hdf5-fix-find-szip.patch"
  patch -p1 -i "${srcdir}/hdf5-build-warnings.patch"
  patch -p1 -i "${srcdir}/hdf-pkgconfig-without-versioning.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  SZIP_INSTALL=${MINGW_PREFIX} \
    ${MINGW_PREFIX}/bin/cmake \
      -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_SKIP_RPATH=ON \
      -DHDF5_BUILD_HL_LIB=ON \
      -DHDF5_BUILD_CPP_LIB=ON \
      -DHDF5_BUILD_FORTRAN=ON \
      -DHDF5_BUILD_TOOLS=ON \
      -DHDF5_ENABLE_DEPRECATED_SYMBOLS=ON \
      -DHDF5_ENABLE_SZIP_SUPPORT=ON \
      -DHDF5_ENABLE_Z_LIB_SUPPORT=ON \
      -DHDF5_INSTALL_CMAKE_DIR="lib/cmake" \
      -DHDF5_INSTALL_DATA_DIR="share/doc/hdf5" \
      ../${_realname}-${_ver}${patch}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  mkdir -p ${srcdir}/build-${MINGW_CHOST}/bin/static/Release/
  mkdir -p ${srcdir}/build-${MINGW_CHOST}/bin/shared/Release/
  cp ${srcdir}/build-${MINGW_CHOST}/bin/static/*.mod ${srcdir}/build-${MINGW_CHOST}/bin/static/Release
  cp ${srcdir}/build-${MINGW_CHOST}/bin/shared/*.mod ${srcdir}/build-${MINGW_CHOST}/bin/shared/Release
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

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

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/libhdf5.settings
}
