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

_realname=matio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.28
pkgrel=1
pkgdesc="C library for reading and writing MATLAB MAT files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: libmatio'
  "cpe: cpe:/a:matio_project:matio"
)
msys2_repository_url='https://github.com/tbeu/matio/'
url='https://sourceforge.net/projects/matio/'
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
source=(https://github.com/tbeu/matio/releases/download/v${pkgver}/matio-${pkgver}.tar.gz)
sha256sums=('9da698934a21569af058e6348564666f45029e6c2b0878ca0d8f9609bf77b8d8')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  # autoreconf to get updated libtool files for clang
  autoreconf -fiv
}

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

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

  make
}

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

  # 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 || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  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}/"
}
