# -*- 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
#======================================================================
# Portfile for LaTeXML
#======================================================================
PortSystem          1.0
PortGroup           texlive 1.0

name                LaTeXML
version             0.8.2
license             public-domain
maintainers         nist.gov:bruce.miller
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.24
perl5.setup         ${name} ${version}
perl5.link_binaries_suffix

categories          tex
homepage            http://dlmf.nist.gov/LaTeXML/

platforms           darwin
supported_archs     noarch

master_sites        ${homepage}/releases/
checksums           rmd160 6dbf960277cdcf30605ce93335e3383673c1ff66 \
                    sha256 3d41a3012760d31d721b569d8c1b430cde1df2b68fcc3c66f41ec640965caabf
# Use:
# openssl rmd160 LaTeXML-0.8.2.tar.gz
# openssl sha256 LaTeXML-0.8.2.tar.gz

#============================================================
# Dependencies
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}-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.

#============================================================
# 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."
}

#============================================================
