# -*- 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
PortGroup           github 1.0
PortGroup           waf 1.0

epoch               1
github.setup        retrieva pficommon 2.0.0
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            2
categories          devel
maintainers         nomaintainer

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

license             BSD

checksums           rmd160  faa0d09e0d0ca27c9675cceca7796ab2319e4027 \
                    sha256  003969a61cf6c06e513538a35fa8a696fb90137fa848fd972dfab4e34ddeda7c \
                    size    449467

patchfiles          patch-wscript.diff

platform darwin {
    if {${os.major} <= 10} {
        known_fail  yes
        pre-fetch {
            ui_error "${name} @${version} does not build on Snow Leopard or earlier."
            error "unsupported platform"
        }
    } elseif {${os.major} >= 13} {
        known_fail  yes
        pre-fetch {
            ui_error "${name} @${version} 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 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 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 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 description {Enable MariaDB} {
    depends_lib-append    port:mariadb
    set my_pkg            mariadb
    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:msgpack0
    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}"
        }
    }
}
