# -*- 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                zthread
version             2.3.2
categories          devel
license             MIT
maintainers         nomaintainer
                    #  v Note: No https site available
homepage            http://${name}.sourceforge.net
description         object-oriented threading and synchronization \
                    library
long_description    ZThread is an advanced ${description}, implemented \
                    in C++. It provides an excellent and powerful \
                    abstraction from native threads. It includes \
                    interruptible thread objects and several other \
                    synchronization control objects and lets you \
                    safely terminate threads without the complications \
                    of using cancellation handlers or other similar \
                    constructs.

master_sites        sourceforge:project/${name}/ZThread/${version}
distname            ZThread-${version}
checksums           rmd160  8419afad91ea8db1fff7ce3bf7784329839dd2e8 \
                    sha256  950908b7473ac10abb046bd1d75acb5934344e302db38c2225b7a90bd1eda854 \
                    size    412527

patchfiles-append   dynamic_lookup-11.patch
post-patch {
    reinplace -E {s/(aclocal|automake)-\${am__api_version}/\1/} \
        ${worksrcpath}/configure

    # These regexes are similar to bugfixes from the Debian package:
    # "020_fix-g++4.4.patch" and "050_fix-ftbfs-with-g++-4.7.patch"
    foreach re [list \
        {/void createScope.*long ms/s/void/bool/} \
        {/if.*isDisabled/s/(isDisabled)/LockHolder<LockType>::\1/} \
        {/LockingPolicy.*Scope.*extract/s/(extract)/this->\1/} \
    ] {
        reinplace -E $re ${worksrcpath}/include/zthread/Guard.h
    }
    # These regexes are similar to bugfixes from the Debian package:
    # "020_fix-g++4.4.patch"
    foreach re [list \
        {/waiter(Arrived|Departed).*self/s/(waiter(Arrived|Departed))/MutexImpl<List, Behavior>::\1/} \
        {/owner(Acquired|Released).*(self|impl)/s/(owner(Acquired|Released))/MutexImpl<List, Behavior>::\1/} \
    ] {
        reinplace -E $re ${worksrcpath}/src/MutexImpl.h
    }

    # Incorporate bugfix from Debian package:
    # "030_executor-remove-thread-bug"
    reinplace -E \
        {/remove.*threads.*current/s/(std::remove.*);/_threads.erase(\1);/} \
        ${worksrcpath}/src/PoolExecutor.cxx

    # Incorporate bugfix from Debian package: "080_wrong-parameter-type"
    reinplace {/virtual bool add/s/T item/const T\& item/} \
        ${worksrcpath}/include/${name}/BlockingQueue.h

    # Add missing DESTDIR to some of the install paths
    foreach re [list \
        {/mkinstalldirs.*(bin|data)dir/s/(\$.(bin|data)dir)/\$(DESTDIR)\1/} \
        {/INSTALL_PROGRAM.*zthread-config/s/(\$.bindir)/\$(DESTDIR)\1/} \
        {/INSTALL_DATA.*datadir.*aclocal/s/(\$.datadir)/\$(DESTDIR)\1/} \
    ] {
        reinplace -E $re {*}[glob -directory ${worksrcpath} Makefile.*]
    }
}

# Update config.guess and config.sub to be able to build on arm64.
depends_build-append    port:automake
post-patch {
    set automake_dirs [glob -directory ${prefix}/share automake-*]
    set automake_dir [lindex [lsort -command vercmp $automake_dirs] end]
    copy -force ${automake_dir}/config.guess ${automake_dir}/config.sub \
        ${worksrcpath}
}

post-destroot {
    delete {*}[glob -directory ${destroot}${prefix}/include/${name} \
        .Barrier.h.swp Barrier.h.orig]
}

variant docs description {Build documentation} {
    depends_build-append    path:bin/doxygen:doxygen

    post-patch {
        reinplace -E {s/then\\/then \\/} \
            {*}[glob -directory ${worksrcpath} Makefile.*]
        delete {*}[glob -directory ${worksrcpath}/doc \
            CVS cvs.html download.html]
        reinplace -q {/a href=.*(download|cvs).html/d} \
            {*}[glob -directory ${worksrcpath}/doc *.html]
    }

    destroot.target-append  docs
    post-destroot {
        set docdir ${prefix}/share/doc/${name}
        xinstall -d ${destroot}$docdir
        copy {*}[glob ${worksrcpath}/doc/*] ${destroot}$docdir/
        delete {*}[glob -directory ${destroot}$docdir \
            zthread.doxygen header.html footer.html]
        move -force ${destroot}$docdir/html/index.html \
             ${destroot}$docdir/
    }
}

default_variants    +docs
