# -*- 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.2
revision            0
conflicts           redis7
checksums           rmd160  94186ee38fa7b697be9a5fc507c2b6266ff81487 \
                    sha256  cea46526594fe05f05b9ff733179eb1263deccf4269059cf081fdef222634c88 \
                    size    4245565

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/

# 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-patch tweaks that are safe for both versions (guard hiredis file)
post-patch {
    if {[file exists ${worksrcpath}/redis.conf]} {
        reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/redis.conf
    }
    if {[file exists ${worksrcpath}/deps/hiredis/Makefile]} {
        reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/deps/hiredis/Makefile
    }
}

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.8
    revision            0
    conflicts           redis
    distname            redis-${version}

    checksums           rmd160  60704caf8bbe1784c2d1a1c8c6ff0c88f95eecc8 \
                        sha256  f6773cb7d63be236c59c2917a82f1f08e47b77d89b2f0c9f53becb22b8ea4172 \
                        size    3538795

    patchfiles-delete   patch-tests-Makefile.diff

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