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

name                    mysql5
version                 5.1.72
# Please set revision_client and revision_server to 0 if you bump version.
set revision_client     5
set revision_server     1
set version_branch      [join [lrange [split ${version} .] 0 1] .]
homepage                https://www.mysql.com/
categories              databases
platforms               darwin
maintainers             {ryandesign @ryandesign}
license                 GPL-2
distname                mysql-${version}
dist_subdir             mysql51
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            mysql:MySQL-${version_branch}

checksums           rmd160  a5ba8bedf4027af90a4612a1a7f3a848999cfbf5 \
                    sha256  c414a64722ffa89d315152a030661d34764894e293e33792809cea61c8a5303e

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" eq ${os.platform} && ${os.major} > 8} {
    set mysqluser       _mysql
} else {
    set mysqluser       mysql
}
add_users ${mysqluser} group=${mysqluser} realname=MySQL\ Server

if {${name} eq ${subport}} {
    # http://bugs.mysql.com/bug.php?id=47360
    PortGroup               muniversal 1.0

    revision                ${revision_client}
    depends_lib \
        port:zlib \
        path:lib/libssl.dylib:openssl \
        port:tcp_wrappers

    patch.pre_args-replace  -p0 -p1
    patchfiles          patch-Makefile.in.diff \
                        patch-scripts-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
    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
    
    # doesn't build with clang as of 2011.06.27
    # This is a bug in mysql's build system, fixed in 5.5
    compiler.blacklist-append *clang*
    
    post-build {
        if {[variant_exists universal] && [variant_isset universal]} {
            set dirs {}
            foreach arch ${universal_archs_to_use} {
                lappend dirs ${worksrcpath}-${arch}
            }
        } else {
            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/${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 port."
        }
    }
}

subport mysql5-server {
    
    revision                ${revision_server}
    supported_archs         noarch
    
    depends_run \
        port:mysql5
    
    distfiles
    
    use_configure           no
    
    build {}
    
    destroot {

        xinstall -m 755 -o root -d ${destroot}${prefix}/var/run
        
        xinstall -m 755 -o ${mysqluser} -g ${mysqluser} -d \
            ${destroot}${dbdir} \
            ${destroot}${prefix}/var/log/${mysql} \
            ${destroot}${prefix}/var/run/${mysql}
        destroot.keepdirs-append  \
            ${destroot}${dbdir} \
            ${destroot}${prefix}/var/log/${mysql} \
            ${destroot}${prefix}/var/run/${mysql}
    }
    
    
    startupitem.create      yes
    startupitem.name        ${mysql}
    startupitem.start       "${prefix}/share/${mysql}/mysql/mysql.server start"
    startupitem.stop        "${prefix}/share/${mysql}/mysql/mysql.server stop"
    
    notes "If this is a new install, in order to setup the database you might want to run:" \
        "sudo -u ${mysqluser} mysql_install_db5"
}

livecheck.type          regex
livecheck.url           https://dev.mysql.com/downloads/mysql/${version_branch}.html
livecheck.regex         "<h1>MySQL Community Server (${version_branch}(\.\[0-9.\]+)?)</h1>"