# -*- 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
PortGroup           conflicts_build 1.0

name                mysql57
set name_mysql      ${name}
version             5.7.44
set boost_version   1.59.0
# Set revision_client and revision_server to 0 on version bump.
set revision_client 0
set revision_server 0
set version_branch  [join [lrange [split ${version} .] 0 1] .]
categories          databases
platforms           darwin
maintainers         nomaintainer
homepage            https://www.mysql.com/

if {$subport eq $name} {
    PortGroup           muniversal 1.0
    PortGroup           cmake 1.0
    PortGroup           select 1.0
    PortGroup           legacysupport 1.1
    PortGroup           openssl 1.0

    compiler.cxx_standard   2011
    configure.cxxflags-append -std=c++11

    revision            ${revision_client}
    # https://downloads.mysql.com/docs/licenses/mysqld-5.7-gpl-en.pdf
    license             {GPL-2 OpenSSLException}
    description         Multithreaded SQL database server
    long_description    MySQL is an open-source, multi-threaded SQL database.

    master_sites        macports_distfiles:mysql \
                        sourceforge:project/boost/boost/${boost_version}:boost

    distname            mysql-${version}
    cmake.out_of_source yes
    set boost_distver   [join [split ${boost_version} .] _]
    set boost_distname  boost_${boost_distver}

    # poll-emulator breaks the build, if active:
    # xcom/task.c:744:37: error: 'POLLRDNORM' undeclared (first use in this function)
    # See also: https://trac.macports.org/ticket/38558
    conflicts_build     boost protobuf3-cpp protobuf-cpp poll-emulator

    distfiles           ${distname}${extract.suffix}:mysql \
                        ${boost_distname}${extract.suffix}:boost

    checksums           ${distname}${extract.suffix} \
                        rmd160  fefc4259ace358f46bc325ff1bd072eed043d3b0 \
                        sha256  d03d8ff688862c40da3488451072a4b2178a86b53b4a7546ee5f864bb15cd919 \
                        size    56570597 \
                        ${boost_distname}${extract.suffix} \
                        rmd160  a7974be69a426e8a56b880a412d6c418e4cd751c \
                        sha256  47f11c8844e579d02691a607fbd32540104a9ac7a2534a8ddaef50daf502baac \
                        size    83709983

    depends_lib-append  port:ncurses \
                        port:libedit \
                        port:libevent \
                        port:lz4 \
                        port:cyrus-sasl2 \
                        port:zlib
    depends_run-append  port:mysql_select

    post-extract {
        file mkdir ${cmake.build_dir}/macports
        copy ${filespath}/macports-default.cnf \
            ${filespath}/my.cnf \
            ${cmake.build_dir}/macports/
    }

    patch.pre_args-replace  -p0 -p1
    patchfiles      patch-cmake-install_layout.cmake.diff \
                    patch-configure.cmake.diff \
                    patch-innodb_engine-fcommon.diff \
                    patch-innodb_memcached-daemon_memcached-include-memcached-util.h.diff \
                    patch-lockpool.diff \
                    patch-cmake-fix-test-env.diff

    platform darwin powerpc {
        # These can be applied unconditionally, but unneeded outside of ppc.
        patchfiles-append \
                    patch-powerpc.diff \
                    patch-NOTE_TRIGGER.diff
    }

    post-patch {
        # https://trac.macports.org/ticket/67324
        ui_info "$UI_PREFIX Applying patch-boost-clang16-cpp17-compat.diff"
        system -W ${workpath}/${boost_distname} \
            "/usr/bin/patch -p0 < ${filespath}/patch-boost-clang16-cpp17-compat.diff"

        reinplace "s|@NAME@|${name_mysql}|g" \
            ${worksrcpath}/cmake/install_layout.cmake
        reinplace "s|@WORKSRCPATH@|${worksrcpath}|g" \
            ${worksrcpath}/cmake/install_layout.cmake
        reinplace "s|@NAME@|${name_mysql}|g" \
            ${cmake.build_dir}/macports/macports-default.cnf \
            ${cmake.build_dir}/macports/my.cnf
        reinplace "s|@PREFIX@|${prefix}|g" \
            ${cmake.build_dir}/macports/macports-default.cnf \
            ${cmake.build_dir}/macports/my.cnf

        # don't force /usr/bin/libtool -- allow cctools' version to be used
        reinplace "s|/usr/bin/libtool|libtool|g" \
            ${worksrcpath}/cmake/merge_archives.cmake.in
    }

    patchfiles-append \
                    patch-rename_version.diff
    post-patch {
        # avoid collision with C++17 header <version>
        move ${worksrcpath}/VERSION ${worksrcpath}/VERSION.txt
    }

    configure.args-delete \
        -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib
    configure.args-append \
        -DCMAKE_INSTALL_NAME_DIR:STRING=${prefix}/lib/${name_mysql}/mysql \
        -DINSTALL_LAYOUT:STRING=MACPORTS \
        -DSYSCONFDIR:PATH=${prefix}/etc/${name_mysql} \
        -DMYSQL_UNIX_ADDR:PATH=${prefix}/var/run/${name_mysql}/mysqld.sock \
        -DMYSQL_DATADIR:PATH=${prefix}/var/db/${name_mysql} \
        -DDEFAULT_CHARSET:STRING=utf8 \
        -DDEFAULT_COLLATION:STRING=utf8_general_ci \
        -DWITH_EMBEDDED_SERVER:BOOL=ON \
        -DWITH_EDITLINE=system \
        -DWITH_ZLIB:STRING=system \
        -DWITH_UNIT_TESTS:BOOL=OFF \
        -DINSTALL_MYSQLTESTDIR=0 \
        -DENABLE_DOWNLOADS:BOOL=OFF \
        -DENABLE_GCOV:BOOL=OFF \
        -DENABLE_DTRACE:BOOL=OFF \
        -DWITH_LIBWRAP:BOOL=OFF \
        -DWITH_INNODB_MEMCACHED=1 \
        -DWITH_LIBEVENT=system \
        -DLIBEVENT_INCLUDE_PATH:PATH="${prefix}/include" \
        -DLIBEVENT_LIB_PATHS:PATH="${prefix}/lib" \
        -DWITH_LZ4=system \
        -DWITH_PARTITION_STORAGE_ENGINE=1 \
        -DDOWNLOAD_BOOST=1 \
        -DWITH_BOOST=${worksrcpath}/../${boost_distname} \
        -DDOWNLOAD_BOOST_TIMEOUT=2400
    configure.cppflags-delete \
        -I${prefix}/include

    if {[string match *gcc* ${configure.compiler}]} {
        configure.args-append \
            -DFORCE_UNSUPPORTED_COMPILER=ON
        configure.ldflags-append \
            -latomic
    }

    select.group        mysql
    select.file         ${filespath}/${name_mysql}

    post-destroot {
        # proc portdestroot::destroot_finish fails to find and compress our man pages
        # so borrow the compress command and run on our files now.
        set manpath "${destroot}${prefix}/share/man"
        set gzip [findBinary gzip ${portutil::autoconf::gzip_path}]
        foreach manpage [glob -type f ${destroot}${prefix}/share/man/${name_mysql}/man\[1-9\]/*] {
            # Fix paths in manpages
            reinplace -q "s|/etc/|${prefix}/etc/${name_mysql}/|g" ${manpage}
            # Compress all manpages with gzip
            system "$gzip -9vf ${manpage}"
        }

#         foreach samp_conffile [glob -type f ${destroot}${prefix}/share/${name_mysql}/support-files/my-*.cnf] {
#             # Fix paths in sample configuration files
#             reinplace -q "s|/etc/my.cnf|${prefix}/etc/${name_mysql}/my.cnf|g" ${samp_conffile}
#         }
        xinstall -m 755 -o root -d \
            ${destroot}${prefix}/etc/${name_mysql}
        copy ${cmake.build_dir}/macports/macports-default.cnf \
            ${destroot}${prefix}/etc/${name_mysql}/
        xinstall -m 755 -o root -d \
            ${destroot}${prefix}/share/${name_mysql}/support-files/macports
        copy ${cmake.build_dir}/macports/my.cnf \
            ${destroot}${prefix}/share/${name_mysql}/support-files/macports/
    }

    post-install {
        if {![file exists ${prefix}/etc/LaunchDaemons/org.macports.${name_mysql}/org.macports.${name_mysql}-server.plist]} {
            ui_msg "The ${name_mysql} client has been installed."
            ui_msg "To install the ${name_mysql} server, install the ${name_mysql}-server port."
        }
    }

    post-activate {
        if {![file exists ${prefix}/etc/${name_mysql}/my.cnf]} {
            copy ${prefix}/share/${name_mysql}/support-files/macports/my.cnf \
                ${prefix}/etc/${name_mysql}/
        }
    }

    notes "
On activation if no ${prefix}/etc/${name_mysql}/my.cnf file exists one
will be created which loads
${prefix}/etc/${name_mysql}/macports-default.cnf.

If a ${prefix}/etc/${name_mysql}/my.cnf file exists MacPorts does not
touch it and any changes you make to ${prefix}/etc/${name_mysql}/my.cnf
will be preserved (e.g., during port upgrades, deactivations or
activations). ${prefix}/etc/${name_mysql}/my.cnf is a good place to
customize your ${name_mysql} installation.

Any changes made to ${prefix}/etc/${name_mysql}/macports-default.cnf
will be lost during port upgrades, deactivations or activations so you
are advised to not make changes here. Currently
${prefix}/etc/${name_mysql}/macports-default.cnf contains only one
directive; to disable networking. With disabled networking it is
possible to install and have running all the MacPorts mysql ports
simultaneously.
"
}

subport ${name_mysql}-server {
    revision            ${revision_server}
    license             BSD
    description         Run ${name_mysql} as server
    long_description    {*}${description}

    supported_archs     noarch
    distfiles

    depends_run         port:${name_mysql}

    if {"darwin" eq ${os.platform} && ${os.major} > 8} {
        set mysqluser       _mysql
    } else {
        set mysqluser       mysql
    }
    add_users ${mysqluser} group=${mysqluser} realname=MySQL\ Server

    pre-extract {
        copy ${filespath}/org.macports.mysql-server.plist ${workpath}/org.macports.${subport}.plist
    }

    post-patch {
        reinplace "s|@NAME@|${subport}|g" \
            ${workpath}/org.macports.${subport}.plist
        reinplace "s|@NAMEMYSQL@|${name_mysql}|g" \
            ${workpath}/org.macports.${subport}.plist
        reinplace "s|@PREFIX@|${prefix}|g" \
            ${workpath}/org.macports.${subport}.plist
        reinplace "s|@SUBPORT@|${subport}|g" \
            ${workpath}/org.macports.${subport}.plist
        reinplace "s|@USER@|${mysqluser}|g" \
            ${workpath}/org.macports.${subport}.plist
        reinplace "s|@GROUP@|${mysqluser}|g" \
            ${workpath}/org.macports.${subport}.plist
    }

    use_configure       no

    build {}

    destroot {
        xinstall -d -m 755 ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${subport}
        xinstall -m 0644 -o root -W ${workpath} \
            org.macports.${subport}.plist \
            ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${subport}
        xinstall -d -m 755 ${destroot}/Library/LaunchDaemons
        ln -s ${prefix}/etc/LaunchDaemons/org.macports.${subport}/org.macports.${subport}.plist \
            ${destroot}/Library/LaunchDaemons/org.macports.${subport}.plist
        xinstall -m 755 -o root -d \
            ${destroot}${prefix}/var/run
        xinstall -m 755 -o ${mysqluser} -g ${mysqluser} -d \
            ${destroot}${prefix}/etc/${name_mysql} \
            ${destroot}${prefix}/var/db/${name_mysql} \
            ${destroot}${prefix}/var/log/${name_mysql} \
            ${destroot}${prefix}/var/run/${name_mysql}
        xinstall -m 700 -o ${mysqluser} -g ${mysqluser} -d \
            ${destroot}${prefix}/var/db/${name_mysql}-files \
            ${destroot}${prefix}/var/db/${name_mysql}-keyring
        destroot.keepdirs-append  \
            ${destroot}${prefix}/var/db/${name_mysql} \
            ${destroot}${prefix}/var/db/${name_mysql}-files \
            ${destroot}${prefix}/var/db/${name_mysql}-keyring \
            ${destroot}${prefix}/var/log/${name_mysql} \
            ${destroot}${prefix}/var/run/${name_mysql}
    }

    notes "
If this is a new install you might want to run:

\$ sudo ${prefix}/lib/${name_mysql}/bin/mysqld --initialize --user=${mysqluser}
\$ ${prefix}/lib/${name_mysql}/bin/mysql_secure_installation
"
}

# No longer available from upstream
livecheck.type          none
