# Maintainer: Markus Mützel <markus.muetzel@gmx.de>

_realname=octave
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=9.2.0
pkgrel=3
pkgdesc="GNU Octave: Interactive programming environment for numerical computations (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
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')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-arpack"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         "${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-qrupdate"
         $([[ ${CARCH} == i686 ]] && echo \
           "${MINGW_PACKAGE_PREFIX}-qscintilla-qt5" \
           "${MINGW_PACKAGE_PREFIX}-qt5-base" \
           "${MINGW_PACKAGE_PREFIX}-qt5-tools" || echo \
           "${MINGW_PACKAGE_PREFIX}-qscintilla-qt6" \
           "${MINGW_PACKAGE_PREFIX}-qt6-base" \
           "${MINGW_PACKAGE_PREFIX}-qt6-tools")
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-sundials"
         "${MINGW_PACKAGE_PREFIX}-texinfo"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-gnuplot"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-fltk"
             "${MINGW_PACKAGE_PREFIX}-portaudio"
             "${MINGW_PACKAGE_PREFIX}-rapidjson")
checkdepends=("unzip"
              "zip")
optdepends=("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}
        "0002-mk-doc-cache-path.patch"
        "0003-no-community-support.patch")
validpgpkeys=('DBD9C84E39FE1AAE99F04446B05F05B75D36644B')  # John W. Eaton
sha256sums=('21417afb579105b035cac0bea09201522e384893ae90a781b8727efa32765807'
            'SKIP'
            'aa5bd559d9774abc0f0c930a606762d1e452cc90278d16d8ae83561c0cae3bf8'
            'e53af21ad087e10a2906506589aab89ab8b43f4e3e4994b4c28e4d8ae83639ad')

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

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      0003-no-community-support.patch
  fi

  autoreconf -fiv
}

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

  declare -a _extra_config
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    # Libtool still needs some hints how to handle LLVM Flang:
    _extra_config+=("--enable-fortran-calling-convention=gfortran"
                    "ac_cv_f77_compiler_gnu=yes"
                    "lt_cv_prog_gnu_ld=yes"
                    "F77=${MINGW_PREFIX}/bin/flang")
    # 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.  Same issue with dllimport attributes at
    # inlined functions in Qt6 headers. 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 -Wno-ignored-attributes"
  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 \
    --prefix=${MINGW_PREFIX} \
    --enable-shared \
    --disable-static \
    --enable-relocate-all \
    "${_extra_config[@]}" \
    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

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING

  # 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
}
