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

name            maxima
categories      math
maintainers     {mareimbrium.org:kuba @KubaO} openmaintainer
platforms       darwin
license         GPL-2+
description     The Maxima computer algebra system
long_description \
        Maxima is a system for the manipulation of symbolic \
        and numerical expressions, including differentiation, \
        integration, Taylor series, Laplace transforms, ordinary \
        differential equations, systems of linear equations, \
        polynomials, and sets, lists, vectors, matrices, and \
        tensors. Maxima yields high precision numeric results \
        by using exact fractions, arbitrary precision integers, \
        and variable precision floating point numbers. Maxima \
        can plot functions and data in two and three dimensions. \
        Maxima includes the following commands: \
        * \"rmaxima\" is a front end which provides advanced line-editing \
        facilities via rlwrap. \
        * \"xmaxima\" is a graphical interface for maxima, written in Tcl/Tk.

homepage        https://maxima.sourceforge.io/

depends_build   port:texinfo \
                port:gsed
depends_run     port:rlwrap \
                port:recode \
                port:gnuplot

subport maxima-devel {
    conflicts   maxima

    # git describe --tags : branch-5_46-base-480-g5b42cccfd
    # Date:  Sun Jul 30 06:55:16 2023 -0700
    # commit 65e393d796fedd34162d2a510af37c2448f07d74
    version     5.47-dev-20230730
    revision    13
    fetch.type  git
    git.url     https://git.code.sf.net/p/maxima/code
    git.branch  65e393d796fedd34162d2a510af37c2448f07d74

    # sourceforge git uses Let's Encryption which can't be used with old macOS
    # see: https://trac.macports.org/ticket/67147
    depends_build-append \
                port:git
    git.cmd     ${prefix}/bin/git

    use_autoreconf  yes

    livecheck.type  none
}

if {${subport} eq ${name}} {
    conflicts   maxima-devel

    version     5.47.0
    revision    13
    # get the source tarball from sourceforge.
    master_sites    sourceforge:project/maxima/Maxima-source/${version}-source

    checksums   rmd160  2bf1b8fcf0900286e163517dec3ac3cc46a0972d \
                sha256  9104021b24fd53e8c03a983509cb42e937a925e8c0c85c335d7709a14fd40f7a \
                size    47939891

    livecheck.regex     {<title>.*/Maxima-source/(.*)-source/maxima.*</title>}
}

patchfiles      src_maxima.in.patch

configure.args  --infodir=${prefix}/share/info \
                --mandir=${prefix}/share/man

variant abcl conflicts ccl clisp ecl gcl sbcl \
    description {Use ABCL as lisp implementation} {
    depends_lib-append      port:abcl
    configure.args-append   --enable-abcl \
                            --with-abcl-jar=${prefix}/share/java/abcl/abcl.jar
}

variant ccl conflicts abcl clisp ecl gcl sbcl \
    description {Use CCL as lisp implementation} {
    depends_lib-append      port:ccl
    if { ${configure.build_arch} eq "x86_64" } {
        configure.args-append \
                            --enable-ccl64 \
                            --enable-ccl64-exec \
                            --with-ccl64=${prefix}/bin/ccl64
    } elseif { ${configure.build_arch} eq "i386" } {
        configure.args-append \
                            --enable-ccl \
                            --enable-ccl-exec \
                            --with-ccl=${prefix}/bin/ccl
    }
}

variant clisp conflicts abcl ccl ecl gcl sbcl \
    description {Use CLISP as lisp implementation} {
    depends_lib-append      port:clisp
    configure.args-append   --enable-clisp \
                            --enable-clisp-exec \
                            --with-clisp=${prefix}/bin/clisp
}

variant ecl conflicts abcl ccl clisp gcl sbcl \
    description {Use ECL as lisp implementation} {
    depends_lib-append      path:bin/ecl:ecl
    configure.args-append   --enable-ecl \
                            --with-ecl=${prefix}/bin/ecl
}

variant gcl conflicts abcl ccl clisp ecl sbcl \
    description {Use GCL as lisp implementation} {
    # NOTE: may fail on modern macOS
    # See: https://sourceforge.net/p/maxima/bugs/4167/
    depends_lib-append      port:gcl
    configure.args-append   --enable-gcl \
                            --with-gcl=${prefix}/bin/gcl
}

variant sbcl conflicts abcl ccl clisp ecl gcl \
    description {Use SBCL as lisp implementation} {
    depends_lib-append      path:bin/sbcl:sbcl
    configure.args-append   --enable-sbcl \
                            --enable-sbcl-exec \
                            --with-sbcl=${prefix}/bin/sbcl
}

if {![variant_isset abcl] && ![variant_isset ccl] && ![variant_isset clisp] && ![variant_isset ecl] && ![variant_isset gcl] && ![variant_isset sbcl]} {
    default_variants-append +sbcl
}

variant xmaxima description {build xmaxima} {
    depends_run-append  port:tk
}

default_variants-append +xmaxima

if {![variant_isset xmaxima]} {
    patchfiles-append   no-xmaxima.patch
}

test.run    yes
test.target check

variant printable_doc description {Build printable documentation} {
    depends_build-append    port:texinfo \
                            port:texlive-basic \
                            port:texlive-latex \
                            port:texlive-plain-generic \
                            port:texlive-fonts-recommended
    build.target            all pdf

    pre-destroot {
        xinstall -d -m 755 ${destroot}${prefix}/share/${name}/${version}/doc/pdf
        xinstall -m 644 ${worksrcpath}/doc/info/maxima.pdf \
                        ${destroot}${prefix}/share/${name}/${version}/doc/pdf
    }
}