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

name                tk
version             8.6.15
revision            1
categories          x11
license             Tcl/Tk
maintainers         {mcalhoun @MarcusCalhoun-Lopez} {gmx.us:chrischavez @chrstphrchvz} openmaintainer
description         Tcl Tool Kit
homepage            https://www.tcl-lang.org/
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.

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

checksums           md5 6d64b6eb021062f378017d403fedcbe6 \
                    sha1 ef44f7be00d0249e281ad87785650f5475bbc939 \
                    rmd160 3cb0ef6823829ed2f7ff0870c7754ea8be20b82a \
                    sha256 550969f35379f952b3020f3ab7b9dd5bfd11c1ef7c9b7c6a75f5c49aca793fec

if {$subport eq $name} {
    distfiles
    # Not noarch so arch checking works transitively
    test.ignore_archs   yes
    platforms       any
    use_configure   no
    build           {}
    destroot {
        if {[variant_exists quartz] && [variant_isset quartz]} {
            set tkdepname tk-quartz
        } else {
            set tkdepname tk-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}
        }
    }
    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-quartz

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

        if {![variant_isset x11]} {
            default_variants +quartz
        }
    } else {
        depends_lib port:tk-x11
    }
    livecheck.type      regex
    # Only check for 8.x versions - 9 will need to be a separate port.
    livecheck.regex     {Tcl/Tk (8(?:\.\d+)*)</a>}
} else {
    depends_build       path:bin/pkg-config:pkgconfig
    depends_lib         port:tcl

    patchfiles          CGRect.patch

    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.args      --bindir=${bindir} \
                        --includedir=${incdir} \
                        --libdir=${libdir} \
                        --mandir=${mandir} \
                        --with-tcl=${prefix}/lib
    configure.env       {TK_LIBRARY=$(libdir)/tk$(VERSION)}
    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
    configure.ldflags   -L${worksrcpath} -L${prefix}/lib

    configure.args.arm64-append     --enable-64bit
    configure.args.x86_64-append    --enable-64bit
    configure.args.ppc64-append     --enable-64bit

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

    test.run            yes

    livecheck.type      none
}

subport tk-quartz {
    platforms       {macosx >= 10}
    supported_archs arm64 i386 x86_64
    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}
            }
        }
    }
}

subport tk-x11 {
    depends_lib-append \
                    port:fontconfig \
                    port:Xft2 \
                    port:xorg-libXScrnSaver
    configure.ldflags-append \
                    -lfontconfig
}
