_realname=octave
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=8.4.0
pkgrel=5
pkgdesc="GNU Octave: Interactive programming environment for numerical computations (mingw-w64)"
url="https://www.octave.org"
msys2_repository_url="https://github.com/gnu-octave/octave"
msys2_references=(
  'archlinux: octave'
)
license=('spdx:GPL-3.0-or-later')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-glpk")
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-sundials"
         "${MINGW_PACKAGE_PREFIX}-qrupdate"
         "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-gnuplot")
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-fltk"
             "${MINGW_PACKAGE_PREFIX}-portaudio"
             "${MINGW_PACKAGE_PREFIX}-rapidjson")
checkdepends=("unzip"
              "zip")
optdepends=('texinfo: for help-support in Octave'
            "unzip: for decompressing .zip archives"
            "zip: for compressing .zip archives"
            "${MINGW_PACKAGE_PREFIX}-epstool: eps output with tight bounding box"
            "${MINGW_PACKAGE_PREFIX}-fltk: alternative plotting and dialogs"
            "${MINGW_PACKAGE_PREFIX}-gnuplot: alternative plotting"
            "${MINGW_PACKAGE_PREFIX}-portaudio: audio support")
source=(https://ftp.gnu.org/gnu/octave/octave-$pkgver.tar.xz{,.sig}
        "0001-no-namespace-in-extern-C.patch::https://hg.savannah.gnu.org/hgweb/octave/raw-rev/123c5d78fc9d"
        "0002-mk-doc-cache-path.patch"
        "0003-no-community-support.patch"
        "0005-makeinfo-perl.patch")
validpgpkeys=('DBD9C84E39FE1AAE99F04446B05F05B75D36644B')  # John W. Eaton
sha256sums=('6f9ad73a3ee4291b6341d6c0f5e5c85d6e0310376e4991b959a6d340b3ffa8a8'
            'SKIP'
            '82909b2b878aff25b276e73def14a14c2756ae3688275df154a425105e3abc33'
            'aa5bd559d9774abc0f0c930a606762d1e452cc90278d16d8ae83561c0cae3bf8'
            '14f6afe84529982041e20653a8c146630f598493abbdf5e8d52f0bee80326a5b'
            '3f6756b39547d996955762b71ce6845a8ebfde6bad92a3a1a6d72fd43df5973d')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

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

  apply_patch_with_msg \
    0002-mk-doc-cache-path.patch \
    0005-makeinfo-perl.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      0001-no-namespace-in-extern-C.patch \
      0003-no-community-support.patch

    # We need the patched libtool with Flang specific changes.
    autoreconf -fiv
  fi

}

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

  declare -a _extra_config
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_config+=(# Clang seems to require a different set of dllexport
                    # attributes than GCC.  For the time being, export all
                    # symbols (including those from gnulib and other static
                    # libraries linked into the Octave libraries).  Doing that
                    # might cause issues in some cases.  But solving this
                    # without breaking GCC support might be difficult.
                    "--disable-lib-visibility-flags"
                    # Libtool still needs some hints how to handle LLVM Flang:
                    "--enable-fortran-calling-convention=gfortran"
                    "ac_cv_f77_compiler_gnu=yes"
                    "lt_cv_prog_gnu_ld=yes"
                    "F77=${MINGW_PREFIX}/bin/flang"
                    # LLVM 16 seems to have switched defaults to C++17.
                    # Apparently, some STL functions that are deprecated in that
                    # version of the standard are used.  For the time being,
                    # build with the C++14 feature set (and GNU extensions).
                    "CXX=${MINGW_PREFIX}/bin/clang++ -std=gnu++14")
    # The headers from some dependent libraries (graphicsmagick++?) cause a
    # torrent of warnings about deprecated declarations.  Sometimes to the
    # point that the build crashes with garbled characters in the terminal
    # output for some reason.  Just calling `make all -j2` again in the build
    # tree lets the compilation finish successfully.
    # Disable those warnings as a work-around.
    CXXFLAGS+=" -Wno-deprecated-declarations"
  fi

  # The configure step sometimes hangs for MINGW32 for currently unknown reasons.
  # Add a timeout of 30 minutes (well over what should be needed) to unblock
  # the build system in that case.
  timeout 30m \
  ../${_realname}-${pkgver}/configure \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/lib \
    --sbindir=${MINGW_PREFIX}/bin \
    --enable-shared \
    --disable-static \
    --enable-relocate-all \
    --enable-link-all-dependencies \
    "${_extra_config[@]}" \
    gl_cv_have_weak=no \
    octave_cv_fftw3_threads_lib="-lfftw3_omp" \
    octave_cv_fftw3f_threads_lib="-lfftw3f_omp" \
    JAVA_HOME=""

  PERL5SHELL="bash -l -c" make all
}

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

package() {
  make -C "build-${MSYSTEM}" DESTDIR="${pkgdir}" install

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
