# -*- 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           legacysupport 1.1
PortGroup           muniversal 1.0
PortGroup           xcode_workaround 1.0
PortGroup           makefile 1.0

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                redis
version             8.6.3
revision            0
conflicts           redis7
checksums           rmd160  b3d043f820baeada428931b74da524eb3836ec9a \
                    sha256  9f54d4458c52be5472cdd1347d737f1d488b520fc3d0911cba47302de8d836e2 \
                    size    4252312

categories          databases
license             AGPL-3
maintainers         nomaintainer

description         Redis is an open source, advanced key-value store.
long_description    {*}${description}

set redis_domain    redis.io
homepage            https://${redis_domain}
master_sites        https://download.${redis_domain}/releases/

# https://trac.macports.org/ticket/59245
xcode_workaround.fixed_xcode_version 11.2

patchfiles          patch-redis.conf.diff \
                    patch-hiredis.diff \
                    patch-gh-12585.diff

# https://github.com/redis/redis/pull/13534
patchfiles-append   0001-config.h-fix-Availability-macros-so-they-actually-wo.patch \
                    0002-debug.c-do-not-use-arm-code-on-ppc.patch \
                    0003-debug.c-fix-PowerPC-case-in-uc_mcontext.patch

# ld: warning: directory '/usr/local/opt/openssl/lib' following -L not found
patchfiles-append   0004-Remove-brewisms.patch

# The build now uses ld directly where the compiler is supposed to be used.
# ld: unknown option: -Wl,-headerpad_max_install_names
patchfiles-append   patch-tests-Makefile.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" \
        ${worksrcpath}/redis.conf \
        ${worksrcpath}/deps/hiredis/Makefile
}

# redis and dep makefiles have their own optflags
configure.optflags

# avoid build conflict with lua
configure.cppflags-replace \
                    -I${prefix}/include \
                    -isystem${prefix}/include

# redis doesn't know about CPPFLAGS so pass it this way
build.args-append   REDIS_CFLAGS="${configure.cppflags}"

# disable silent rules
build.args-append   V=1

# use jemalloc
build.args-append   MALLOC=jemalloc

# enable TLS.
build.args-append   BUILD_TLS=yes
depends_lib         path:lib/libssl.dylib:openssl

# see ${worksrcpath}/deps/jemalloc/Makefile
compiler.cxx_standard       2014
configure.cxxflags-append   -std=c++14

# https://github.com/redis/redis/issues/13535
if {[string match *gcc* ${configure.compiler}]} {
    configure.ldflags-append -latomic
}

destroot.keepdirs   ${destroot}${prefix}/var/db/redis
installs_libs       no
makefile.has_destdir    no

post-destroot {
    foreach conffile {redis.conf sentinel.conf} {
        if {[file exists ${worksrcpath}/${conffile}]} {
            xinstall -m 0644 ${worksrcpath}/${conffile} \
                ${destroot}${prefix}/etc/${conffile}.sample
        }
    }
}

post-activate {
    foreach conffile {redis.conf sentinel.conf} {
        if {![file exists ${prefix}/etc/${conffile}] && [file exists ${prefix}/etc/${conffile}.sample]} {
            file copy ${prefix}/etc/${conffile}.sample ${prefix}/etc/${conffile}
        }
    }
    xinstall -d ${prefix}/var/log
    touch ${prefix}/var/log/redis.log
}

startupitem.create      yes
startupitem.executable  ${prefix}/bin/redis-server ${prefix}/etc/redis.conf

livecheck.regex     ${name}-(\\d+.\\d+.\\d+)

notes "
If you prefer to start a redis server manually, rather than using 'port load', then use this command:

    redis-server ${prefix}/etc/redis.conf

Redis is now licensed under the dual Server Side Public License or Redis Source Available License.
The BSD-licensed community fork is valkey; you can install a redis-compatible port with:

    port install valkey +redis_compat
"

# Legacy/LTS subport: redis7
subport redis7 {
    version             7.4.9
    revision            0
    conflicts           redis
    distname            redis-${version}

    checksums           rmd160  66535b09659b9208c8a35469f348e8630d13abde \
                        sha256  a71a67b47b2705d3448f0400573e3ad5c4c9f8c18236f426dc6acc7284bf42ad \
                        size    3542936

    patchfiles-delete   patch-tests-Makefile.diff

    livecheck.regex     ${name}-(7.\\d+.\\d+)
}
