# -*- 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
# $Id: Portfile 85131 2011-10-10 01:50:25Z ryandesign@macports.org $

PortSystem              1.0

name                    mariadb
conflicts               mysql5-devel mysql5 mysql4
version                 5.2.9
categories              databases
platforms               darwin
license                 GPL-2
maintainers             jogwtr@googlemail.com

description \
    A database server that offers drop-in replacement functionality for MySQL
long_description \
    MariaDB is a database server that offers drop-in replacement functionality for \
    MySQL1. MariaDB is built by some of the original authors of MySQL, with \
    assistance from the broader community of Free and open source software \
    developers. In addition to the core functionality of MySQL, MariaDB offers a \
    rich set of feature enhancements including alternate storage engines, server \
    optimizations, and patches.

homepage                http://mariadb.org/
master_sites \
    http://mirrors.supportex.net/mariadb/${distname}/kvm-tarbake-jaunty-x86/ \
    http://www.percona.com/downloads/MariaDB/${distname}/kvm-tarbake-jaunty-x86/ \
    http://ftp.osuosl.org/pub/mariadb/${distname}/kvm-tarbake-jaunty-x86/ \
    http://mirrors.supportex.net/mariadb/${distname}/kvm-tarbake-jaunty-x86/ \
    http://technocation.org/mirror/mariadb/${distname}/kvm-tarbake-jaunty-x86/ \
    http://mirror.aarnet.edu.au/pub/MariaDB/${distname}/kvm-tarbake-jaunty-x86/ \
    http://ftp-stud.hs-esslingen.de/pub/Mirrors/mariadb/${distname}/kvm-tarbake-jaunty-x86/ \
    http://gd.tuwien.ac.at/db/mariadb/${distname}/kvm-tarbake-jaunty-x86/ \
    http://mirrors.fe.up.pt/pub/mariadb/${distname}/kvm-tarbake-jaunty-x86/
distname                mariadb-${version}

checksums               md5     eb79ea79d3199c37f15d88fbd97e2216 \
                        sha1    da87cdc4d3f76cbbe059684175461e18972f323b \
                        rmd160  23f89e13ec9b9f731f42ba65f40ea07ad21a4471

depends_lib             port:zlib \
                        port:openssl

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

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

patchfiles                patch-Makefile.in.diff

configure.args \
    --mandir=${prefix}/share/man \
    --infodir=${prefix}/share/info \
    --localstatedir=${dbdir} \
    --libdir=${libdir} \
    --bindir=${bindir} \
    --includedir=${prefix}/include/${mariadb} \
    --datadir=${prefix}/share/${mariadb} \
    --sysconfdir=${sysconfdir} \
    --with-zlib-dir=bundled \
    --with-ssl=${prefix} \
    --with-extra-charsets=complex \
    --with-unix-socket-path=${prefix}/var/run/${mariadb}/mysqld.sock \
    --with-mysqld-user=${mariadbuser} \
    --without-docs \
    --with-plugins=max \
    --enable-thread-safe-client \
    --enable-assembler \
    --enable-thread-safe-client \
    --with-big-tables \
    --with-plugin-aria \
    --with-aria-tmp-tables \
    --without-plugin-innodb_plugin \
    --with-libevent \
    --enable-local-infile

# 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
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 {
    set dirs ${worksrcpath}
    foreach dir ${dirs} {
        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/${mariadb}/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.${mariadb}/org.macports.${mariadb}.plist]} {
        ui_msg "The MariaDB client has been installed."
        ui_msg "If you also want a MariaDB server, install the mariadb-server port."
    }
}
