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

name                LaTeXML
version             0.8.2
revision            1
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.22
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}-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.
options latexml.found_tex
default latexml.found_tex no

# No texmf install via Makefile.
pre-configure {
   configure.args-append   TEXMF=
  }
# We don't want dependencies on texlive, but want to cooperate.
# We'll install style files where texlive expects or will expect.
post-destroot {
    xinstall -d ${texlive_texmfdist}/tex/latex/latexml
    foreach sty [glob ${worksrcpath}/lib/LaTeXML/texmf/*.sty] {
        file copy -force ${sty} ${texlive_texmfdist}/tex/latex/latexml/
    }
}
# invoke mktexlsr IFF texlive already installed;
# otherwise texlive will do it if it's ever installed.
post-activate {
    if [file executable ${prefix}/bin/mktexlsr] {
        latexml.found_tex yes
        ${texlive.mktexlsr}
    }
}
# Remove style files
post-uninstall {
    foreach sty [glob -nocomplain ${texlive_texmfdist}/tex/latex/latexml/*.sty] {
        delete ${sty}
    }
    if [file executable ${prefix}/bin/mktexlsr] {
        ${texlive.mktexlsr}
    }
}

# The mactex variant expects MacTeX to be installed
# and installs latexml's stylefiles to MacTeX's texmf (local)
options latexml.mactex_bin
options latexml.mactex_texmf
default latexml.mactex_bin ""
default 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"] {
            latexml.mactex_bin ${dir}
            break
        }
    }
    if { ${latexml.mactex_bin} != "" } {
        latexml.mactex_texmf \
            [exec ${latexml.mactex_bin}/kpsewhich --expand-var='\$TEXMFLOCAL']
        regsub -all {'} ${latexml.mactex_texmf} "" latexml.mactex_texmf
        latexml.found_tex yes
    } else {
        return -code error "Cannot find MacTeX installation; aborting"
    }

    post-destroot {
        xinstall -d ${latexml.mactex_texmf}/tex/latex/latexml
        foreach sty [glob ${worksrcpath}/lib/LaTeXML/texmf/*.sty] {
            file copy -force ${sty} ${latexml.mactex_texmf}/tex/latex/latexml/
        }
    }
    post-activate {
        system "${latexml.mactex_bin}/mktexlsr"
    }
    # Remove style files
    post-uninstall {
        foreach sty [glob -nocomplain ${latexml.mactex_texmf}/tex/latex/latexml/*.sty] {
            delete ${sty}
        }
        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
}

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

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