# -*- 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                tcl
version             8.6.17
# Tk (x11/tk) port depends on this version
categories          lang
license             Tcl/Tk
maintainers         {mcalhoun @MarcusCalhoun-Lopez} {gmx.us:chrischavez @chrstphrchvz} openmaintainer
platforms           darwin freebsd
description         Tool Command Language
long_description    \
    Tcl (Tool Command Language) is a very powerful but easy to learn dynamic \
    programming language, suitable for a very wide range of uses, including web \
    and desktop applications, networking, administration, testing and many more. \
    Open source and business-friendly, Tcl is a mature yet evolving language \
    that is truly cross platform, easily deployed and highly extensible.

homepage            https://www.tcl-lang.org

if {$subport eq $name} {
    revision        0
    checksums       rmd160  7ba0f589c661c8dd94b9733ad2c5436eb91c1919 \
                    sha256  a3903371efcce8a405c5c245d029e9f6850258a60fa3761c4d58995610949b31 \
                    size    11724552
}

subport tcl9 {
    version         9.0.4
    revision        0
    checksums       rmd160  41179b681eb73238153889e4ad7920e6646e6a71 \
                    sha256  d0aed49230bc02a65c1e0229e65f34590a4b037ec40d546f32573b467f7551ea \
                    size    11866337

    # https://trac.macports.org/ticket/74171
    patchfiles-append   zipfs.patch

    # https://trac.macports.org/ticket/73923
    post-patch {
        reinplace "s|cp -a|cp -pRP|" ${workpath}/tcl${version}/unix/Makefile.in ${workpath}/tcl${version}/pkgs/thread3.0.6/Makefile.in
    }
}

master_sites        sourceforge:project/tcl/Tcl/${version}

distname            ${name}${version}-src
worksrcdir          ${name}${version}/unix
patch.dir           ${workpath}/${name}${version}

depends_lib         port:zlib

configure.args      --disable-corefoundation

if {$subport eq $name} {
    set libdir          ${prefix}/lib
    configure.args-append   --mandir=${prefix}/share/man
} else {
    depends_lib-append      port:libtommath
    set libdir              ${prefix}/lib/${subport}
    configure.cppflags-append \
                            -I${prefix}/include/libtommath
    configure.args-append   --includedir=${prefix}/include/${subport} \
                            --libdir=${libdir} \
                            --mandir=${prefix}/share/${subport}/man \
                            --enable-man-symlinks \
                            --with-system-libtommath \
                            ac_cv_path_tclsh=

    # FIXME: https://trac.macports.org/ticket/73923
    compiler.blacklist-append \
                            *gcc-4.0 *gcc-4.2
    # FIXME: tclLoadDyld.c:449:24: error: assignment to 'const struct mach_header *' from incompatible pointer type 'const struct mach_header_64 *' [-Wincompatible-pointer-types]
    if {${configure.build_arch} in [list arm i386 ppc]} {
        configure.cflags-append \
                            -Wno-error=incompatible-pointer-types
    }

    # As of 9.0.4, the build cannot find its own library...
    build.env-append        DYLD_LIBRARY_PATH=\.:\$DYLD_LIBRARY_PATH
}

configure.cppflags-delete   -I${prefix}/include
configure.ldflags-delete    -L${prefix}/lib

pre-configure {
    # sqlite3 package is provided by sqlite3-tcl
    delete [glob ${worksrcpath}/../pkgs/sqlite*]
}

post-configure {
    reinplace -E {s|-arch [^ ]+||g} ${worksrcpath}/tclConfig.sh
}

# see https://trac.macports.org/ticket/17189
destroot.target-append \
                    install-private-headers
destroot.destdir    INSTALL_ROOT=${destroot}
post-destroot {
    if {$subport eq $name} {
        ln -s tclsh8.6 ${destroot}${prefix}/bin/tclsh
        ln -s libtcl8.6.dylib ${destroot}${prefix}/lib/libtcl.dylib
    }

    if {${configure.ccache}} {
        reinplace {/TCL_CC/s/ccache //} ${destroot}${libdir}/tclConfig.sh
    }
}

if {$subport eq $name} {
    configure.args-append   --disable-threads

    variant threads description {add multithreading support} {
        configure.args-replace --disable-threads \
                               --enable-threads
    }
    default_variants-append +threads
}

platform macosx {
    # CF is not fork()-safe and software e.g. using tk and fork() will crash
    variant corefoundation description {Enable CoreFoundation support (not fork-safe)} {
        configure.args-replace --disable-corefoundation \
                               --enable-corefoundation
    }
    # tk +quartz crashes at launch without CF support
    default_variants-append +corefoundation
}

variant memdebug description {enable memory debugging support} {
    configure.args-append --enable-symbols=mem
}

variant dtrace description {Enable DTrace static probes} {
    configure.args-append --enable-dtrace
}

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

if {$subport eq $name} {
    notes "
    The Sqlite3 Tcl package is now being provided by the sqlite3-tcl port:
    sudo port install sqlite3-tcl
    "
}

test.run            yes

livecheck.type      regex
if {$subport eq $name} {
    livecheck.regex     {Tcl/Tk (8(?:\.\d+)*)</a>}
} else {
    livecheck.regex     {Tcl/Tk (\d+(?:\.\d+)*)</a>}
}
