# -*- 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           cmake 1.0

name                mysql55-connector-cpp
set name_package    mysql-connector-c++
version             1.1.9
revision            0
set branch          [join [lrange [split ${version} .] 0 1] .]
categories          databases
platforms           darwin
maintainers         nomaintainer
license             GPL-2

set mirror_dir      Downloads/Connector-C++
homepage            https://www.mysql.com/products/connector/
master_sites        http://mysql.mirrors.pair.com/${mirror_dir} \
                    http://mysql.he.net/${mirror_dir} \
                    http://mirrors.sunsite.dk/mysql/${mirror_dir} \
                    http://sunsite.informatik.rwth-aachen.de/mysql/${mirror_dir} \
                    http://ftp.plusline.de/mysql/${mirror_dir}

distname            ${name_package}-${version}
checksums           rmd160  2445b7c3aab96e459bf8726963fbc7e203af7e6e \
                    sha256  3e31847a69a4e5c113b7c483731317ec4533858e3195d3a85026a0e2f509d2e4

depends_build-append \
                    port:boost

patch.pre_args      -p1
patchfiles          patch-CMakeLists.txt.diff \
                    patch-cppconn-CMakeLists.txt.diff \
                    patch-driver-CMakeLists.txt.diff \
                    patch-examples-examples.h.diff \
                    patch-examples-pthreads.cpp.diff \
                    patch-examples-standalone_example.cpp.diff \
                    patch-examples-standalone_example_docs1.cpp.diff \
                    patch-examples-standalone_example_docs2.cpp.diff

post-extract {
    file copy -force ${filespath}/examples-CMakeLists.txt \
                     ${worksrcpath}/examples/CMakeLists.txt
}

post-patch {
    reinplace "s,@NAME@,/${subport},g" \
        ${worksrcpath}/CMakeLists.txt \
        ${worksrcpath}/cppconn/CMakeLists.txt \
        ${worksrcpath}/driver/CMakeLists.txt \
        ${worksrcpath}/examples/CMakeLists.txt

    reinplace "s,@DBPORT@,/${mp.port},g" \
        ${worksrcpath}/examples/CMakeLists.txt \
        ${worksrcpath}/examples/examples.h

    reinplace "s,@PREFIX@,${prefix},g" \
        ${worksrcpath}/examples/CMakeLists.txt
}

configure.args-append \
                    -DCMAKE_INSTALL_NAME_DIR:STRING=${prefix}/lib/${subport}

post-destroot {
    xinstall -d ${destroot}${prefix}/share/${subport}
    copy ${worksrcpath}/examples \
        ${destroot}${prefix}/share/${subport}/

    foreach lib [glob -directory ${destroot}${prefix}/lib/${subport} -type f *.dylib] {
        system "install_name_tool -id ${prefix}/lib/${subport}/[strsed ${lib} /^.*\\///] ${lib}"
    }
}

set mp.ports {
    mysql51
    mysql56
    mysql57
    mariadb
    percona
}
set mp.names {}
foreach mp.port ${mp.ports} {
    lappend mp.names "${mp.port}-connector-cpp"
}
foreach mp.name ${mp.names} {
    set idx             [lsearch ${mp.names} ${mp.name}]
    set mp.port     [lindex ${mp.ports} $idx]
    set mp.conflicts  [lreplace ${mp.names} $idx $idx]

    if {[string compare ${mp.name} ${name}] == 0 && [string compare ${mp.name} ${subport}] == 0} {
        description         The MySQL Connector/C++ for ${mp.port}.
        long_description    ${description} \
                            Standardized MySQL database driver for C++ development.
        depends_lib-append  port:${mp.port}
        configure.args-append \
                            -DMYSQLCPPCONN_DYNLOAD_MYSQL_LIB:FILEPATH=${prefix}/lib/${mp.port}/mysql/libmysqlclient.dylib \
                            -DMYSQL_CONFIG_EXECUTABLE:FILEPATH=${prefix}/lib/${mp.port}/bin/mysql_config
    } else {
        subport ${mp.name} {
            description         The MySQL Connector/C++ for ${mp.port}.
            long_description    ${description} \
                                Standardized MySQL database driver for C++ development.
            depends_lib-append  port:${mp.port}
            configure.args-append \
                                -DMYSQLCPPCONN_DYNLOAD_MYSQL_LIB:FILEPATH=${prefix}/lib/${mp.port}/mysql/libmysqlclient.dylib \
                                -DMYSQL_CONFIG_EXECUTABLE:FILEPATH=${prefix}/lib/${mp.port}/bin/mysql_config
        }
    }
}

livecheck.type      regex
livecheck.url       [lindex ${master_sites} 0]
livecheck.regex     "\"mysql-connector-c\\+\\+-(\\d\[\\d\\.\]*)\\${extract.suffix}\""