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

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

PortGroup       legacysupport 1.1
# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name            texlive-bin
version         2025.74524
revision        1

categories      tex
maintainers     {dports @drkp}
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/

# 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 (and configure) time, it omits a number of libraries and
# utilities that we don't build. However, the port will still work
# with an unmodified texlive distfile.
master_sites    https://www.ambulatoryclam.net/texlive/

use_xz          yes
distname        texlive-source-${version}-stripped
worksrcdir      ${distname}

# ICU requires C++17
compiler.cxx_standard \
                2017


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

checksums           texlive-source-2025.74524-stripped.tar.xz \
                    rmd160  a42756b12dd28f851ed99fef5347205a0a2f6858 \
                    sha256  38fd1e3d7fa68f6f66300f7e4802653037639e6a35cabeec85a36bd7c4a4385e \
                    size    52422844 \
                    tlpkg-TeXLive-2025.74524.tar.xz \
                    rmd160  08c94126bdd4873e8485dc9636d28058a3fabe2b \
                    sha256  23fdd34031780d08da2c9c37a55a0436d0c096ac112daa747447f7698d8e0318 \
                    size    119052

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

depends_run     port:ghostscript

depends_build-append \
                path:bin/perl:perl5 \
                port:pkgconfig

# patches related to changes in install paths
patchfiles-append  \
                   patch-texk_tex4htk_Makefile.in.diff \
                   patch-texk_kpathsea_Makefile.in.diff \
                   patch-texk_chktex_Makefile.in.diff \
                   patch-texk_texlive_linked_scripts_Makefile.in.diff \
                   patch-texk_xdvik_xdvi-sh.in.diff \
                   patch-utils_texdoctk_Makefile.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

# disable luazlib's zlib version check since it is way too specific;
# see https://trac.macports.org/ticket/69182
patchfiles-append  patch-texk_web2c_luatexdir_luazlib_lzlib.c.diff

# patch to fix broken luatex integral symbol; see
# https://tug.org/pipermail/tldistro/2025q1/000517.html
patchfiles-append  patch-r74657-74661-74678.diff

# patch to fix dvipdmx segfault issue; see (again)
# https://tug.org/pipermail/tldistro/2025q1/000517.html
patchfiles-append  patch-r74687.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
    reinplace "s|@@TEXMFDIST@@|${texlive_texmfdist}|" ${worksrcpath}/utils/texdoctk/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

}


set build_dir ${workpath}/build
pre-configure {
    file mkdir ${build.dir}
}
configure.dir ${build_dir}
configure.cmd ${worksrcpath}/configure
build.dir ${build_dir}

# 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 \
                --without-system-xpdf \
                --with-system-zlib \
                --with-system-zziplib \
                --disable-biber \
                --disable-etex \
                --disable-detex \
                --disable-dvi2tty \
                --disable-dvipng \
                --disable-dvisvgm \
                --disable-lcdf-typetools \
                --disable-ps2eps \
                --disable-psutils \
                --disable-t1utils \
                --disable-texinfo \
                --disable-xindy \
                --without-x \
                --without-pdfopen \
                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
    }
}

# luajit uses the svn revision number in the library version which
# older versions of Xcode's ld do not like:
#    ld: malformed 64-bit  a.b.c.d.e version number: 4.81742
if {[vercmp ${xcodeversion} 16] < 0} {
    post-patch {
        reinplace -E {/^LUAJIT_LT_VERSINFO=/s/([0-9]+)\:([0-9])+\:([0-9])+/\1:0:\3/} ${worksrcpath}/libs/luajit/configure
    }
}

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
configure.args.ppc-append   --disable-luajittex
configure.args.ppc64-append --disable-luajittex

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

# this is not optimal, but we have to duplicate this cxx11 PG logic here
# because of https://trac.macports.org/ticket/56280
if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
    lappend configure.objcxxflags -stdlib=${configure.cxx_stdlib}
}
if {[string match macports-libstdc++ ${configure.cxx_stdlib}]} {
        lappend configure.objcxxflags -D_GLIBCXX_USE_CXX11_ABI=0
}

foreach arch {arm64 x86_64 i386 ppc ppc64} {
    build.env.${arch}-append \
                            "OBJCFLAGS=${configure.objcflags} [muniversal::get_archflag objc ${arch}]" \
                            "OBJCXXFLAGS=${configure.objcxxflags} [muniversal::get_archflag objcxx ${arch}]"
}

# work around differences between libc++ and libstdc++ headers building with clang
# https://trac.macports.org/ticket/54358#comment:26
if {[string match *clang* ${configure.cxx}]} {
    configure.cxxflags-append -std=c++17
    configure.cxxflags-append -Wno-reserved-user-defined-literal
}

# Useful variables to set up the TeXDist directory structure.
set texdist_root         /Library/TeX/Distributions
set texdist_fd           .FactoryDefaults
if {[string equal ${prefix} {/opt/local}]} {
    set texdist_name     MacPorts-TeXLive
} else {
    regsub -all {/} [string trim ${prefix} {/}] {_} prefix_
    set texdist_name     MacPorts-${prefix_}-TeXLive
}
set texdist_mp           ${texdist_root}/${texdist_name}.texdist
set texdist_mpfd         ${texdist_root}/${texdist_fd}/${texdist_name}/Contents

post-destroot   {
    # 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"
        }
    }
}

post-activate {
    # Run mktexlsr if possible. Since mktexlsr is installed by
    # texlive-basic, it won't exist on a clean install, but we should
    # run it if upgrading.
    if {[file exists ${prefix}/bin/mktexlsr]} {
        ui_debug {Rebuilding TeX ls-R filename databases}
        system "${prefix}/bin/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
}

# Install the TeXDist directory structure describing MacPorts' TeX Live.
#
# It is used to switch between TeX distribution on macOS by tools such as
# the TeX Live Utility, the texdist script, the TeX Distribution
# Preference Pane, and the LocalTeX Preference Pane.
# For more details, see
#   - the file TeXDist-description.rtf (installed by MacTeX/BasicTeX in
#     /Library/TeX/Distributions),
#   - MacTeX/BasicTeX's postinstall script,
#   - the postinstall script in https://github.com/TeXLive-M/TeXDist, and
#   - http://www.tug.org/mactex/multipletexdistributions.html .
#
# With respect to TeXDist-description.rtf and the postinstall scripts,
# we do not install
#   - /usr/texbin,
#   - /Library/TeX/Distributions/.DefaultTeX,
#   - anything in /Library/TeX outside /Library/TeX/Distributions.
# In fact they are useful only when other distributions are installed,
# and they install those missing pieces.
#
# MacTeX/BasicTeX's postinstall (but not the other one) install also
# symlinks Local/TEXMF{HOME,VAR,CONFIG} inside Contents, but they are not
# documented in TeXDist-description.rtf: we do not install them.
#
# These are installed outside MacPorts's prefix, so we do not install
# them when running as a non-root user.
if {[getuid] == 0} {

    destroot.violate_mtree  yes

    post-destroot {
        # 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_exists universal] && [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}

        # The directories MacPorts-TeXLive.texdist and MacPorts-teTeX.texdist in
        # /Library/TeX/Distributions/ and MacPorts-TeXLive and MacPorts-teTeX in
        # /Library/TeX/Distributions/.FactoryDefaults/ are considered by
        # TeX Live / MacTeX developers as owned by MacPorts (see
        # https://github.com/macports/macports-ports/pull/6755#issuecomment-613055213).
        #
        # Moreover, they are useful only if MacPorts' TeX Live is installed, so
        # removing the old ones here and not restoring them in post-deactivate
        # does no harm to the user. Consider also that teTeX does not exist
        # anymore in MacPorts.

        xinstall -d ${destroot}${texdist_mp}
        ln -s ../${texdist_fd}/${texdist_name}/Contents ${destroot}${texdist_mp}/Contents

        xinstall -d ${destroot}${texdist_mpfd}/AllTexmf
        ln -s ${texlive_texmfports} ${destroot}${texdist_mpfd}/AllTexmf/texmf
        ln -s ${texlive_texmfdist} ${destroot}${texdist_mpfd}/AllTexmf/texmf-dist
        ln -s ${texlive_texmfdist}/doc ${destroot}${texdist_mpfd}/AllTexmf/texmf-doc
        ln -s ${texlive_texmflocal} ${destroot}${texdist_mpfd}/AllTexmf/texmf-local
        ln -s ${texlive_texmfsysvar} ${destroot}${texdist_mpfd}/AllTexmf/texmf-var

        xinstall -d ${destroot}${texdist_mpfd}/Doc
        ln -s ${texlive_texmfdist}/doc ${destroot}${texdist_mpfd}/Doc/texmf-dist-doc
        ln -s ${texlive_texmfports}/doc ${destroot}${texdist_mpfd}/Doc/texmf-doc
        ln -s ${texlive_texmfdist}/doc ${destroot}${texdist_mpfd}/Doc/texmf-doc-doc
        ln -s ${texlive_texmfdist}/doc ${destroot}${texdist_mpfd}/Doc/texmf-var-doc

        ln -s ${prefix}/share/info ${destroot}${texdist_mpfd}/Info
        ln -s ${prefix}/share/man ${destroot}${texdist_mpfd}/Man

        # The texdist script determines if a TeX distribution is usable
        # by testing the binaries in the directory called `uname -p`.
        # `uname -p` outputs i386 also for x86_64 systems.
        xinstall -d ${destroot}${texdist_mpfd}/Programs
        if {[variant_exists universal] && [variant_isset universal]} {
            if {[string match "*ppc*" ${configure.universal_archs}]} {
                ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/powerpc
                ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/ppc
            }
            if {"x86_64" in ${configure.universal_archs} || "i386" in ${configure.universal_archs}} {
                ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/i386
            }
            if {"x86_64" in ${configure.universal_archs}} {
                ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/x86_64
            }
        } else {
            if {[string match "ppc*" ${configure.build_arch}]} {
                ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/powerpc
                ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/ppc
            } else {
                if {${configure.build_arch} eq "x86_64"} {
                    ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/i386
                }
                ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/${configure.build_arch}
            }
        }
        ln -s ${texlive_mactex_texbindir} ${destroot}${texdist_mpfd}/Programs/texbin

        xinstall -d ${destroot}${texdist_mpfd}/Resources/English.lproj
        xinstall -m 644 ${filespath}/Description.rtf \
            ${destroot}${texdist_mpfd}/Resources/English.lproj/Description.rtf
        reinplace "s|%%PREFIX%%|${prefix}|g" \
            ${destroot}${texdist_mpfd}/Resources/English.lproj/Description.rtf
        reinplace "s|%%VERSION%%|${version}|g" \
            ${destroot}${texdist_mpfd}/Resources/English.lproj/Description.rtf

        ln -s ${prefix}/share ${destroot}${texdist_mpfd}/Root

        set texdist_vfile [open ${destroot}${texdist_mpfd}/TeXDistVersion w]
        puts ${texdist_vfile} "1"
        close ${texdist_vfile}

        ln -s ${texlive_texmflocal} ${destroot}${texdist_mpfd}/TexmfLocal
        ln -s ${texlive_texmfsysvar} ${destroot}${texdist_mpfd}/TexmfSysVar
    }

    pre-activate {
        # Delete leftover MacPorts-related TeXDist directories.
        # See comment above in post-destroot for the rationale.
        if {[string equal ${prefix} {/opt/local}]} {
            set texdist_delete_dist [list TeXLive teTeX]
            foreach dist ${texdist_delete_dist} {
                set texdist_delete_dir [list \
                                            ${texdist_root}/MacPorts-${dist}.texdist \
                                            ${texdist_root}/${texdist_fd}/MacPorts-${dist} \
                                           ]
                foreach dir ${texdist_delete_dir} {
                    if {[file exists ${dir}]} {
                        delete file ${dir}
                    }
                }
            }
        }
    }
}

default_variants    +x11

livecheck.type      none
