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

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   md5     63a13111a136118ec72faee1228143e6 \
                sha1    a7593f9f62e41eb0edab0deb0e0b92a62ec219b9 \
                rmd160  9e2248f4f97bd939bada96cf8ddf16f8b8c44fc6 \
                sha256  95cd528a80f5e4bdb557af9b14a7197d6860793a3894e25e7c9fad2ed05d4c3c

    livecheck.regex     {Tcl/Tk (8(?:\.\d+)*)</a>}
} elseif {$subport in {tk9-quartz tk9-x11}} {
    version     9.0.4
    checksums   md5     1b133d4ddf3dec2c01340cdc68c87096 \
                sha1    ca517d325695cdd1d5fe71ae5bb1ae724d51d00b \
                rmd160  ef0356645a77429ecf7da590f9cf2eeaa89d498d \
                sha256  d7a146d2917eb8b5cc95276dbf0e3d03c7464d2b19c1675357857c989301dbb4

    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

    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.args      --with-tcl=${prefix}/lib/${tclport} \
                        --bindir=${bindir} \
                        --includedir=${incdir} \
                        --libdir=${libdir} \
                        --mandir=${mandir} \
                        --enable-man-symlinks
    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/${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}
    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}
            }
        }
    }
}

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