# -*- 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             7.4.2
revision            0
categories          databases
license             SSPL

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/

checksums           rmd160  86968a20c18ee7a9383edcec1237d750f01ef0a7 \
                    sha256  4ddebbf09061cbb589011786febdb34f29767dd7f89dbe712d2b68e808af6a1f \
                    size    3533864

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

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

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

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

# 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

destroot.keepdirs   ${destroot}${prefix}/var/db/redis

installs_libs       no

makefile.has_destdir    no

post-destroot {
    foreach conffile {redis.conf sentinel.conf} {
        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 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

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
"

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