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

_realname=armadillo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=15.2.3
pkgrel=1
pkgdesc="Fast C++ matrix library with syntax similar to MATLAB and Octave (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://arma.sourceforge.io/"
msys2_repository_url="https://gitlab.com/conradsnicta/armadillo-code"
msys2_references=(
  'aur: armadillo'
  'gentoo: sci-libs/armadillo'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-superlu")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
install=${_realname}-${MSYSTEM}.install
source=(https://sourceforge.net/projects/arma/files/${_realname}-${pkgver}.tar.xz
        0001-mingw-config-fix.patch
        0002-fix-pkgconfig-file.patch)
sha256sums=('0182d67d6949e4347a0bc62fc8c2793be7eb203c71f19edff93f8c45fd4a8190'
            'a2d0b9bbdc5a5a2006bf2a56bb2041784d576cdd5c60b9cef1b158cfb0468ae8'
            '830adb017e12c0e90671471dc6870a28ee21755a55e95280a04f73c2d65d665b')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

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

  apply_patch_with_msg \
    0001-mingw-config-fix.patch \
    0002-fix-pkgconfig-file.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DOPENBLAS_PROVIDES_LAPACK=ON \
    -DBUILD_SMOKE_TEST=ON \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

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

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

  # Prepend build directory with PATH to get armadillo DLL file
  PATH=$PWD:$PATH "${MINGW_PREFIX}"/bin/ctest.exe .
}

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

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/${_realname}
  install -m644 "${srcdir}/${_realname}-${pkgver}/"*{.html,.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"
  sed -e "s|${_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${pkgdir}${MINGW_PREFIX}/share/Armadillo/CMake/ArmadilloLibraryDepends.cmake"
}
