# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem          1.0

set package.name    lib_mysqludf_json
version             0.0.2
set mysql.name      mysql55
name                ${mysql.name}-${package.name}

set mysql.ports     {mysql5 mysql51 mysql55 mariadb percona}
foreach mysql.port ${mysql.ports} {

    lappend mysql.extension.ports "${mysql.port}-${package.name}"
}
foreach mysql.extension.port ${mysql.extension.ports} {

    set idx [lsearch ${mysql.extension.ports} ${mysql.extension.port}]
    set mysql.port [lindex ${mysql.ports} $idx]
    if {${mysql.extension.port} == ${name} == ${subport}} {

        depends_lib-append  port:${mysql.port}
    } else {

        subport ${mysql.port}-${package.name} {
    
            set mysql.name      ${mysql.port}
            depends_lib-append  port:${mysql.port}
        }
    }
}

categories          databases lang
maintainers         nomaintainer
platforms           darwin
license             LGPL

description         Map database data to the JSON.
long_description    ${description}

homepage            http://www.mysqludf.org/${package.name}/
master_sites        ${homepage}

dist_subdir         lib_mysqludf
distname            ${package.name}_${version}

checksums           rmd160  4c98739ed2753580195e11e602777188bc9a20ff \
                    sha256  bed11c73d1e5d23669051b8a430f5c17b961e462ee82612b7dcdbceba47052b5

extract.mkdir       yes

use_configure       no

configure.cflags-append \
                    -I${prefix}/include/${mysql.name} \
                    -I${prefix}/include/${mysql.name}/mysql \
                    -bundle \
                    -undefined dynamic_lookup

post-extract {
    move ${worksrcpath}/${package.name}.so \
            ${worksrcpath}/${package.name}.so.orig
}

build {
    set cmd "${configure.cpp} ${configure.ldflags} ${configure.cflags} ${configure.cppflags}"
    if [variant_isset universal] {

        set  "${cmd} ${configure.universal_cflags}"
    }
    system "cd ${worksrcpath} && ${cmd} ${package.name}.c -o ${package.name}.so"

    xinstall -d ${worksrcpath}/macports
    set fp [open ${worksrcpath}/macports/installdb.sql w]
    puts $fp "USE mysql;"
    puts $fp "DROP FUNCTION IF EXISTS lib_mysqludf_json_info;"
    puts $fp "DROP FUNCTION IF EXISTS json_array;"
    puts $fp "DROP FUNCTION IF EXISTS json_members;"
    puts $fp "DROP FUNCTION IF EXISTS json_object;"
    puts $fp "DROP FUNCTION IF EXISTS json_values;"
    puts $fp "CREATE FUNCTION lib_mysqludf_json_info RETURNS STRING SONAME '${package.name}.so';"
    puts $fp "CREATE FUNCTION json_array RETURNS REAL SONAME '${package.name}.so';"
    puts $fp "CREATE FUNCTION json_members RETURNS REAL SONAME '${package.name}.so';"
    puts $fp "CREATE FUNCTION json_object RETURNS REAL SONAME '${package.name}.so';"
    puts $fp "CREATE FUNCTION json_values RETURNS REAL SONAME '${package.name}.so';"
    close $fp

    set fp [open ${worksrcpath}/macports/uninstalldb.sql w]
    puts $fp "USE mysql;"
    puts $fp "DROP FUNCTION IF EXISTS lib_mysqludf_json_info;"
    puts $fp "DROP FUNCTION IF EXISTS json_array;"
    puts $fp "DROP FUNCTION IF EXISTS json_members;"
    puts $fp "DROP FUNCTION IF EXISTS json_object;"
    puts $fp "DROP FUNCTION IF EXISTS json_values;"
    close $fp
}

destroot {
    xinstall -m 755 -d ${destroot}${prefix}/lib/${mysql.name}/plugin
    xinstall -m 644 -W ${worksrcpath} \
        ${package.name}.so \
        ${destroot}${prefix}/lib/${mysql.name}/plugin
    xinstall -m 755 -d ${destroot}${prefix}/share/${mysql.name}/${package.name}
    xinstall -m 644 -W ${worksrcpath}/macports installdb.sql uninstalldb.sql \
        ${destroot}${prefix}/share/${mysql.name}/${package.name}
    xinstall -m 755 -d ${destroot}${prefix}/share/${mysql.name}/${package.name}/doc/html
    xinstall -m 644 ${worksrcpath}/${package.name}.html \
        ${destroot}${prefix}/share/${mysql.name}/${package.name}/doc/html/index.html
}

livecheck.url       [lindex ${master_sites} 0]
livecheck.type      regex
livecheck.regex     "href=.${package.name}_((?!\\${extract.suffix}).*)\\${extract.suffix}."

notes "

    This port installs two mysql scripts to simplify the install/uninstall of the ${package.name}
    UDFs (user-defined-functions) along with html documentation describing the functions syntax.
    
    Install:
    \$ ${prefix}/lib/${mysql.name}/bin/mysql -uroot -p < ${prefix}/share/${mysql.name}/${package.name}/installdb.sql

    Uninstall:
    \$ ${prefix}/lib/${mysql.name}/bin/mysql -uroot -p < ${prefix}/share/${mysql.name}/${package.name}/uninstalldb.sql
    
    HTML Documentation:
    $ open ${prefix}/share/${mysql.name}/${package.name}/doc/html/index.html
    "