# $Id$
# Maintainer (ArchLinux): Alexander Rødseth <rodseth@gmail.com>
# Contributor (ArchLinux): William Rea <sillywilly@gmail.com>
# Contributor (ArchLinux): Stefan Husmann <stefan-husmann@t-online.de>
# Maintainer (MSYS2): Ray Donnelly <mingw.android@gmail.com>

_realname=arpack
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}64")
         $([[ "${CARCH}" != "x86_64" ]] || echo "${MINGW_PACKAGE_PREFIX}-p${_realname}"))
pkgver=3.9.1
pkgrel=5
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="Fortran77 subroutines designed to solve large scale eigenvalue problems (mingw-w64)"
url='https://forge.scilab.org/index.php/p/arpack-ng/'
license=('spdx:BSD-3-Clause')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ "${CARCH}" != "x86_64" ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi")
             "${MINGW_PACKAGE_PREFIX}-openblas"
             $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-openblas64"))
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-ng")
options=('!buildflags')
source=(https://github.com/opencollab/arpack-ng/archive/${pkgver}/${_realname}-${pkgver}.tar.gz)
sha256sums=('f6641deb07fa69165b7815de9008af3ea47eb39b2bb97521fbf74c97aba6e844')

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

_build_arpack() {
  _idx_opt=$1

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_build_type} \
      -DICB=ON \
      -DTESTS=OFF \
      ${_idx_opt} \
      ../${_realname}-ng-${pkgver}

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

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

  if [ "${CARCH}" == "x86_64" ]; then
    _parpack_option="-DMPI=ON"
  fi

  msg2 "Build static ARPACK with 32-bit indexing"
  _build_arpack "-DBUILD_SHARED_LIBS=OFF ${_parpack_option}"

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared-32" && cd "${srcdir}/build-${MSYSTEM}-shared-32"

  msg2 "Build shared ARPACK with 32-bit indexing"
  _build_arpack "${_parpack_option}"


  if [ "${CARCH}" != "i686" ]; then
    mkdir -p "${srcdir}/build-${MSYSTEM}-static-64" && cd "${srcdir}/build-${MSYSTEM}-static-64"

    msg2 "Build static ARPACK with 64-bit indexing"
    _build_arpack "-DINTERFACE64=1 -DITF64SUFFIX=_64 -DBUILD_SHARED_LIBS=OFF -DBLAS_LIBRARIES=openblas_64"

    mkdir -p "${srcdir}/build-${MSYSTEM}-shared-64" && cd "${srcdir}/build-${MSYSTEM}-shared-64"

    msg2 "Build shared ARPACK with 64-bit indexing"
    _build_arpack "-DINTERFACE64=1 -DITF64SUFFIX=_64 -DBLAS_LIBRARIES=openblas_64"
  fi
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-static-32"
  msg2 "Static check with 32-bit indexing"
  ${MINGW_PREFIX}/bin/cmake -DTESTS=ON ../${_realname}-ng-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build . --target test || true

  cd "${srcdir}/build-${MSYSTEM}-shared-32"
  msg2 "Shared check with 32-bit indexing"
  ${MINGW_PREFIX}/bin/cmake -DTESTS=ON ../${_realname}-ng-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build . --target test || true

  if [ "${CARCH}" != "i686" ]; then
    cd "${srcdir}/build-${MSYSTEM}-static-64"
    msg2 "Static check with 64-bit indexing"
    ${MINGW_PREFIX}/bin/cmake -DTESTS=ON ../${_realname}-ng-${pkgver}
    ${MINGW_PREFIX}/bin/cmake --build . --target test || true

    cd "${srcdir}/build-${MSYSTEM}-shared-64"
    msg2 "Shared check with 64-bit indexing"
    ${MINGW_PREFIX}/bin/cmake -DTESTS=ON ../${_realname}-ng-${pkgver}
    ${MINGW_PREFIX}/bin/cmake --build . --target test || true
  fi
}

package_arpack() {
  pkgdesc="Fortran77 subroutines designed to solve large scale eigenvalue problems (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-openblas")

  # Static Install
  cd "${srcdir}/build-${MSYSTEM}-static-32"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared-32"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/${_realname}-ng-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

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

  if [ "${CARCH}" == "x86_64" ]; then
    # separate files for PARPACK
    mkdir -p "${srcdir}"/${MSYSTEM}-PARPACK/bin
    mkdir -p "${srcdir}"/${MSYSTEM}-PARPACK/include/arpack
    mkdir -p "${srcdir}"/${MSYSTEM}-PARPACK/lib/pkgconfig
    mv "${pkgdir}"${MINGW_PREFIX}/bin/libparpack* "${srcdir}"/${MSYSTEM}-PARPACK/bin/
    mv "${pkgdir}"${MINGW_PREFIX}/include/arpack/parpack* "${srcdir}"/${MSYSTEM}-PARPACK/include/arpack/
    mv "${pkgdir}"${MINGW_PREFIX}/lib/libparpack* "${srcdir}"/${MSYSTEM}-PARPACK/lib/
    mv "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/parpack* "${srcdir}"/${MSYSTEM}-PARPACK/lib/pkgconfig/
  fi
}

package_parpack() {
  pkgdesc="Fortran77 subroutines designed to solve large scale eigenvalue problems with MPI (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-arpack"
            "${MINGW_PACKAGE_PREFIX}-openblas")
  provides=()
  conflicts=()
  replaces=()

  mkdir -p "${pkgdir}"${MINGW_PREFIX}
  cp -rf "${srcdir}"/${MSYSTEM}-PARPACK/* "${pkgdir}"${MINGW_PREFIX}
}

package_arpack64() {
  pkgdesc="Fortran77 subroutines designed to solve large scale eigenvalue problems with 64-bit indexing (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-openblas64")
  provides=()
  conflicts=()
  replaces=()

  # Static Install
  cd "${srcdir}/build-${MSYSTEM}-static-64"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared-64"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/${_realname}-ng-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}64/COPYING

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

}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
