# -*- 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                    reduce
version                 20211019
revision                0
set version_dashes      [regsub {^(\d{4})(\d{2})(\d{2})$} ${version} {\1-\2-\3}]
set svnrev              6110
categories              math lang
license                 BSD
platforms               darwin
maintainers             {gmail.com:mark.brethen @mbrethen} openmaintainer
description             REDUCE Computer Algebra System
long_description \
    REDUCE is an interactive system for general algebraic computations of \
    interest to mathematicians, scientists and engineers.
homepage                https://reduce-algebra.sourceforge.io/
master_sites            sourceforge:project/reduce-algebra/snapshot_${version_dashes}
distname                Reduce-svn${svnrev}-src

checksums               sha256  046e3eda8ab006bb184b6bbf911bb87b35f110b300b5f52766445e9c8a22ad1a \
                        rmd160  d48669ca88162e33acf2872702f14b6eaddb514f \
                        size    240908050

compiler.cxx_standard 2011

patchfiles-append       patch-configure.ac.diff \
                        patch-csl-build.diff \
                        patch-packages-plot-gnuintfc.red.diff

depends_build-append    port:gnuplot \
                        port:automake \
                        port:autoconf \
                        port:libtool \
                        port:xorg-xorgproto \
                        port:xorg-libX11 \
                        port:xorg-libXcursor \
                        port:xorg-libXext \
                        port:Xft2 \
                        port:xorg-libXrandr \
                        port:xrender

depends_lib-append      port:fontconfig \
                        port:freetype \
                        port:libffi \
                        port:ncurses \
                        port:netpbm

configure.ldflags-append -lintl

use_parallel_build      no

universal_variant       no

post-patch {
    reinplace -W ${worksrcpath} "s|@PREFIX@|${prefix}|g" \
        configure.ac \
        csl/cslbase/configure.ac \
        csl/fox/configure.ac \
        csl/foxtests/configure.ac \
        packages/plot/gnuintfc.red
}

configure {
    # we have to run configure twice, once --with-csl
    # and another time --with-psl, prior to building
    # this generates two different build directories
    # and both are built during the build phase
    # we do it like this to make it clear what is happening here...
    
    # Technically portconfigure::configure_main is not part of the official exposed
    # MacPorts API, so theoretically it could change in the future and the port
    # would break... but it should work for now.

    configure.args-append --with-csl
    portconfigure::configure_main

    configure.args-replace --with-csl --with-psl
    portconfigure::configure_main
}

destroot {
    set builddir [exec ${worksrcpath}/scripts/findhost.sh [exec ${worksrcpath}/config.guess]]
    set cslbuilddir ${worksrcpath}/cslbuild/${builddir}
    set pslbuilddir ${worksrcpath}/pslbuild/${builddir}
    set libexecdir  ${destroot}${prefix}/libexec/${name}
    set bindir      ${destroot}${prefix}/bin
    set appsdir     ${destroot}${applications_dir}/${name}

    xinstall -d \
        ${libexecdir} \
        ${libexecdir}/psl \
        ${libexecdir}/psl/psl \
        ${libexecdir}/psl/red \
        ${appsdir}

    copy ${worksrcpath}/macbuild/README.for.distribution ${libexecdir}/README
    
    copy ${cslbuilddir}/csl/reduce.app ${appsdir}
    xinstall -m a+rx ${worksrcpath}/macbuild/runcsl.sh ${appsdir}/redcsl
    xinstall -m a+rx ${cslbuilddir}/redfront/rfcsl ${appsdir}
    
    copy {*}[glob ${pslbuilddir}/psl/*] ${libexecdir}/psl/psl
    copy {*}[glob ${pslbuilddir}/red/*] ${libexecdir}/psl/red
    xinstall -m a+rx ${worksrcpath}/macbuild/runpsl.sh ${libexecdir}/psl/redpsl
    xinstall -m a+rx ${cslbuilddir}/redfront/rfpsl ${libexecdir}/psl

    copy ${worksrcpath}/doc/manual/manual.pdf ${libexecdir}/reduce-manual.pdf

    # link scripts to ${prefix}/bin
    ln -s ${applications_dir}/${name}/redcsl ${bindir}/redcsl
    ln -s ${prefix}/libexec/${name}/psl/redpsl ${bindir}/redpsl
    ln -s ${applications_dir}/${name}/rfcsl ${bindir}/rfcsl
    ln -s ${prefix}/libexec/${name}/psl/rfpsl ${bindir}/rfpsl
}

livecheck.version   ${version_dashes}
livecheck.url       https://sourceforge.net/projects/reduce-algebra/files/
livecheck.regex     {snapshot_(\d{4}-\d{2}-\d{2})}
