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

name                tk
version             8.6.18
revision            0
categories          x11
license             Tcl/Tk
maintainers         {mcalhoun @MarcusCalhoun-Lopez} {gmx.us:chrischavez @chrstphrchvz} openmaintainer
description         Tcl Tool Kit
long_description    This is Tk version ${version}, a GUI toolkit for Tcl. The best way to get \
                    started with Tcl is to read ``Tcl and the Tk Toolkit'' by John K.         \
                    Ousterhout, Addison-Wesley, ISBN 0-201-63337-X.
homepage            https://www.tcl-lang.org

subport tk8-quartz  {revision 0}
subport tk8-x11     {revision 0}
subport tk9-quartz  {revision 0}
subport tk9-x11     {revision 0}
subport tk-quartz {
    replaced_by     tk8-quartz
}
subport tk-x11 {
    replaced_by     tk8-x11
}

if {$subport in {tk8-quartz tk8-x11}} {
    checksums       rmd160  9e2248f4f97bd939bada96cf8ddf16f8b8c44fc6 \
                    sha256  95cd528a80f5e4bdb557af9b14a7197d6860793a3894e25e7c9fad2ed05d4c3c \
                    size    4600288

    livecheck.regex {Tcl/Tk (8(?:\.\d+)*)</a>}
} elseif {$subport in {tk9-quartz tk9-x11}} {
    version         9.0.4
    revision        0
    checksums       rmd160  ef0356645a77429ecf7da590f9cf2eeaa89d498d \
                    sha256  d7a146d2917eb8b5cc95276dbf0e3d03c7464d2b19c1675357857c989301dbb4 \
                    size    4613019

    livecheck.regex {Tcl/Tk (\d+(?:\.\d+)*)</a>}
}

master_sites        sourceforge:project/tcl/Tcl/${version}
dist_subdir         tcltk
distname            ${name}${version}-src
worksrcdir          ${name}${version}

if {$subport eq $name} {
    distfiles
    # Not noarch so arch checking works transitively
    test.ignore_archs   yes
    platforms           any
    use_configure       no
    universal_variant   yes
    build               { }
    destroot {
        if {[variant_exists quartz] && [variant_isset quartz]} {
            set tkdepname tk${tkmajor}-quartz
        } else {
            set tkdepname tk${tkmajor}-x11
        }

        set bindir ${prefix}/libexec/${tkdepname}
        ln -s {*}[glob -directory ${bindir} *] ${destroot}${prefix}/bin

        set incdir ${prefix}/include/${tkdepname}
        ln -s {*}[glob -directory ${incdir} *] ${destroot}${prefix}/include

        set libdir ${prefix}/lib/${tkdepname}
        ln -s {*}[glob -types f -directory ${libdir} *] ${destroot}${prefix}/lib
        ln -s {*}[glob -types d -directory ${libdir} tk*] ${destroot}${prefix}/lib
        ln -s ${libdir}/pkgconfig/tk.pc ${destroot}${prefix}/lib/pkgconfig

        set mandir ${prefix}/share/${tkdepname}/man
        foreach sect {1 3 n} {
            xinstall -d ${destroot}${prefix}/share/man/man${sect}
            ln -s {*}[glob -directory ${mandir}/man${sect} *] ${destroot}${prefix}/share/man/man${sect}
        }
    }

    # tk metaport depends on tk 8 for now - can revisit this when all
    # dependents switch to depending on a specific subport.
    set tkmajor 8

    # TODO: unexclude powerpc here, once quartz is fixed (but not prior to that):
    if {${os.platform} eq "darwin" && ${os.subplatform} eq "macosx"
        && ${os.major} >= 10 && ![string match ppc* ${build_arch}]} {
        variant quartz conflicts x11 {}
        # The buildbot uses default variants, so binaries for ports
        # that don't depend on a specific tk subport will be linked
        # with tk-quartz, which is thus always needed.
        depends_lib port:tk${tkmajor}-quartz

        variant x11 conflicts quartz {
            depends_lib-append port:tk${tkmajor}-x11
        }

        if {![variant_isset x11]} {
            default_variants-append +quartz
        }
    } else {
        depends_lib port:tk${tkmajor}-x11
    }
    livecheck.type      none
} else {
    depends_build       path:bin/pkg-config:pkgconfig
    if {$subport in {tk8-quartz tk8-x11}} {
        set tclport tcl8
    } else {
        set tclport tcl9
    }
    depends_lib         port:${tclport}

    configure.dir       ${worksrcpath}/unix
    build.dir           ${configure.dir}

    set bindir          ${prefix}/libexec/${subport}
    set incdir          ${prefix}/include/${subport}
    set libdir          ${prefix}/lib/${subport}
    set mandir          ${prefix}/share/${subport}/man

    configure.env       {TK_LIBRARY=$(libdir)/tk$(VERSION)}

    configure.args      --bindir=${bindir} \
                        --enable-man-symlinks \
                        --includedir=${incdir} \
                        --libdir=${libdir} \
                        --mandir=${mandir} \
                        --with-tcl=${prefix}/lib/${tclport}

    # FIXME: cups is broken.
    # tkUnixPrint.c:158: error: ‘CUPS_PRINT_COLOR_MODE_COLOR’ undeclared here (not in a function)
    # TODO: consider building against cups port.
    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        if {$subport in {tk9-quartz tk9-x11}} {
            configure.args-append \
                        --disable-libcups
        }
    }

    platform darwin {
        configure.args-append \
                        tcl_cv_type_64bit="long long"
    }

    # see https://trac.macports.org/ticket/58447
    configure.cppflags-replace \
                        -I${prefix}/include -isystem${prefix}/include/${tclport}
    configure.ldflags   -L${worksrcpath} -L${prefix}/lib/${tclport}

    # see https://trac.macports.org/ticket/17189
    destroot.target-append \
                        install-private-headers
    post-destroot {
        if {$subport in {tk8-quartz tk8-x11}} {
            ln -s libtk8.6.dylib ${destroot}${libdir}/libtk.dylib
            ln -s wish8.6 ${destroot}${bindir}/wish
        }
    
        foreach fl {tk3d.h tkFont.h} {
            xinstall -m 0644 ${workpath}/tk${version}/generic/${fl} ${destroot}${incdir}
        }
    }

    test.run            yes

    livecheck.type      none
}

if {$subport in {tk8-quartz tk9-quartz}} {
    platforms       {macosx >= 10}

    # FIXME: this fixes the build, but it crashes at startup on GetScreen.
    if {$subport eq "tk8-quartz"} {
        patchfiles-append \
            patch-fix-quartz.diff
    }

    # To facilitate debugging:
    platform powerpc {
        configure.args-append \
                    --enable-symbols
        configure.optflags
    }

    configure.args-append \
                    --enable-aqua

    post-destroot {
        move \
            ${destroot}${incdir}/X11 \
            ${destroot}${incdir}/X11_tk
        fs-traverse fl [list ${destroot}${incdir}] {
            if {[file extension ${fl}] eq ".h"} {
                reinplace -q "s|include <X11/|include <X11_tk/|g" ${fl}
                reinplace -q "s|include \"X11/|include \"X11_tk/|g" ${fl}
            }
        }
    }
}

if {$subport in {tk8-x11 tk9-x11}} {
    depends_lib-append \
                    path:lib/pkgconfig/fontconfig.pc:fontconfig \
                    port:Xft2 \
                    port:xorg-libXScrnSaver
    configure.ldflags-append \
                    -lfontconfig
}
