# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 90697 2012-03-12 19:25:53Z ryandesign@macports.org $

PortSystem      1.0

name            poco
version         1.3.6p2
revision        1
categories      devel
maintainers     nomaintainer
platforms       darwin
description     POCO C++ Libraries

long_description \
    POCO aims to be for network-centric, cross-platform C++ software development \
    what Apple's Cocoa is for Mac development, or Ruby on Rails is for Web \
    development -- a powerful, yet easy to use platform to build your applications upon.

homepage        http://pocoproject.org/
master_sites    sourceforge:project/poco/sources/poco-[strsed ${version} {s/[^0-9.].*$//}]
use_bzip2       yes
distname        ${name}-${version}-all
set docdistname ${name}-[strsed ${version} {s/[a-z].*$//}]-all-doc

checksums       [suffix ${distname}] \
                    md5     8f1a6c3511764167d39f1950da3fcb37 \
                    sha1    46c66620f7685e9ed9f7caffc8ff3a09454c06de \
                    rmd160  effaff38aebca34af7dbbd3aae0798af95268790 \
                ${docdistname}.tar.gz \
                    md5     a6abb9a02b0a7406194853d422d5fc28 \
                    sha1    2167220ad0c354c3ae8a9ea1be4a7d91d73976a3 \
                    rmd160  78b82bd8dd19dd4bb599543d860a1aae8585a64f

set omit        {NetSSL_OpenSSL Data/ODBC Data/MySQL Crypto}

# Poco build doesn't support the standard --target config option.
if {$build_arch == "x86_64"} {
    set config      Darwin_x86_64
} else {
    set config      Darwin
}

universal_variant   no

# Something in the dependency checking matches the macports.org in the path
# as a *.o file. It's all downhill from there. This patch removes the
# dependency (.dep) file creation & use from the Makefiles.
#
# Note future maintainers / contributors: you will find you won't need
# this patch when building outside the *macports.org* path. Don't remove it
# without testing within the *macports.org* build path. See ticket #19486.
patchfiles          patch-rules-deps.diff

configure.args      --no-samples --config=${config}

# Make sure poco build uses the right compiler.
build.args-append   CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    CPP=${configure.cpp}

pre-configure {
    configure.args-append   --omit=[join ${omit} ,]

    reinplace -E "/^C(XX)?FLAGS +=/s|\$| ${configure.cppflags} ${configure.cflags}|" \
        ${worksrcpath}/build/config/${config}
    reinplace -E "/^((SH|DY)LIB|LINKFLAGS) +=/s|\$| -L\$(LIBPATH) ${configure.ldflags}|" \
        ${worksrcpath}/build/config/${config}
}

use_parallel_build  yes

build.target    poco

test.run        yes

test {
    proc luniq {list} {
        set x() {}; unset x
        foreach {item} ${list} {
            set x(${item}) ""
        }
        return [array names x]
    }

    set file [open ${worksrcpath}/components]

    set components {}
    while {[gets ${file} component] != -1} {
        lappend components ${component}
    }

    close ${file}

    foreach {component} [luniq ${components}] {
        foreach {testrunner} [glob -nocomplain ${worksrcpath}/${component}/testsuite/bin/*/*/testrunner{,d}] {
            catch {system "${testrunner} -all -print"}
        }
    }
}

set docdir      ${prefix}/share/doc/${name}-${version}

post-destroot {
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} CHANGELOG CONTRIBUTORS LICENSE NEWS README VERSION \
        ${destroot}${docdir}

    if {![variant_isset doc]} {
        xinstall -d ${destroot}${docdir}/html
        xinstall -m 0644 -W ${worksrcpath}/doc Acknowledgements.html ${destroot}${docdir}/html
    }

    set libdir ${prefix}/lib

    foreach {dylib} [glob -types f -tails -directory ${destroot}${libdir} *.dylib] {
        system "install_name_tool -id ${libdir}/${dylib} ${destroot}${libdir}/${dylib}"

        set otool [lrange [split [exec otool -L ${destroot}${libdir}/${dylib}] \n] 2 end]

        foreach {otoolentry} ${otool} {
            set lib [lindex ${otoolentry} 0]

            if {! [string match ${worksrcpath}/* ${lib}]} {
                continue
            }

            system "install_name_tool -change ${lib} ${libdir}/[file tail ${lib}] \
                ${destroot}${libdir}/${dylib}"
        }
    }
}

variant ssl description {Build NetSSL and Crypto library} {
    depends_lib-append      port:openssl
    set omit [ldelete [ldelete ${omit} NetSSL_OpenSSL] Crypto]
}

variant odbc description {Build ODBC backend for Data library} {
    depends_lib-append      lib:libiodbc:unixODBC
    set omit [ldelete ${omit} Data/ODBC]
}

variant mysql description {Build MySQL backend for Data library} {
    depends_lib-append      path:bin/mysql_config5:mysql5

    post-configure {
        reinplace -E "/^C(XX)?FLAGS +=/s|\$| -I${prefix}/include/mysql5/mysql|" \
            ${worksrcpath}/build/config/${config}
        reinplace -E "/^((SH|DY)LIB|LINKFLAGS) +=/s|\$| -L${prefix}/lib/mysql5/mysql|" \
            ${worksrcpath}/build/config/${config}
    }

    set omit [ldelete ${omit} Data/MySQL]
}

variant doc description {Install extra documentation} {
    extract.only            ${distfiles}
    distfiles-append        ${docdistname}.tar.gz

    post-extract {
        system "tar xzf ${distpath}/${docdistname}.tar.gz -C ${workpath}"
    }

    post-destroot {
        set workdocpath ${workpath}/${docdistname}

        xinstall -d ${destroot}${docdir}/html/CppUnit
        eval xinstall -m 0644 [glob -directory ${workdocpath} *.html] ${destroot}${docdir}/html
        eval xinstall -m 0644 [glob -directory ${worksrcpath}/CppUnit/doc *] ${destroot}${docdir}/html/CppUnit

        foreach {dir} {images css} {
            xinstall -d ${destroot}${docdir}/html/${dir}
            eval xinstall -m 0644 [glob -directory ${workdocpath}/${dir} *] ${destroot}${docdir}/html/${dir}
        }
    }
}

livecheck.regex     /${name}-(\[0-9.p\]+)${extract.suffix}
