# -*- 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
# $Id$

PortSystem          1.0
PortGroup           github 1.0
PortGroup           waf 1.0 

github.setup        pfi pficommon 83ea9e102fbe9dfd788f49b69f18585355f64692
version             20141020

categories          devel
maintainers         hum openmaintainer

description         Common Library of C++
long_description    ${name} is a general purpose C++ library.

platforms           darwin
license             BSD

checksums           rmd160  d6db87aeecf629d5591998ce75daad63800580bd \
                    sha256  1249c1fc0d7b125690a318d84bbe7eee60fad463d6c136adad6072edf8ea1540

patchfiles          patch-wscript.diff

platform darwin {
    if {${os.major} <= 10} {
        pre-fetch {
            ui_error "$name does not build on Snow Leopard or earlier."
            error "unsupported platform"
        }
    } elseif {${os.major} >= 13} {
        pre-fetch {
            ui_error "$name does not build on Mavericks or later."
            error "unsupported platform"
        }
    }
}

# optional database: my = MySQL, pg = PostgreSQL.
set my_pkg          mysql56
set pg_version      94

post-patch {
    set mysql_config    ${prefix}/lib/${my_pkg}/bin/mysql_config
    set pg_config       ${prefix}/lib/postgresql${pg_version}/bin/pg_config
    reinplace "s|@mysql_config@|${mysql_config}|g" ${worksrcpath}/src/database/wscript
    reinplace "s|@pg_config@|${pg_config}|g"       ${worksrcpath}/src/database/wscript
}

# msgpack for jubatus.
default_variants    +mprpc

configure.args-append     --disable-mysql \
                          --disable-postgresql \
                          --disable-mprpc \
                          --disable-fcgi \
                          --disable-magickpp 

variant mysql56 conflicts mysql55 mysql51 mariadb percona description {Enable MySQL 5.6} {
    depends_lib-append    port:mysql56
    set my_pkg            mysql56
    configure.args-delete --disable-mysql
}

variant mysql55 conflicts mysql56 mysql51 mariadb percona description {Enable MySQL 5.5} {
    depends_lib-append    port:mysql55
    set my_pkg            mysql55
    configure.args-delete --disable-mysql
}

variant mysql51 conflicts mysql56 mysql55 mariadb percona description {Enable MySQL 5.1} {
    depends_lib-append    port:mysql51
    set my_pkg            mysql51
    configure.args-delete --disable-mysql
}

variant mariadb conflicts mysql56 mysql55 mysql51 percona description {Enable MariaDB} {
    depends_lib-append    port:mariadb
    set my_pkg            mariadb
    configure.args-delete --disable-mysql
}

variant percona conflicts mysql56 mysql55 mysql51 mysql5 mariadb description {Enable Percona} {
    depends_lib-append    port:percona
    set my_pkg            percona
    configure.args-delete --disable-mysql
}

variant postgresql94 conflicts postgresql93 postgresql92 postgresql91 postgresql90 postgresql84 description {Enable PostgreSQL 9.4} {
    depends_lib-append    port:postgresql94
    set pg_version        94
    configure.args-delete --disable-postgresql
}

variant postgresql93 conflicts postgresql94 postgresql92 postgresql91 postgresql90 postgresql84 description {Enable PostgreSQL 9.3} {
    depends_lib-append    port:postgresql93
    set pg_version        93
    configure.args-delete --disable-postgresql
}

variant postgresql92 conflicts postgresql94 postgresql93 postgresql91 postgresql90 postgresql84 description {Enable PostgreSQL 9.2} {
    depends_lib-append    port:postgresql92
    set pg_version        92
    configure.args-delete --disable-postgresql
}

variant postgresql91 conflicts postgresql94 postgresql93 postgresql92 postgresql90 postgresql84 description {Enable PostgreSQL 9.1} {
    depends_lib-append    port:postgresql91
    set pg_version        91
    configure.args-delete --disable-postgresql
}

variant postgresql90 conflicts postgresql94 postgresql93 postgresql92 postgresql91 postgresql84 description {Enable PostgreSQL 9.0} {
    depends_lib-append    port:postgresql90
    set pg_version        90
    configure.args-delete --disable-postgresql
}

variant postgresql84 conflicts postgresql94 postgresql93 postgresql92 postgresql91 postgresql90 description {Enable PostgreSQL 8.4} {
    depends_lib-append    port:postgresql84
    set pg_version        84
    configure.args-delete --disable-postgresql
}

variant fcgi description {Enable FastCGI module} {
    depends_lib-append    port:fcgi
    configure.args-replace s|--disable-fcgi|--with-fcgi=${prefix}|
}

variant mprpc description {Enable MessagePack RPC module} {
    depends_lib-append    port:msgpack
    configure.args-delete --disable-mprpc
}

variant magickpp description {Enable Magick++ of ImageMagick module} {
    depends_lib-append    port:ImageMagick
    configure.args-delete --disable-magickpp
}

post-destroot {
    set libdir   ${destroot}${prefix}/lib
    foreach libname [glob -tails -directory ${libdir} *.dylib] {
        system "install_name_tool -id ${prefix}/lib/${libname} ${libdir}/${libname}"
    }
    set bindir   ${destroot}${prefix}/bin
    set builddir ${worksrcpath}/build/src
    foreach path [concat [glob ${libdir}/*.dylib] ${bindir}/genrpc] {
        set libpathes [regexp -all -inline {\S+\/libpficommon\S+dylib \(} [exec otool -L ${path}]]
        foreach libpath ${libpathes} {
            regexp {\S+\/(libpficommon\S+dylib)} ${libpath} srcpath libname
            system "install_name_tool -change ${srcpath} ${prefix}/lib/${libname} ${path}"
        }
    }
}