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

PortSystem          1.0
PortGroup           texlive 1.0

name                LaTeXML
version             0.8.8
revision            0
checksums           rmd160  4f6aee98474e58905fe89327b55be42e3d5138f0 \
                    sha256  7d2bbe2ce252baf86ba3f388cd0dec3aa4838f49d612b9ec7cc4ff88105badcc \
                    size    15310900
license             public-domain
maintainers         {nist.gov:bruce.miller @brucemiller}
description         LaTeXML converts TeX to XML/HTML/MathML
long_description \
   LaTeXML converts TeX to XML, including HTML, XHTML, ePub with MathML.

# Written in Perl, but it is an application, not just modules
PortGroup           perl5 1.0

perl5.branches      5.34
perl5.setup         ${name} ${version}
perl5.link_binaries_suffix

categories          tex
homepage            https://dlmf.nist.gov/LaTeXML/
master_sites        ${homepage}releases/ \
                    https://cpan.metacpan.org/authors/id/B/BR/BRMILLER/

platforms           {darwin any}
supported_archs     noarch

depends_lib-append \
        port:p${perl5.major}-archive-zip \
        port:p${perl5.major}-file-which \
        port:p${perl5.major}-getopt-long \
        port:p${perl5.major}-image-size \
        port:p${perl5.major}-io-string \
        port:p${perl5.major}-json-xs \
        port:p${perl5.major}-libwww-perl \
        port:p${perl5.major}-parse-recdescent \
        port:p${perl5.major}-pod-parser \
        port:p${perl5.major}-text-unidecode \
        port:p${perl5.major}-time-hires \
        port:p${perl5.major}-uri \
        port:p${perl5.major}-xml-libxml \
        port:p${perl5.major}-xml-libxslt \
        port:p${perl5.major}-perlmagick

# Also requires: DB_File, Pod::Parser, Test::More & version
# but those should be in any non-obsolete Perl's core modules.

#============================================================
# The #! line normally uses env perl for flexibility,
# but this doesn't work w/multiple perls on MacPorts
post-configure {
    fs-traverse file ${worksrcpath}/bin {
        if {[file isfile ${file}]} {
            ui_info "Fixing path in ${file}"
            reinplace "s|#!/usr/bin/env perl|#!${prefix}/bin/perl${perl5.major}|g" ${file}
        }
    }
}
#============================================================
# LaTeXML works MUCH better if there is a TeX installed.
# - it uses some latex style files from texlive within bindings
# - it can install its own style files for use within tex/latex
# We could simply depend on texlive, but some folks prefer MacTeX
# and object to 2nd multi-GB download!  So we define variants.
set latexml.found_tex no

# We don't want dependencies on texlive, but want to cooperate.
# We'll install style files where texlive expects or will expect.
configure.args-append   TEXMF=${texlive_texmfdist}

# But the .packlist will need cleanup
post-destroot {
    fs-traverse file ${destroot}${prefix}/share {
        if {[file isfile ${file}] && [file tail ${file}] eq ".packlist"} {
            ui_info "Fixing paths in [string map "${destroot}${prefix}/ {}" ${file}]"
            reinplace -n "s|${destroot}||p" ${file}
        }
    }
}

# Note whether we've found a TeX
post-activate {
    if {[file executable ${prefix}/bin/mktexlsr]} {
        set latexml.found_tex yes
    }
}

# The mactex variant expects MacTeX to be installed
# and installs latexml's stylefiles to MacTeX's texmf (local)
set latexml.mactex_bin ""
set latexml.mactex_texmf ""
set latexml.mactex_candidates { \
   "/Library/TeX/texbin" \
   "/usr/texbin" \
}
variant mactex description {Build with MacTeX support} {
    # First, check if MacTeX actually seems to be there...
    foreach dir ${latexml.mactex_candidates} {
        if {[file executable "${dir}/kpsewhich"]} {
            set latexml.mactex_bin ${dir}
            break
        }
    }
    if { ${latexml.mactex_bin} != "" } {
        set latexml.mactex_texmf \
            [exec ${latexml.mactex_bin}/kpsewhich --expand-var='\$TEXMFLOCAL']
        regsub -all {'} ${latexml.mactex_texmf} "" latexml.mactex_texmf
        set latexml.found_tex yes
    } else {
        return -code error "Cannot find MacTeX installation; aborting"
    }

    post-destroot {
        xinstall -d ${destroot}${latexml.mactex_texmf}/tex/latex/latexml
        foreach sty [glob ${worksrcpath}/lib/LaTeXML/texmf/*.sty] {
            xinstall -m 644 ${sty} ${destroot}${latexml.mactex_texmf}/tex/latex/latexml
        }
    }
    post-activate {
        system "${latexml.mactex_bin}/mktexlsr"
    }
    post-deactivate {
        system "${latexml.mactex_bin}/mktexlsr"
    }
    notes "Using MacTeX for TeX: will install styles to MacTeX's texmf-local
        ${latexml.mactex_texmf} \
        (which is outside macport's common directory structure)"
    # AND, since we're installing files outside macports' normal directories
    destroot.violate_mtree  yes
}

if {! ${latexml.found_tex}} {
    notes "${name} works best with some version of TeX installed.
        Please consider installing texlive, or PRE-install MacTeX and use the +mactex variant."
}
