# -*- 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
# $Id$

PortSystem          1.0
PortGroup           texlive 1.0

name                texlive-common
version             2011

categories          tex
maintainers         dports
description         TeX Live common infrastructure.
long_description    This port provides files that support a MacPorts    \
                    installation of TeX Live, such as configuration     \
                    files and the scripts that generate them.

homepage            http://www.tug.org/texlive/
platforms           darwin
supported_archs     noarch
license             Permissive

master_sites        http://flute.csail.mit.edu/texlive/
worksrcdir          ${distname}
use_xz              yes

checksums           rmd160  ebb014281c37d2fab37fb464a9739b7fb48b5478 \
                    sha256  3a428bdbb8e82aa2281fcba64afcccff21ef30e6ff3bd6d96621c7b0b355c610

livecheck.type  regex
livecheck.url   ${master_sites}
livecheck.regex ${name}-(\[\\d-\]+)\\.tar

post-patch {
    foreach x {texmf.cnf.d/10paths.cnf texmfcnf.lua texlive-update-cnf} {
        reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXMFMAIN@@|${texlive_texmfmain}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXMFDIST@@|${texlive_texmfdist}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXMFPORTS@@|${texlive_texmfports}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXMFLOCAL@@|${texlive_texmflocal}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXMFSYSVAR@@|${texlive_texmfsysvar}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXMFSYSCONFIG@@|${texlive_texmfsysconfig}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXMFHOME@@|${texlive_texmfhome}|g" ${worksrcpath}/${x}
        reinplace "s|@@TEXLIVE_BINDIR@@|${texlive_bindir}|g" ${worksrcpath}/${x}
    }
}

use_configure   no

build           { }

set texmfdirs [list ${texlive_texmfmain} \
                    ${texlive_texmfdist} \
                    ${texlive_texmfports} \
                    ${texlive_texmflocal} \
                    ${texlive_texmfsysvar} \
                    ${texlive_texmfsysconfig}]
destroot {
    # Create texmf directories
    foreach texmfdir $texmfdirs {
        xinstall -d ${destroot}${texmfdir}
    }

    # Create empty ls-R files so that they're marked as owned by this
    # port. The contents will be generated/updated by mktexlsr.
    foreach texmfdir $texmfdirs {
        touch ${destroot}${texmfdir}/ls-R
    }

    # Create fmt/updmap/hyphen files, directories, and install update script
    xinstall -d ${destroot}${texlive_texmfsysconfig}/fmtutil.d
    xinstall -d ${destroot}${texlive_texmfsysconfig}/language.d
    xinstall -d ${destroot}${texlive_texmfsysconfig}/updmap.d
    xinstall -m 644 ${worksrcpath}/updmap-hdr.cfg \
        ${destroot}${texlive_texmfsysconfig}/updmap.d/00updmap-hdr.cfg
    xinstall -m 644 ${worksrcpath}/fmtutil-hdr.cnf \
        ${destroot}${texlive_texmfsysconfig}/fmtutil.d/00fmtutil-hdr.cnf
    xinstall -m 644 ${worksrcpath}/language.us \
        ${destroot}${texlive_texmfsysconfig}/language.d/00language.us.dat
    xinstall -m 644 ${worksrcpath}/language.us.def \
        ${destroot}${texlive_texmfsysconfig}/language.d/00language.us.def
    xinstall -m 644 ${worksrcpath}/language.us.lua \
        ${destroot}${texlive_texmfsysconfig}/language.d/00language.us.lua
    xinstall -d ${destroot}${texlive_texmfsysvar}/web2c
    touch ${destroot}${texlive_texmfsysvar}/web2c/updmap.cfg
    touch ${destroot}${texlive_texmfsysvar}/web2c/fmtutil.cnf
    xinstall -d ${destroot}${texlive_texmfsysvar}/tex/generic/config
    touch ${destroot}${texlive_texmfsysvar}/tex/generic/config/language.dat
    touch ${destroot}${texlive_texmfsysvar}/tex/generic/config/language.def
    touch ${destroot}${texlive_texmfsysvar}/tex/generic/config/language.dat.lua
    xinstall -m 755 ${worksrcpath}/texlive-update-cnf \
        ${destroot}${prefix}/libexec/texlive-update-cnf

    # Install texmf.cnf files
    touch ${destroot}${texlive_texmfsysconfig}/texmf.cnf
    xinstall -d ${destroot}${texlive_texmfsysconfig}/texmf.cnf.d
    foreach cnffile [glob -directory ${worksrcpath}/texmf.cnf.d *.cnf] {
        xinstall -m 644 $cnffile \
            ${destroot}${texlive_texmfsysconfig}/texmf.cnf.d/
    }

    # Symlink texmf.cnf to its usual expected location, just in case
    # anything tries to look for it there
    xinstall -d ${destroot}${texlive_texmfports}/web2c
    ln -s ${texlive_texmfsysconfig}/texmf.cnf \
        ${destroot}${texlive_texmfports}/web2c/texmf.cnf

    # Install texmfcnf.lua (for ConTeXt MkIV)
    xinstall -m 644 ${worksrcpath}/texmfcnf.lua \
        ${destroot}${texlive_texmfsysconfig}/

}

pre-activate {
    # Delete ls-R index file if it exists
    foreach texmfdir $texmfdirs {
        if [file exists ${texmfdir}/ls-R] {
            delete ${texmfdir}/ls-R
        }
    }

    # An earlier version forgot to register language.def to this port,
    # so delete it if it exists to prevent a conflict
    if [file exists ${texlive_texmfsysvar}/tex/generic/config/language.def] {
        delete ${texlive_texmfsysvar}/tex/generic/config/language.def
    }

    # Deactivate tetex and any of the texlive 2007 ports. replaced_by
    # does not accomplish this because not all of these ports conflict
    # with this one (although they will cause problems later).
    set deactivate_proc registry_deactivate_composite
    if {[info commands registry_deactivate_composite] == {}} {
        set deactivate_proc registry_deactivate
    }
    foreach oldport {texlive_base texlive_texmf-full texlive_texmf-minimal texlive_texmf-docs teTeX} {
        if {![catch {registry_active $oldport}]} {
            $deactivate_proc $oldport "" [list ports_nodepcheck 1]
        }
    }
}

post-activate {
    system "${prefix}/libexec/texlive-update-cnf texmf.cnf"
    system "${prefix}/libexec/texlive-update-cnf fmtutil.cnf"
    system "${prefix}/libexec/texlive-update-cnf language.dat"
    system "${prefix}/libexec/texlive-update-cnf language.def"
    system "${prefix}/libexec/texlive-update-cnf language.dat.lua"
    system "${prefix}/libexec/texlive-update-cnf updmap.cfg"

    # run mktexlsr if it exists (i.e. if we are upgrading an existing
    # installation)
    if [file exists ${prefix}/bin/mktexlsr] {
        texlive.mktexlsr
    }
}