# $Id: Portfile 57751 2009-09-16 05:04:20Z ryandesign@macports.org $

PortSystem              1.0

name                    mysql4
conflicts               mysql5 mysql5-devel
version                 4.1.22
homepage                http://www.mysql.com/
categories              databases
platforms               darwin
maintainers             ryandesign openmaintainer
license                 GPLv2
distname                mysql-${version}
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.

set sitedir             Downloads/MySQL-4.1/
master_sites \
    http://www.mirrorservice.org/sites/ftp.mysql.com/${sitedir} \
    http://www.softagency.co.jp/MySQL/${sitedir} \
    http://mirrors.tilian.co.uk/mysql.com/${sitedir} \
    ftp://planetmirror.com/pub/mysql/${sitedir} \
    ftp://sunsite.dk/mirrors/mysql/${sitedir} \
    http://mysql.mediatraffic.fi/${sitedir} \
    ftp://filepile.tiscali.de/mirror/mysql/${sitedir} \
    ftp://ftp.rtfm.no/pub/mysql/${sitedir} \
    http://www.mysql.cz/${sitedir} \
    ftp://ftp.u-paris10.fr/mysql.com/${sitedir} \
    http://mysql.oms-net.nl/${sitedir} \
    ftp://ftp.free.fr/pub/MySQL\${sitedir}

checksums \
    md5 37b4479951fa0cf052269d27c41ca200

depends_lib \
    port:openssl

set dbdir               ${prefix}/var/db/mysql

post-patch {
    reinplace "s%/etc/my.cnf%${prefix}/etc/${name}/my.cnf%g" \
        ${worksrcpath}/scripts/mysqlaccess.sh \
        ${worksrcpath}/scripts/mysqld_multi.sh \
        ${worksrcpath}/scripts/mysqldumpslow.sh \
        ${worksrcpath}/scripts/mysqlhotcopy.sh \
        ${worksrcpath}/sql-bench/bench-init.pl.sh \
        ${worksrcpath}/support-files/my-huge.cnf.sh \
        ${worksrcpath}/support-files/my-innodb-heavy-4G.cnf.sh \
        ${worksrcpath}/support-files/my-large.cnf.sh \
        ${worksrcpath}/support-files/my-medium.cnf.sh \
        ${worksrcpath}/support-files/my-small.cnf.sh \
        ${worksrcpath}/support-files/mysql.server.sh
    reinplace "s%/etc/%${prefix}/etc/${name}/%g" \
        ${worksrcpath}/mysys/default.c
}

configure.args \
    --mandir=${prefix}/share/man \
    --infodir=${prefix}/share/info \
    --localstatedir=${dbdir} \
    --sysconfdir=${prefix}/etc/${name} \
    --with-openssl=${prefix} \
    --with-extra-charsets=complex \
    --with-innodb \
    --with-unix-socket-path=${prefix}/var/run/mysqld/mysqld.sock \
    --with-mysqld-user=mysql \
    --without-debug \
    --without-bench \
    --enable-thread-safe-client

variant server {
    # Create a startupitem to start/stop the server
    startupitem.create  yes
    startupitem.start   "${prefix}/share/mysql/mysql.server start"
    startupitem.stop    "${prefix}/share/mysql/mysql.server stop"
}

pre-destroot {
    xinstall -m 755 -d ${destroot}${prefix}/var/log/mysql
    destroot.keepdirs ${destroot}${prefix}/var/log/mysql
    
    if { [variant_isset server] } {
        addgroup mysql
        set gid [existsgroup mysql]
        adduser mysql gid=${gid} realname=MySQL\ Server
        
        system "chown mysql:mysql ${destroot}${prefix}/var/log/mysql"
    }
}

post-destroot {
    system "rm -rf ${destroot}${prefix}/mysql-test"
    
    xinstall -m 775 -d ${destroot}${dbdir}
    xinstall -m 755 -d ${destroot}${prefix}/var/run/mysqld
    destroot.keepdirs-append ${destroot}${dbdir}
    destroot.keepdirs-append ${destroot}${prefix}/var/run/mysqld
    if { [variant_isset server] } {
        xinstall -o root -m 755 -d ${destroot}${prefix}/etc/${name}
        xinstall -o root -m 644 -c ${filespath}/my.cnf ${destroot}${prefix}/etc/${name}/my.cnf.sample
        reinplace "s|__PREFIX|${prefix}|g" ${destroot}${prefix}/etc/${name}/my.cnf.sample
        xinstall -o mysql -g mysql -m 775 -d ${destroot}${dbdir}
        destroot.keepdirs-append ${destroot}${dbdir}
        system "chown -R mysql:mysql ${destroot}${dbdir}"
        
        xinstall -o mysql -g mysql -m 755 -d ${destroot}${prefix}/var/run/mysqld
        destroot.keepdirs-append ${destroot}${prefix}/var/run/mysqld
        system "chown -R mysql:mysql ${destroot}${prefix}/var/run/mysqld"
    }
}

post-install {
    ui_msg "******************************************************"
    ui_msg "* You might want to run                              *"
    ui_msg "* sudo -u mysql mysql_install_db                     *"
    ui_msg "* if this is a new install                           *"
    ui_msg "*                                                    *"
    ui_msg "* If you did not choose the server variant you may   *"
    ui_msg "* add a mysql user and you will need to change the   *"
    ui_msg "* ownership to that user for the following:          *"
    ui_msg "*    ${dbdir}                                        *"
    ui_msg "*    ${prefix}/var/run/mysqld                        *"
    ui_msg "*    ${prefix}/var/log/mysql                         *"
    ui_msg "******************************************************"
}

livecheck.type          none
