# Maintainer: Hurcan Solter <hsolter@gmail.com>

_realname=armadillo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.5.0
pkgrel=1
pkgdesc="C++ linear algebra library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://arma.sourceforge.io/"
license=(MPL2)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
install=${_realname}-${CARCH}.install
source=(https://sourceforge.net/projects/arma/files/${_realname}-${pkgver}.tar.xz
        0001-mingw-config-fix.patch)
sha256sums=('ea990c34dc6d70d7c95b4354d9f3b0819bde257dbb67796348e91e196082cb87'
            '772719e60eb2970ecb37844382dbcb6d0439f949c5080f9865798115640b612a')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-mingw-config-fix.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=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DLAPACK_NAMES=openblas \
    ../${_realname}-${pkgver}

  make
}

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

  install -d ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}
  install -m644 "${srcdir}/${_realname}-${pkgver}/"*{.html,.png,.pdf,README.md} \
    "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"

  # Remove builders local installation path
  local _prefix_win=$(cygpath -m ${MINGW_PREFIX})
  sed -s "s|${_prefix_win}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/include/armadillo_bits/config.hpp"
}
