# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=matio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.21
pkgrel=1
pkgdesc="matio is a C library for reading and writing MATLAB MAT files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://sourceforge.net/projects/matio/'
license=('Simplified BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=(https://github.com/tbeu/matio/releases/download/v${pkgver}/matio-${pkgver}.tar.gz)
sha256sums=('21809177e55839e7c94dada744ee55c1dea7d757ddaab89605776d50122fb065')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  cp -rf "${_realname}-${pkgver}" "build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  chmod +x ./configure
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --with-hdf5

  make ${MAKEFLAGS}
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  # it is not clear why the following workaround is needed to
  # execute tests, but after touching the testsuite.at, the
  # tests work, when otherwise they fail:
  touch ./test/testsuite.at

  make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" -j1 install

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
