# -*- 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
PortGroup           python 1.0
PortGroup           texlive 1.0

name                dblatex
version             0.3.10
revision            1
categories          textproc tex
maintainers         {cal @neverpanic} openmaintainer
license             GPL-2+
platforms           darwin
supported_archs     noarch

description         DocBook to LaTeX Converter
long_description    Dblatex transforms a DocBook XML/SGML document to LaTeX. \
                    Once transformed into LaTeX, standard LaTeX tools are \
                    used to produce DVI, Postcript or PDF files. \
                    dblatex tries to hide as much as possible the latex \
                    compiling stuff by providing a single clean script to \
                    produce directly DVI, PostScript and PDF output. \
                    The actual output rendering is done not only by the \
                    XSL stylesheets transformation, but also by a dedicated \
                    LaTeX package. The goal is to allow a deep LaTeX \
                    customisation without changing the XSL stylesheets. \
                    Post-processing is done by Python, to make publication \
                    faster, convert the images if needed, and do the whole \
                    compilation.

homepage            http://dblatex.sourceforge.net
master_sites        sourceforge:project/dblatex/dblatex/dblatex-${version}

checksums           sha256  6fd696b740e0044ae1caf843d225d98c01b6ed916550384544e7e31c0c6a2cfa \
                    rmd160  b113dc1a04180704e36558039ef1cc9d13900105

use_bzip2           yes

depends_lib         port:texlive-latex-extra \
                    port:texlive-latex-recommended \
                    port:texlive-math-science \
                    port:dejavu-fonts \
                    port:ImageMagick \
                    port:docbook-xml \
                    port:docbook-xsl

python.link_binaries no
python.default_version 27

# this gets assigned before python.bin is set to the version selected by the variant
#destroot.cmd        ${python.bin} setup.py
destroot.destdir    --root=${destroot} \
                    --catalogs=${destroot}${prefix}/etc/xml/catalog

# The mactex variant expects MacTeX to be installed
# and installs dblatex's stylefiles to MacTeX's texmf (local)
set dblatex.texmflocal ""
set dblatex.mactex_bin ""

variant mactex description {Allow dblatex to use a MacTeX installation instead of Macports texlive tools.
To install the mactex variant /Library/TeX/texbin must be added to binpath
in macports.conf} {
    # First, check if MacTeX actually seems to be thereā€¦
    set dblatex.mactex_candidates { \
       "/Library/TeX/texbin" \
       "/usr/texbin" \
    }
    foreach dir ${dblatex.mactex_candidates} {
        if [file executable "${dir}/kpsewhich"] {
            set dblatex.mactex_bin ${dir}
            break
        }
    }
    if { ${dblatex.mactex_bin} != "" } {
        set dblatex.texmflocal \
            [exec ${dblatex.mactex_bin}/kpsewhich --expand-var='\$TEXMFLOCAL']
        regsub -all {'} ${dblatex.texmflocal} "" dblatex.texmflocal
    } else {
        return -code error "Cannot find MacTeX installation; aborting"
    }

    depends_lib-delete \
                    port:texlive-latex-extra \
                    port:texlive-latex-recommended \
                    port:texlive-math-science

    notes "The mactex variant will install styles to MacTeX's texmf-local
        ${dblatex.texmflocal}
    (which is outside macport's common directory structure).
    For the mactex variant to work,
    ${dblatex.mactex_bin} must also be added to binpath in macports.conf"

    # AND, since we're installing files outside macports' normal directories
    destroot.violate_mtree  yes
}

post-destroot {
    reinplace "s|${destroot}${prefix}|${prefix}|g" ${destroot}${python.prefix}/bin/dblatex
    reinplace "s|#!/usr/bin/env python|#!${python.bin}|" ${destroot}${python.prefix}/bin/dblatex

    fs-traverse f ${destroot}${python.prefix}/share {
        if {[file isfile $f]} {
            set f [string range $f [string length ${destroot}${python.prefix}/share/] end]
            file mkdir [file dirname ${destroot}${prefix}/share/$f]
            ln -s ${python.prefix}/share/$f ${destroot}${prefix}/share/$f
        }
    }
    if {![variant_isset mactex]} {
        set dblatex.texmflocal ${texlive_texmflocal}
    }
    file mkdir ${destroot}${dblatex.texmflocal}/tex/latex/dblatex
    fs-traverse f ${destroot}${python.prefix}/share/dblatex/latex {
        if {[file isfile $f]} {
            if {[catch {exec ${prefix}/bin/kpsewhich -a [file tail $f]} result]} {
                set f [string range $f [string length ${destroot}${python.prefix}/share/dblatex/latex/] end]
                file mkdir [file dirname ${destroot}${dblatex.texmflocal}/tex/latex/dblatex/$f]
                ln -s ${python.prefix}/share/dblatex/latex/$f ${destroot}${dblatex.texmflocal}/tex/latex/dblatex/$f
            }
        }
    }
    ln -s ${python.prefix}/bin/dblatex ${destroot}${prefix}/bin/
}

proc dblatex.mktexlsr {} {
    if {![variant_isset mactex]} {
        texlive.mktexlsr
    } else {
        global dblatex.mactex_bin
        system "${dblatex.mactex_bin}/mktexlsr"
        if [file exists "${dblatex.mactex_bin}/mtxrun"] {
            system "${dblatex.mactex_bin}/mtxrun --generate"
        }
    }
}

post-activate {
    dblatex.mktexlsr
}

post-deactivate {
    dblatex.mktexlsr
}

livecheck.regex     /${name}-(\[0-9.\]+)/