# -*- 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_fPROJ4
version             0.1.0
revision            3
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

description         Based on program PROJ.4, ${package.name} converts geographic longitude and \
                    latitude coordinates into cartesian coordinates.
long_description    This library is based on program PROJ.4 which is a standard Unix filter \
                    function which converts geographic longitude and latitude coordinates \
                    into cartesian coordinates, by means of a wide variety of cartographic \
                    projection functions.

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

dist_subdir         lib_mysqludf
distname            ${package.name}-${version}
worksrcdir          ${distname}-src

checksums           rmd160  062aabb5764d9c0d6bf3803faa7a5e0d0116d90a \
                    sha256  22ec139f1652040828da18a4b3a469355486d459f8000d25381422b9c9e45cdd

depends_lib-append  port:proj4

use_configure       no

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

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_fPROJ4_info;"
    puts $fp "DROP FUNCTION IF EXISTS fPROJ4_transform;"
    puts $fp "DROP FUNCTION IF EXISTS fPROJ4_Geo2UTM;"
    puts $fp "DROP FUNCTION IF EXISTS fPROJ4_UTM2Geo;"
    puts $fp "CREATE FUNCTION lib_mysqludf_fPROJ4_info RETURNS STRING SONAME '${package.name}.so';"
    puts $fp "CREATE FUNCTION fPROJ4_transform RETURNS REAL SONAME '${package.name}.so';"
    puts $fp "CREATE FUNCTION fPROJ4_Geo2UTM RETURNS REAL SONAME '${package.name}.so';"
    puts $fp "CREATE FUNCTION fPROJ4_UTM2Geo 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_fPROJ4_info;"
    puts $fp "DROP FUNCTION IF EXISTS fPROJ4_transform;"
    puts $fp "DROP FUNCTION IF EXISTS fPROJ4_Geo2UTM;"
    puts $fp "DROP FUNCTION IF EXISTS fPROJ4_UTM2Geo;"
    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}
}

livecheck.url       [lindex ${master_sites} 0]
livecheck.type      regex
livecheck.regex     "${package.name}-(\\d+\\.\\d+(\[\\drc.\]+)?)${extract.suffix}"

notes "

    This port installs two mysql scripts to simplify the install/uninstall of the ${package.name}
    UDFs (user-defined-functions).
    
    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
    "