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

name                texlive-common
version             2017.1

categories          tex
maintainers         {dports @drkp}
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        https://giraffe.cs.washington.edu/texlive/ \
                    https://alpaca.cs.washington.edu/texlive/ \
                    https://www.ambulatoryclam.net/texlive/
worksrcdir          ${distname}
use_xz              yes

checksums           rmd160  6fb366c4bddd6b98840d1e852af123ad25ffeafa \
                    sha256  feef1314f09f5a13db4cb84803e7f879bb31a8ae50fdd8a89fd92ac1e37cb028

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

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

use_configure   no

build           { }

set texmfdirs [list ${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
    }
}

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)
    #
    # Note that this could fail if mktexlsr is installed but broken
    # during an upgrade, but that's ok (see #48358)
    if [file exists ${prefix}/bin/mktexlsr] {
        catch {texlive.mktexlsr}
    }
}