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

# luajittex requires muniversal (and some patches to configure
# scripts) to build universal
PortGroup       muniversal 1.0

name            texlive-bin
version         2016

categories      tex
maintainers     dports
description     TeX Live Binaries.
long_description    TeX Live is an easy way to get up and running with TeX. \
    It provides a comprehensive TeX system with binaries for most flavors \
    of Unix, including GNU/Linux, and also Windows. It includes all the \
    major TeX-related programs, macro packages, and fonts that are free \
    software, including support for many languages around the world.

homepage        http://www.tug.org/texlive/
platforms       darwin

# This port installs two libraries: kpathsea (LGPL-2.1+) and ptexenc (BSD).
# The binaries are covered under various licenses, but all distributable;
# see the texlive portfile for more information.
license         Copyleft Permissive LGPL-2.1+ BSD

# Our distfile is a stripped-down version of the texlive source
# tarball, available from CTAN in systems/texlive/Source. For faster
# download time, it omits a number of libraries and utilities that we
# don't build. However, the port should still work with an unmodified
# texlive distfile.
master_sites    http://giraffe.cs.washington.edu/texlive/
set distversion 20160605
use_xz          yes
distname        texlive-source-${distversion}-stripped
worksrcdir      ${distname}

set tlpkgdistname   tlpkg-TeXLive-${distversion}
distfiles-append    ${tlpkgdistname}${extract.suffix}

checksums           texlive-source-20160605-stripped.tar.xz \
                    rmd160  6458766f5dd3e4f00c529fe1762a56a154383bc4 \
                    sha256  1b3f88056a2cb9273f01df22f67c24db8b5f73c967d6ddb30abc6e69fad424c6 \
                    tlpkg-TeXLive-20160605.tar.xz \
                    rmd160  066447d2f6a358a3633c997fe4b6194afae48a1c \
                    sha256  1cd8cb957d0c442be20451e661dc936f7aedaf41d4c603cb7b6ca0c12a044795

depends_lib     port:fontconfig \
                port:freetype \
                port:libpng \
                port:ncurses \
                port:zlib \
                port:libzzip \
                port:poppler \
                path:lib/pkgconfig/cairo.pc:cairo \
                path:lib/pkgconfig/pixman-1.pc:libpixman \
                port:graphite2 \
                port:icu \
                port:harfbuzz \
                port:harfbuzz-icu \
                port:libpaper \
                port:gmp \
                port:mpfr \
                port:potrace \
                port:texlive-common

depends_run     port:ghostscript

depends_build   path:bin/perl:perl5 \
                path:bin/pkg-config:pkgconfig

# patches related to changes in install paths
patchfiles-append  patch-texk_chktex_Makefile.in.diff \
                   patch-texk_kpathsea_Makefile.in.diff \
                   patch-texk_tex4htk_Makefile.in.diff \
                   patch-texk_texlive_linked_scripts_Makefile.in.diff \
                   patch-texk_texlive_tl_scripts_Makefile.in.diff \
                   patch-texk_xdvik_xdvi-sh.in.diff
                   
# patches to luajit/luajittex's config scripts to correctly detect
# architecture in universal builds
patchfiles-append  patch-libs_luajit_configure.diff \
                   patch-texk_web2c_configure.diff

post-patch {
    reinplace "s|@@TEXMFDIST@@|${texlive_texmfdist}|" ${worksrcpath}/texk/texlive/linked_scripts/Makefile.in
    reinplace "s|@@TEXMFDIST@@|${texlive_texmfdist}|" ${worksrcpath}/texk/texlive/tl_scripts/Makefile.in
    reinplace "s|@@TEXMFDIST@@|${texlive_texmfdist}|" ${worksrcpath}/texk/tex4htk/Makefile.in
    reinplace "s|@@TEXLIVE_BINDIR@@|${texlive_bindir}|" ${worksrcpath}/texk/xdvik/xdvi-sh.in
    reinplace "s|@@TEXMFDIST@@|${texlive_texmfdist}|" ${worksrcpath}/texk/chktex/Makefile.in

    # Defaults for the texmf search paths need to be compiled into
    # libkpathsea -- really, just the location of texmf.cnf, which
    # defines all the other paths. texlive normally finds this file
    # using a bindir-relative path extracted from texmf.cnf, but this
    # is both incorrect (it fails to find our texmf.cnf) and overkill
    # (we know the absolute path of texmf.cnf).
    #
    # Accordingly, we patch the kpathsea makefile to not try to derive
    # the default paths from texmf.cnf, and just provide our own
    # paths.h with the right path substituted in.
    delete ${worksrcpath}/texk/kpathsea/texmf.cnf
    file copy ${filespath}/texk_kpathsea_paths.h ${worksrcpath}/texk/kpathsea/paths.h
    reinplace "s|@@PREFIX@@|${prefix}|" ${worksrcpath}/texk/kpathsea/paths.h
    reinplace "s|@@TEXMFSYSCONFIG@@|${texlive_texmfsysconfig}|" ${worksrcpath}/texk/kpathsea/paths.h
}

# llvm-gcc apparently fails on Xcode 4.0.x (#30758)
compiler.blacklist {llvm-gcc-4.2 < 2335.15}

# Fix build failure on Tiger due to use of -isystem
compiler.blacklist-append gcc-4.0 gcc-3.3

# We use MacPorts-provided libraries instead of the ones included in
# texlive whenever possible, to avoid redundancy and to better keep
# current with respect to security updates and the like.
#
# At present, the only exceptions are TeX-specific libraries like
# kpathsea and ptexenc that it doesn't make sense to package
# separately.
#
# Many of the --with-system-* and --disable-* flags are actually
# redundant because we've removed those components from the distfile,
# but we leave them here for compatibility with the stock distfile.
configure.args  --bindir=${texlive_bindir} \
                --mandir=${texlive_bindir} \
                --infodir=${prefix}/share/info \
                --enable-shared \
                --disable-multiplatform \
                --disable-native-texlive-build \
                --without-old-mac-fonts \
                --with-banner-add="/MacPorts ${version}_${revision}" \
                --with-system-freetype2 \
                --without-system-freetype \
                --without-system-icu \
                --with-system-cairo \
                --with-system-gd \
                --with-system-gmp \
                --with-system-graphite2 \
                --with-system-harfbuzz \
                --with-system-icu \
                --with-system-libpaper \
                --with-libpaper-includes=${prefix}/include \
                --with-libpaper-libdir=${prefix}/lib \
                --with-system-libpng \
                --with-libpng-includes=${prefix}/include \
                --with-libpng-libdir=${prefix}/lib \
                --with-system-mpfr \
                --with-system-ncurses \
                --with-system-pixman \
                --with-system-poppler \
                --with-system-potrace \
                --with-system-xpdf \
                --with-system-zlib \
                --with-system-zziplib \
                --disable-biber \
                --disable-etex \
                --disable-detex \
                --disable-dvi2tty \
                --disable-dvipng \
                --disable-lcdf-typetools \
                --disable-ps2eps \
                --disable-psutils \
                --disable-t1utils \
                --disable-texinfo \
                --disable-xindy \
                --without-x \
                --without-pdfopen \
                --enable-build-in-source-tree \
                ac_cv_prog_AWK=/usr/bin/awk

# makejvf has some invalid character encodings in one of its source
# files, which cause certain versions of clang to hang (#38643). Using
# -Wno-invalid-source-encoding avoids this problem.
# It appears to be specific to Xcode 4.6, but there's no harm in
# silencing the warning for all versions of clang.
post-patch {
    if {[string match *clang* ${configure.compiler}]} { 
        reinplace "/^WARNING_CFLAGS =/s/$/ -Wno-invalid-source-encoding/" ${worksrcpath}/texk/makejvf/Makefile.in
    }
}

configure.cppflags-delete   -I${prefix}/include
configure.ldflags-delete    -L${prefix}/lib
configure.cppflags-append   -isystem${prefix}/include
configure.env-append        LIBRARY_PATH=${prefix}/lib

# luajittex does not support ppc
if {[variant_isset universal]} {
    set merger_configure_args(ppc) --disable-luajittex
    set merger_configure_args(ppc64) --disable-luajittex
} else {
    switch -- ${configure.build_arch} {
        ppc -
        ppc64 { configure.args-append --disable-luajittex }
    }
}


# work around bug in texlive makefiles where $OBJC is not recorded
# during configure phase
build.env                   OBJC=${configure.objc} \
                            OBJCFLAGS="${configure.objcflags} [get_canonical_archflags objc]" \
                            OBJCXX=${configure.objcxx}

if {[string match *clang* ${configure.cxx}]} {
    build.env-append        OBJCXXFLAGS="${configure.objcxxflags} [get_canonical_archflags objcxx] -stdlib=${configure.cxx_stdlib}"
} else {
    build.env-append        OBJCXXFLAGS="${configure.objcxxflags} [get_canonical_archflags objcxx]"
}

post-destroot   {
    # Not sure why this gets installed into the wrong location, but
    # fix it here.
    move ${destroot}${prefix}/lib/kpathsea/c-auto.h ${destroot}${prefix}/include/kpathsea/c-auto.h
    
    # Anything that gets installed into texmf-dist will be installed
    # by one of the texmf ports
    delete ${destroot}${prefix}/share/texmf-dist
    delete ${destroot}${texlive_texmfdist}

    # Do not install symlinks to scripts that we don't install
    delete ${destroot}${texlive_bindir}/latexmk
    delete ${destroot}${texlive_bindir}/tlmgr    

    # install the tlpkg/TeXLive perl module
    xinstall -d ${destroot}${prefix}/share/tlpkg
    copy ${workpath}/${tlpkgdistname} ${destroot}${prefix}/share/tlpkg/TeXLive

    # gzip manpages that are installed into texlive_bindir
    foreach manpage [glob -directory ${destroot}${texlive_bindir} man*/*] {
        # need to make sure it isn't a symlink
        if {[file type $manpage] == "file"} {
            system "gzip $manpage"
        }
    }

    # Create version/arch-specific links to the texbin directory used
    # by the TeXDist prefpane; it seems that future versions of the
    # prefpane will want these.
    xinstall -d ${destroot}${texlive_mactex_texdistdir}-${version}
    if {[variant_isset universal]} {
        ln -s ${texlive_mactex_texbindir} \
            ${destroot}${texlive_mactex_texdistdir}-${version}/universal-darwin
    } else {
        ln -s ${texlive_mactex_texbindir} \
            ${destroot}${texlive_mactex_texdistdir}-${version}/${configure.build_arch}-darwin
    }
    ln -s ${texlive_mactex_texdistdir}-${version} \
          ${destroot}${texlive_mactex_texdistdir}
}

post-activate {
    system "${texlive_bindir}/mktexlsr"
}

variant x11 description {Build with x11 support, using Xaw widgets for xdvi} {
    depends_lib-append port:xorg-libXp port:xpm
    configure.args-delete --without-x --without-pdfopen
    depends_lib-append port:xorg-libXaw port:xorg-libXi
    configure.args-append --with-xdvi-x-toolkit=xaw
}

variant motif requires x11 description {Use Motif to build xdvi} {
    depends_lib-delete port:xorg-libXaw
    configure.args-delete --with-xdvi-x-toolkit=xaw
    depends_lib-append lib:libXm:openmotif
    configure.args-append --with-xdvi-x-toolkit=motif
}

default_variants    +x11

livecheck.type      regex
livecheck.url       ${homepage}acquire.html
livecheck.regex     "TeX Live (\[0-9\]+) "