#-*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           compilers  1.0
PortGroup           muniversal 1.0
PortGroup           legacysupport 1.1
PortGroup           linear_algebra 1.0

name                octave
version             10.3.0
# package_version has to be kept in sync with the octave PortGroup
set package_version 10.x.x
revision            0

categories          math science
license             GPL-3+
maintainers         {mcalhoun @MarcusCalhoun-Lopez} {mps @Schamschula} openmaintainer
description         a high-level language for numerical computations
long_description    \
    GNU Octave is a high-level language, primarily intended for numerical \
    computations. It provides a convenient command-line interface for \
    solving linear and nonlinear problems numerically.

homepage            https://www.gnu.org/software/octave

master_sites        gnu:octave

checksums           rmd160  d98af721da65637f85463bc4f9a8372fd7797eea \
                    sha256  2fcb38dc062e440f1e06c069bbca840ed46dcc8f983e473e1558fcc38384ee6b \
                    size    38137120

# see https://lists.gnu.org/archive/html/octave-maintainers/2016-05/msg00286.html
compiler.cxx_standard \
                    2017

# if there is an error bulding octave package, avoid
#    error: structure has no member 'dir'
patchfiles-append patch-better_error.diff

# bsdtar and tar have different outputs
# starting in Mac OS X Snow Leopard (10.6), tar is an alias of bsdtar
# this patch breaks packages builds on 10.6 ppc, so exclude it there
if { ${os.platform} eq "darwin" && ${os.major} >= 10 && ${os.arch} ne "powerpc" } {
    patchfiles-append patch-bsdtar.diff
}

# facilitate universal builds of packages
patchfiles-append patch-configure_make.m.diff

# see https://octave.discourse.group/t/rpath-issues-with-octave-executables-with-macports/6342/30
patchfiles-append patch-configure_rpath.diff

if {${os.platform} eq "darwin"} {
    post-patch {
        reinplace "s|%PREFIX%|${prefix}|" \
            ${worksrcpath}/configure.ac
    }
}

# allow mkoctfile to recognize and pass along -arch compiler switch
patchfiles-append patch-arch_arg.diff

# see https://savannah.gnu.org/bugs/?50025
# fails to apply: patchfiles-append patch-crash_on_exit.diff

# octave compiles in the hostname value
# it is used to determine installation locations
#    of architecture-dependent package files
# this means the build is dependent on the minor number of the OS
# the following patch attempts to prevent this
patchfiles-append patch-versions.diff

# reports of `__opengl_info__` not working the first time
patchfiles-append patch-__opengl_info__.diff

# see https://trac.macports.org/ticket/57288
patchfiles-append patch-run-octave.diff

# see https://savannah.gnu.org/patch/index.php?9806
set java_version 1.8
patchfiles-append patch-java_version.diff
post-patch {
    reinplace \
        "s|__MACPORTS_JAVA_VERSION__|${java_version}|g" \
        ${worksrcpath}/Makefile.in
}

# https://trac.macports.org/ticket/69575
legacysupport.redirect_bins mkoctfile-${version} octave-${version} octave-cli-${version} octave-config-${version}

if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
    # Patch the script which runs at destroot:
    patchfiles-append patch-run-octave-libstdcpp.diff

    post-patch {
        reinplace "s|%PREFIX%|${prefix}|" ${worksrcpath}/run-octave.in
    }

    # Patch scripts installed by LegacySupport so that they call correct binaries.
    post-activate {
        foreach obin {mkoctfile octave octave-cli octave-config} {
            reinplace "s|\"\${0}-orig\"|$obin-${version}-orig|" \
                ${prefix}/bin/${obin}-${version}
        }
    }
}

post-patch {
    platform darwin {
        # Keep ppc before i386, so that Rosetta builds for ppc.
        # Also, use build_arch and not sysctl:
        # https://trac.macports.org/ticket/69573
        if { ${configure.build_arch} eq "arm64" } {
            set short_host_name aarch64-apple-${os.platform}${os.major}.x.x
        } elseif { ${configure.build_arch} eq "ppc" } {
            set short_host_name powerpc-apple-${os.platform}${os.major}.x.x
        } elseif { ${configure.build_arch} eq "ppc64" } {
            set short_host_name powerpc64-apple-${os.platform}${os.major}.x.x
        } elseif { ${os.arch} eq "i386" } {
            if { ${os.major} >= 9 && [sysctl hw.cpu64bit_capable] == 1 } {
                set short_host_name x86_64-apple-${os.platform}${os.major}.x.x
            } else {
                set short_host_name i686-apple-${os.platform}${os.major}.x.x
            }
        }
    }
    reinplace \
        "s|__MACPORTS_canonical_host_type__|${short_host_name}|g" \
        configure \
        build-aux/subst-config-vals.in.sh
}

use_autoreconf      yes

pre-configure {
    # see https://trac.macports.org/ticket/51707
    set fc_full [lindex [split ${configure.fc} /] end]
    set fc      [lindex [split ${fc_full} -] 0]
    switch ${fc} {
        gfortran {
            set fcv    [lindex [split ${fc_full} -] end]
            set gccdir gcc[join [split ${fcv} .] ""]
            if {${gccdir} eq "gccdevel"} {set gccdir gcc-devel}
            if {[file exists ${prefix}/lib/${gccdir}/libquadmath.dylib]} {
                configure.args-append FLIBS="-L${prefix}/lib/${gccdir} -lgfortran -lquadmath -lm"
            } else {
                configure.args-append FLIBS="-L${prefix}/lib/${gccdir} -lgfortran -lm"
            }
        }
        g95 {
            configure.args-append FLIBS="-L${prefix}/lib -lf95 -lm"
        }
        default {
            ui_warn "Unknown Fortran compiler: ${configure.fc}"
        }
    }
}

# Some older versions of CLANG do not honor the CPATH
# environment variables, which is required for compiling this port
# when using MacPorts.  The versions seem to be: MacPorts CLANG 2.9 or
# earlier, and Apple CLANG 318.0.58 or older.
# see https://bugs.llvm.org/show_bug.cgi?id=8971
# see https://trac.macports.org/ticket/40250
#
# Clang must be able to use cstdint.
# see https://trac.macports.org/ticket/53044
#
# Clang must be able to handle VLA extension.
# see https://clang.llvm.org/compatibility.html#vla
# see https://trac.macports.org/ticket/57270
#
compiler.blacklist-append {clang <= 802}

# see see https://trac.macports.org/ticket/53044
compiler.blacklist-append {*gcc-4.6}

configure.checks.implicit_function_declaration.whitelist-append strchr unreachable

# see https://wiki.octave.org/Building for a list of dependencies

# avoid depends_build-append path:libexec/coreutils/libstdbuf.so:coreutils
configure.env-append "MKDIR_P=/bin/mkdir -p"

# see https://trac.macports.org/ticket/57850
configure.env-append "INSTALL_DATA=\${INSTALL} -m 644 -p"

if { ${os.platform} eq "darwin" && ${os.major} > 10 } {
    # avoid depends_build-append port:cctools
    configure.env-append RANLIB=/usr/bin/ranlib
} else {
    # ranlib on older systems is too old to handle newer clang output
    # https://trac.macports.org/ticket/54228
    depends_build-append port:cctools
    configure.env-append RANLIB=${prefix}/bin/ranlib
}

# main octave port lists as a depends_lib
# configure.ac list it among the "[p]rograms used in Makefiles"
depends_build-append port:gawk
configure.awk ${prefix}/bin/gawk

# in configure.ac, said to be "[p]rograms used to generate icons file formats
depends_build-append \
    port:icoutils    \
    path:lib/pkgconfig/librsvg-2.0.pc:librsvg
configure.env-append              \
    ICOTOOL=${prefix}/bin/icotool \
    RSVG_CONVERT=${prefix}/bin/rsvg-convert

# octave does not depend directly on openssl, but it
#    is brought in by many of the other dependencies
#
# openssl's libraries are never linked into octave
# openssl's header files are never included
# the openssl part of Qt is never used
#
# similarly for libpaper
#
# see https://lists.macports.org/pipermail/macports-dev/2016-December/035032.html
#
license_noconflict  \
    openssl         \
    openssl11       \
    libpaper

# octave does not depend directly on graphviz, but it
#    is brought in by its dependency on librsvg.
# librsvg, in turn, is only used in the build phase to generate icons.
license_noconflict-append \
    graphviz

# main octave port lists as a depends_lib
# configure.ac list it among the "[p]rograms used in Makefiles"
depends_build-append port:grep
configure.env-append  GREP=${prefix}/bin/ggrep
configure.env-append EGREP=${prefix}/bin/gegrep
configure.env-append FGREP=${prefix}/bin/gfgrep

# configure.ac list it among the "[p]rograms used in Makefiles"
depends_build-append path:libexec/gnubin/find:findutils
configure.env-append FIND=${prefix}/bin/gfind

# main octave port lists as a depends_lib
# configure.ac list it among the "[p]rograms used in Makefiles"
depends_build-append port:gsed
configure.env-append SED=${prefix}/bin/gsed

depends_build-append port:flex
configure.env-append LEX=${prefix}/bin/flex

depends_build-append port:bison
configure.env-append "YACC=${prefix}/bin/bison -y"

depends_build-append port:gperf
configure.env-append GPERF=/usr/bin/gperf

# configure.ac list it among the "[p]rograms used in Makefiles"
depends_build-append port:perl5
configure.perl ${prefix}/bin/perl5

# avoid depends_build-append port:desktop-file-utils
#configure.env-append DESKTOP_FILE_INSTALL=""
configure.env-append ac_cv_prog_DESKTOP_FILE_INSTALL=""

depends_build-append \
    port:pkgconfig

compilers.choose    fc f77 f90 cc cxx

# GCC 6 fails in several ways
#  in order to use the Carbon framework, -fpermissive is required
#  GCC does not understand Xcode's use of __attribute__
#      see https://trac.macports.org/ticket/53021
#  as of Octave 4.4, GCC 6 also fails with
#     error: call of overloaded 'abs(signed char)' is ambiguous
#     see https://lists.gnu.org/archive/html/octave-maintainers/2016-02/msg00168.html
compiler.blacklist-append macports-gcc-6

# for now, limit the number of variants
# some of these compilers may work fine
compilers.setup     \
    require_fortran \
    -gcc6           \
    -gcc5           \
    -gcc49          \
    -gcc48          \
    -gcc47          \
    -gcc46          \
    -gcc45          \
    -gcc44          \
    -clang34        \
    -clang33

#configure.args-append --with-shell=???

configure.args-append       \
    --with-framework-carbon \
    --without-x             \
    --enable-static

configure.args-append       \
    --disable-openmp

configure.args-append       \
    --disable-java          \
    --without-qt            \
    --without-fltk          \
    --without-opengl        \
    --without-sndfile       \
    --without-portaudio     \
    --without-magick        \
    --disable-docs

configure.args-append --disable-silent-rules

# avoid
#     Undefined symbols for architecture x86_64:
#       "print_usage()", referenced from: ...
configure.args-append       \
    --enable-link-all-dependencies

# in configure.ac, listed as one of "[p]rograms used when running Octave"
depends_lib-append port:python312
configure.python ${prefix}/bin/python3.12

# in configure.ac, listed as one of "[p]rograms used when running Octave"
depends_lib-append port:ghostscript
configure.env-append GHOSTSCRIPT=${prefix}/bin/gs

# in configure.ac, listed as one of "[p]rograms used when running Octave"
depends_lib-append port:gnuplot
configure.env-append GNUPLOT=${prefix}/bin/gnuplot

# in configure.ac, listed as one of "[p]rograms used when running Octave"
depends_lib-append port:less
configure.env-append DEFAULT_PAGER=${prefix}/bin/less

depends_lib-append   port:ncurses
depends_lib-append   port:readline
depends_lib-append   port:pcre

#--without-amd
#--without-camd
#--without-colamd
#--without-cholmod
#--without-cxsparse
#--without-umfpack
depends_lib-append  \
    port:SuiteSparse_config \
    port:SuiteSparse_AMD \
    port:SuiteSparse_CAMD \
    port:SuiteSparse_COLAMD \
    port:SuiteSparse_CHOLMOD \
    port:SuiteSparse_CXSparse \
    port:SuiteSparse_SPQR \
    port:SuiteSparse_UMFPACK
foreach lib {amd camd colamd cholmod cxsparse umfpack} {
    configure.args-append \
        --with-${lib}="-l${lib} -lsuitesparseconfig"
}

#--without-qhull
depends_lib-append  port:qhull

#--without-z
depends_lib-append  port:zlib

#--without-hdf5
depends_lib-append  port:hdf5
configure.args-append \
    --with-hdf5-includedir=${prefix}/include \
    --with-hdf5-libdir=${prefix}/lib
# avoid configure error
#    fatal error: 'mpi.h' file not found
# see https://github.com/macports/macports-ports/pull/1865
#    for a discussion
# UPD. 2024.03. No longer an issue, apparently:
# https://trac.macports.org/ticket/69574
# foreach mpi {mpich mpich_devel openmpi openmpi_devel} {
#     require_active_variants hdf5 "" ${mpi}
# }

#--disable-fftw-threads
#--without-fftw3
#--without-fftw3f
depends_lib-append  port:fftw-3
depends_lib-append  port:fftw-3-single

#--without-glpk
depends_lib-append  port:glpk

#--without-curl
depends_lib-append  port:curl

#--without-qrupdate
depends_lib-append  port:qrupdate

#--without-arpack
depends_lib-append  port:arpack

# see https://lists.gnu.org/archive/html/help-octave/2016-08/msg00066.html
configure.args-append --without-openssl
#depends_lib-append path:lib/libssl.dylib:openssl

#--disable-rapidjson
depends_lib-append  path:lib/pkgconfig/RapidJSON.pc:rapidjson

# see https://trac.macports.org/ticket/56989
configure.args-append \
    --without-sundials_nvecserial \
    --without-sundials_ida \
    --without-sundials_sunlinsolklu \
    --without-sundials_core
variant sundials description {enable sundials support for solving ODEs} {
    set sundials_ver     6
    set sundials_base    ${prefix}/libexec/sundials${sundials_ver}
    set sundials_include ${sundials_base}/include
    set sundials_lib     ${sundials_base}/lib

    configure.env-append "CXXFLAGS=-std=gnu++17"

    depends_lib-append \
        port:sundials${sundials_ver}
    compiler.cpath-append \
        ${sundials_include}
    compiler.library_path-append \
        ${sundials_lib}
     configure.args-delete \
         --without-sundials_nvecserial \
         --without-sundials_ida \
         --without-sundials_sunlinsolklu
    configure.args-append \
        --with-sundials_ida-includedir=${sundials_include} \
        --with-sundials_ida-libdir=${sundials_lib} \
        --with-sundials_nvecserial-includedir=${sundials_include} \
        --with-sundials_nvecserial-libdir=${sundials_lib} \
        --with-sundials_sunlinsolklu-includedir=${sundials_include} \
        --with-sundials_sunlinsolklu-libdir=${sundials_lib}
}
default_variants-append +sundials

# see etc/README.MacOS
depends_run-append   \
    port:epstool     \
    port:ghostscript \
    port:fig2dev     \
    port:pstoedit

# see https://savannah.gnu.org/bugs/?53473
depends_run-append  \
    port:texinfo
patchfiles-append patch-makeinfo.diff
post-patch {
    reinplace \
        "s|__MACPORTS_prefix__|${prefix}|g" \
        libinterp/corefcn/help.h
}

pre-configure {
    configure.args-append --with-blas="-L${prefix}/lib ${linalglib}" --with-lapack=""
}

variant java description {enable Java interface} {
    PortGroup java 1.0

    configure.args-replace --disable-java --enable-java
    java.version ${java_version}+
}
# java variant seems to cause problems for both users and build bots
#    https://lists.macosforge.org/pipermail/macports-users/2016-May/thread.html#41052
#    https://trac.macports.org/ticket/51480
#default_variants-append +java

variant qt5 description {build the GUI using Qt5} {
    PortGroup qt5 1.0

    qt5.depends_component qttools sqlite-plugin
    depends_lib-append port:qscintilla-qt5

    # location of libqscintilla2.dylib
    configure.ldflags-append \
        -L${qt_libs_dir}

    configure.args-replace --without-qt --with-qt=5

    configure.env-append PATH=${qt_dir}/bin:$env(PATH)
    build.env-append     PATH=${qt_dir}/bin:$env(PATH)
    destroot.env-append  PATH=${qt_dir}/bin:$env(PATH)
    test.env-append      PATH=${qt_dir}/bin:$env(PATH)
}
if { ![variant_isset qt5] } {
    # Cutoff for default values
    #    macOS 10.7 - last OS supported by Qt 4
    #       see http://doc.qt.io/qt-4.8/supported-platforms.html
    #    macOS 10.10 - oldest OS supported by the current long-term support version of Qt 5 (Qt 5.9).
    #       see http://web.archive.org/web/20170702143426/https://doc.qt.io/qt-5/supported-platforms-and-configurations.html
    #    macOS 10.11 - oldest OS supported for qscintilla-qt5, as of 2022-09-21
    if {${os.major} >= 15 || ${os.platform} ne "darwin"} {
        default_variants-append +qt5
    }
}

variant fltk description {enable fltk as a graphics toolkit for plotting} {
    depends_lib-append path:lib/libfltk.dylib:fltk
    configure.args-replace --without-fltk --with-fltk
}
# see https://github.com/macports/macports-ports/pull/1865 for a discussion on fltk
#default_variants-append +fltk

if {[variant_isset fltk] || [variant_isset qt5]} {
    # native_graphics (fltk) and gui (Qt) require OpenGL and font and printing capabilities
    depends_lib-append  \
        port:fontconfig \
        port:freetype   \
        port:gl2ps

    configure.args-replace --without-opengl --with-framework-opengl
}

if { [variant_isset fltk] && [variant_isset qt5] } {
    notes-append "unless octave is run with --no-gui-libs, graphics_toolkit(\"fltk\") will cause a crash"
}

variant sound description {enable audio support (file I/O and playback)} {
    depends_lib-append port:libsndfile
    depends_lib-append port:portaudio

    configure.args-replace --without-sndfile   --with-sndfile
    configure.args-replace --without-portaudio --with-portaudio
}
default_variants-append +sound

variant app description "build application bundle to launch ${subport}" {
    if {[vercmp ${xcodeversion} 4.5] < 0} {
        # need a way to generate icns file for Xcode prior to 4.5
        # see http://trac.macports.org/ticket/51487
        depends_build-append port:libicns
    }

    global appName
    set appName Octave.app

    post-build {
        xinstall -d -m 0755 ${worksrcpath}/${appName}/Contents
        xinstall -d -m 0755 ${worksrcpath}/${appName}/Contents/Resources
        xinstall -d -m 0755 ${worksrcpath}/${appName}/Contents/MacOS

        set script [open "${worksrcpath}/${appName}/Contents/MacOS/Octave" w 0755]
        if { [variant_isset qt5] } {
            puts ${script} "#!/bin/sh"
            puts ${script} ""
            puts ${script} "${prefix}/bin/octave-${version} --gui \"\$@\""
        } else {
            puts ${script} "#!/usr/bin/osascript"
            puts ${script} ""
            puts ${script} "tell application \"Terminal\" to do script \"${prefix}/bin/octave-${version} --no-gui-libs; exit\""
        }
        close ${script}

        set values "
            CFBundleDevelopmentRegion                         string  English
            CFBundleExecutable                                string  Octave
            CFBundleIconFile                                  string  Octave.icns
            CFBundleIdentifier                                string  org.octave.Octave
            CFBundleInfoDictionaryVersion                     string  6.0
            CFBundleSignature                                 string  Octave
            CFBundleVersion                                   string  ${version}
            CFBundleShortVersionString                        string  ${version}
            CFBundleDocumentTypes                             array   {}
            CFBundleDocumentTypes:                            dict    {}
            CFBundleDocumentTypes:0:CFBundleTypeRole          string  \"Editor\"
            CFBundleDocumentTypes:0:CFBundleTypeExtensions    array   {}
            CFBundleDocumentTypes:0:CFBundleTypeExtensions:   string  \"m\"
            CFBundleDocumentTypes:0:CFBundleTypeOSTypes       array   {}
            CFBundleDocumentTypes:0:CFBundleTypeOSTypes:      string  \"Mfile\"
            CFBundleName                                      string  Octave
            CFBundlePackageType                               string  APPL
            NSPrincipalClass                                  string  NSApplication
        "

        foreach {key type value} ${values} {
            system -W "${worksrcpath}/${appName}/Contents" "/usr/libexec/PlistBuddy -c \"Add :${key} ${type} ${value}\" Info.plist"
        }

        # have Info.plist be human readable
        system "/usr/bin/plutil -convert xml1 ${worksrcpath}/${appName}/Contents/Info.plist"

        # conversion by plutil set verys limited permissions
        system "/bin/chmod 0644 ${worksrcpath}/${appName}/Contents/Info.plist"

        xinstall -d -m 0755 ${worksrcpath}/Octave.iconset

        # values from original SVG file
        set svg    etc/icons/octave-logo.svg
        set width  283.28912
        set height 283.28833
        set dpi    90

        foreach res {16 32 128 256 512} {
            set hres [expr 2*${res}]
            # see https://savannah.gnu.org/bugs/?37062
            # see http://hg.savannah.gnu.org/hgweb/octave/rev/1687269e31e4
            system -W ${worksrcpath} "${prefix}/bin/rsvg-convert -w ${res} ${svg}  > Octave.iconset/icon_${res}x${res}.png"
            system -W ${worksrcpath} "${prefix}/bin/rsvg-convert -w ${hres} ${svg} > Octave.iconset/icon_${res}x${res}@2x.png"
        }

        if {[vercmp ${xcodeversion} 4.5] >= 0} {
            system -W ${worksrcpath} "/usr/bin/iconutil -c icns -o ${appName}/Contents/Resources/Octave.icns Octave.iconset"
        } else {
            # /usr/bin/iconutil introduced in Xcode 4.5
            # see http://trac.macports.org/ticket/51487
            system -W ${worksrcpath}/Octave.iconset \
                "${prefix}/bin/png2icns ${worksrcpath}/${appName}/Contents/Resources/Octave.icns icon_16x16.png icon_32x32.png icon_128x128.png icon_256x256.png icon_512x512.png"
        }
    }

    post-destroot {
        copy ${worksrcpath}/${appName} ${destroot}${applications_dir}
    }
}
if { [variant_isset qt5] } {
    default_variants-append +app
}


# the qt gui uses a few Objective-C calls, and for some reason
# libobjc.dylib is not being automatically linked in on older systems
if { [variant_isset qt5] } {
   if { ${os.platform} eq "darwin" && ${os.major} < 18 } {
       configure.ldflags-append -lobjc
   }
}

variant docs description {build documentation files} {
    depends_build-append port:texinfo
    configure.env-append                \
        MAKEINFO=${prefix}/bin/makeinfo \
        TEXI2DVI=${prefix}/bin/texi2dvi \
        TEXI2PDF=${prefix}/bin/texi2pdf

    # see https://lists.macosforge.org/pipermail/macports-dev/2016-January/032293.html
    depends_build-append \
        port:texlive-basic \
        port:texlive-latex

    # see https://trac.macports.org/ticket/51132
    depends_build-append \
        port:texlive-fonts-recommended

    configure.args-replace --disable-docs --enable-docs
}
default_variants-append +docs

# GraphicsMagick and octave need to be built with the same C++ standard library
# or else undefined symbols:
#    "Magick::Image::ping(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)"
set magickConflict {}
set magickDefault yes
if {${configure.cxx_stdlib} eq "libc++"} {
    foreach gccVar ${compilers.gcc_variants} {
        if {[variant_exists ${gccVar}] } {
            set varPort [fortran_depends_port_name ${gccVar}]
            lappend magickConflict ${varPort}
            if {[variant_isset ${gccVar}] } {
                set magickDefault no
            }
        }
    }
}

variant graphicsmagick description {use GraphicsMagick for image I/O} conflicts {*}${magickConflict} {
    depends_lib-append port:GraphicsMagick
    configure.args-replace --without-magick --with-magick=GraphicsMagick++
}
if {${magickDefault}} {
    default_variants-append +graphicsmagick
}

post-destroot {
    # put any startup commands in ${destroot}${prefix}/share/octave/${version}/m/startup/octaverc
    # see https://www.gnu.org/software/octave/doc/interpreter/Startup-Files.html
    file delete ${destroot}${prefix}/share/octave/site/m/startup/octaverc

    if {[file exists ${worksrcpath}/liboctave/operators/libcxx-fix.h]} {
        # install the libc++ fix, no matter if used or not, since it is
        # required for projects including these headers.
        xinstall -m 644 ${worksrcpath}/liboctave/operators/libcxx-fix.h \
            ${destroot}${prefix}/include/${name}-${version}/${name}/libcxx-fix.h
    }

    # do not force upgrade of every dependency every time the version changes
    foreach lib {liboctave.dylib liboctinterp.dylib} {
        xinstall -d -m 0755 ${destroot}${prefix}/lib/octave/${package_version}
        ln -s ../${version}/${lib} ${destroot}${prefix}/lib/octave/${package_version}/${lib}
        system "install_name_tool -id ${prefix}/lib/octave/${package_version}/${lib} ${destroot}${prefix}/lib/octave/${version}/${lib}"
    }

    if { [variant_isset qt5] } {
        # do not force upgrade of every dependency every time the version changes
        foreach lib {liboctgui.dylib} {
            xinstall -d -m 0755 ${destroot}${prefix}/lib/octave/${package_version}
            ln -s ../${version}/${lib} ${destroot}${prefix}/lib/octave/${package_version}/${lib}
            system "install_name_tool -id ${prefix}/lib/octave/${package_version}/${lib} ${destroot}${prefix}/lib/octave/${version}/${lib}"
        }
    }
}

# generate default octave_packages file so that it is removed if Octave is uninstalled
if {!${universal_possible} || ![variant_isset universal]} {
    post-destroot {
        set octave_install_share ${destroot}${prefix}/share/octave/packages
        set octave_install_lib   ${destroot}${prefix}/lib/octave/packages
        set octave_packages_file ${destroot}${prefix}/share/octave/octave_packages
        system -W ${worksrcpath} \
            "./run-octave -cli -q -f -H --eval 'try; pkg prefix ${octave_install_share} ${octave_install_lib}; pkg global_list ${octave_packages_file}; pkg -verbose -global rebuild; disp(lasterror.message); catch; exit(1); end_try_catch;'"
    }
} else {
    merger-post-destroot {
        foreach arch ${universal_archs_to_use} {
            set octave_install_share ${destroot}-${arch}${prefix}/share/octave/packages
            set octave_install_lib   ${destroot}-${arch}${prefix}/lib/octave/packages
            set octave_packages_file ${destroot}-${arch}${prefix}/share/octave/octave_packages
            system -W ${worksrcpath}-${arch} \
                "./run-octave -cli -q -f -H --eval 'try; pkg prefix ${octave_install_share} ${octave_install_lib}; pkg global_list ${octave_packages_file}; pkg -verbose -global rebuild; disp(lasterror.message); catch; exit(1); end_try_catch;'"
        }
    }
}

# avoid warnings about generated directories
destroot.keepdirs-append ${destroot}${prefix}/share/octave/packages
destroot.keepdirs-append ${destroot}${prefix}/lib/octave/packages

# unless octave_packages file is regenerated, then installed Octave packages
#    will NOT be recognized whenever Octave is upgraded
post-activate {
    set octave_install_share ${prefix}/share/octave/packages
    set octave_install_lib   ${prefix}/lib/octave/packages
    system "${prefix}/bin/octave-cli -q -f -H --eval 'try; pkg prefix ${octave_install_share} ${octave_install_lib}; pkg -verbose -global rebuild; disp(lasterror.message); catch; exit(1); end_try_catch;'"
}

test.run    yes
test.target check
