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

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

github.setup        valkey-io valkey 8.1.3
github.tarball_from archive
revision            0
categories          databases
license             BSD

maintainers         {makr @mohd-akram} openmaintainer

description         in-memory data store
long_description    Valkey is an open source (BSD) high-performance key/value \
                    datastore that supports a variety workloads such as \
                    caching, message queues, and can act as a primary database.

homepage            https://valkey.io

checksums           rmd160  b5a8577d8b757d41c71249ab5bbdc826850211cf \
                    sha256  8f862b3b0a72fd40587793964539589f6f83d01361ca1598b370cfaa301e0ec0 \
                    size    3811511

patchfiles          patch-valkey.conf.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/valkey.conf
}

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

# avoid build conflict with lua
# https://trac.macports.org/ticket/71078
configure.cppflags-replace \
                    -I${prefix}/include \
                    -isystem${prefix}/include

# error: address argument to atomic operation must be a pointer to non-const
compiler.c_standard 2017

if {[string match *gcc* ${configure.compiler}] &&
    ${configure.build_arch} in [list i386 ppc]} {
    # https://github.com/valkey-io/valkey/issues/434
    configure.ldflags-append    -latomic
}

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

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

# use jemalloc
build.args-append   USE_JEMALLOC=yes

# enable TLS.
build.args-append   BUILD_TLS=yes OPENSSL_PREFIX=${prefix}
destroot.args-append    OPENSSL_PREFIX=${prefix}
depends_lib         path:lib/libssl.dylib:openssl

# prevent conflict with redis
destroot.args-append    USE_REDIS_SYMLINKS=no

set db_dir          ${prefix}/var/db/${name}
set log_dir         ${prefix}/var/log/${name}
set run_dir         ${prefix}/var/run/${name}

destroot.keepdirs   ${destroot}${db_dir} \
                    ${destroot}${log_dir} \
                    ${destroot}${run_dir}

installs_libs       no

makefile.has_destdir    no

post-destroot {
    xinstall -d ${destroot}${prefix}/share/doc/${name}
    foreach conffile {valkey.conf sentinel.conf} {
        xinstall -m 0644 ${worksrcpath}/${conffile} \
            ${destroot}${prefix}/share/doc/${name}/${conffile}
    }
    xinstall -o ${name} -g ${name} -d ${destroot}${db_dir}
    xinstall -o ${name} -g ${name} -d ${destroot}${log_dir}
    xinstall -o ${name} -g ${name} -d ${destroot}${run_dir}
}

post-activate {
    foreach conffile {valkey.conf sentinel.conf} {
        if {![file exists ${prefix}/etc/${conffile}]} {
            file copy ${prefix}/share/doc/${name}/${conffile} \
                ${prefix}/etc/${conffile}
        }
    }
}

variant redis_compat description {Install redis compatibility symlinks} {
    conflicts               redis
    destroot.args-delete    USE_REDIS_SYMLINKS=no
}

add_users           ${name} group=${name} realname=Valkey\ Database\ Server
startupitem.create  yes
startupitem.user    ${name}
startupitem.group   ${name}
startupitem.executable  ${prefix}/bin/valkey-server ${prefix}/etc/valkey.conf

github.livecheck.regex  {([0-9.]+)}
