# $Id: Portfile 64248 2010-02-27 07:29:06Z ryandesign@macports.org $

PortSystem              1.0
PortGroup               archcheck 1.0

# http://bugs.mysql.com/bug.php?id=47360
PortGroup               muniversal 1.0

name                    mysql5-devel
conflicts               mysql5 mysql4
version                 5.5.2-m2
set branch              [join [lrange [split ${version} .] 0 1] .]
homepage                http://www.mysql.com/
categories              databases
platforms               darwin
maintainers             jwa ryandesign
license                 GPLv2
distname                mysql-${version}
dist_subdir             mysql5
use_parallel_build      yes

description \
    Multithreaded SQL database server

long_description \
    MySQL is an open-source, multi-threaded SQL database \
    with a command syntax very similar to mSQL.

master_sites \
    http://mysql.mirrors.pair.com/Downloads/MySQL-${branch}/ \
    http://mysql.he.net/Downloads/MySQL-${branch}/ \
    http://mirrors.sunsite.dk/mysql/Downloads/MySQL-${branch}/ \
    http://sunsite.informatik.rwth-aachen.de/mysql/Downloads/MySQL-${branch}/ \
    http://ftp.plusline.de/mysql/Downloads/MySQL-${branch}/

checksums               md5     cd3254f29561953ffb7c023cb1b825d2 \
                        sha1    801b6c9d3cdad2e5e273d3b4c8fe7e68cd1bab3c \
                        rmd160  0549f599de5ba9efc26ab45dfcddca135a70d631

depends_lib \
    port:zlib \
    port:openssl

archcheck.files         lib/libz.dylib \
                        lib/libssl.dylib

set major_version       [strsed ${version} {s/\..*$//}]
set mysql               mysql${major_version}
set libdir              ${prefix}/lib/${mysql}
set bindir              ${libdir}/bin
set dbdir               ${prefix}/var/db/${mysql}
set sysconfdir          ${prefix}/etc/${mysql}

if {"darwin" == ${os.platform} && ${os.major} > 8} {
    set mysqluser       _mysql
} else {
    set mysqluser       mysql
}

patchfiles \
    patch-Makefile.in.diff \
    patch-mysql_secure_installation.sh.diff

post-patch {
    reinplace s%@BINDIR@%${bindir}%g ${worksrcpath}/scripts/mysql_secure_installation.sh
}

configure.args \
    --mandir=${prefix}/share/man \
    --infodir=${prefix}/share/info \
    --localstatedir=${dbdir} \
    --libdir=${libdir} \
    --bindir=${bindir} \
    --includedir=${prefix}/include/${mysql} \
    --datadir=${prefix}/share/${mysql} \
    --sysconfdir=${sysconfdir} \
    --with-zlib-dir=${prefix} \
    --with-ssl=${prefix} \
    --with-extra-charsets=complex \
    --with-unix-socket-path=${prefix}/var/run/${mysql}/mysqld.sock \
    --with-mysqld-user=${mysqluser} \
    --without-docs \
    --with-plugins=all \
    --enable-thread-safe-client
# the following is in store for future
#    --program-suffix=5

# Add readline support.
# "--without-readline" has the peculiar meaning "do not use the bundled copy
# of readline but use the system's (i.e. MacPorts') copy of readline"
depends_lib-append      port:readline
archcheck.files-append  lib/libreadline.dylib
configure.args-append   --without-readline

# Build libmysqld embedded server.
configure.cflags-append -fPIC
configure.cxxflags-append -fPIC
configure.args-append --with-embedded-server --with-pic

post-build {
    if {[variant_isset universal]} {
        set dirs {}
        foreach arch ${universal_archs_to_use} {
            lappend dirs ${worksrcpath}-${arch}
        }
    } else {
        set dirs ${worksrcpath}
    }
    foreach dir ${dirs} {
        reinplace "s|${dir}/libservices|${libdir}/mysql|g" \
            ${dir}/plugin/daemon_example/.libs/libdaemon_example.lai \
            ${dir}/plugin/semisync/.libs/semisync_master.lai \
            ${dir}/plugin/semisync/.libs/semisync_slave.lai \
            ${dir}/storage/example/.libs/ha_example.lai
        reinplace -E {s|-arch [a-z0-9_]+||g} \
            ${dir}/scripts/mysql_config \
            ${dir}/scripts/mysqlbug
    }
}

pre-destroot {
    xinstall -m 755 -d ${destroot}${sysconfdir}
    destroot.keepdirs-append ${destroot}${sysconfdir}
}

post-destroot {
    # Fix paths in manpages and sample configuration files
    foreach manpage [glob -type f ${destroot}${prefix}/share/man/man\[1-9\]/*] {
        reinplace "s|/etc/my.cnf|${sysconfdir}/my.cnf|g" ${manpage}
    }
    foreach samp_conffile [glob -type f ${destroot}${prefix}/share/${mysql}/mysql/my-*.cnf] {
        reinplace "s|/etc/my.cnf|${sysconfdir}/my.cnf|g" ${samp_conffile}
    }
    
    # Symlink mysql binaries into bin directory, with ${major_version} appended to the name
    foreach f [glob -tails -directory ${destroot}${bindir} my*] {
        ln -sf ${bindir}/${f} ${destroot}${prefix}/bin/${f}${major_version}
    }
}

post-install {
    if {![file exists ${prefix}/etc/LaunchDaemons/org.macports.${mysql}/org.macports.${mysql}.plist]} {
        ui_msg "The MySQL client has been installed."
        ui_msg "If you also want a MySQL server, install the mysql5-server-devel port."
    }
}

livecheck.type          regex
livecheck.version       [lindex [split ${version} -] 0]
livecheck.url           http://dev.mysql.com/
livecheck.regex         "<a href=\"http://dev.mysql.com/downloads/mysql/\[^\"\]+\">(${branch}(\.\[0-9.\]+)?)\[^<\]*</a>"
