# -*- 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           cmake 1.1
PortGroup           compiler_blacklist_versions 1.0

github.setup        apple foundationdb 6.3.24
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0

categories          databases
license             Apache-2
maintainers         {me.com:spam_brian @brianwells} openmaintainer
description         FoundationDB gives you the power of ACID transactions in a distributed database.
long_description    FoundationDB is a distributed database designed to handle large volumes of structured data \
                    across clusters of commodity servers. It organizes data as an ordered key-value store and \
                    employs ACID transactions for all operating systems. It is especially well-suited for read/write workloads \
                    but also has excellent performance for write-intensive workloads. Users interact with the \
                    database using API language binding. FoundationDB is Open source, is Ready for production, \
                    provides Industry-leading performance, is Fault tolerant and is Multi-model. This Port provides \
                    Latest FoundationDB Release to macOS with full support for all these features.

homepage            https://www.foundationdb.org/

checksums           rmd160  470b145f9974cc0898a176425d72f9bc43704248 \
                    sha256  e5ab9925d1a1f2f3ceb48ce4d3c5211176aedf3f04df7d3945b578f7646b9fb2 \
                    size    10654924

# Required python version
set py_ver          3.10
set py_ver_nodot    [string map {. {}} ${py_ver}]

depends_lib-append  port:mono
depends_build-append port:python${py_ver_nodot}

# make sure FoundationDB uses python from the selected python port
configure.env-append  PYTHON_ROOT_DIR=${prefix}/bin/python${py_ver}

# The FoundationDB build can easily run out of memory and crash,
# so the recommendation is to use "ninja -j1" when building.
use_parallel_build  no
cmake.generator     Ninja

# Requires c++17 (std::variant)
compiler.cxx_standard   2017
compiler.blacklist-append {clang < 1100} {macports-clang-[4-9].0}

destroot {
    # install compiled binaries

    xinstall -d -m 755 ${destroot}${prefix}/bin
    xinstall -m 755 ${workpath}/build/packages/bin/fdbcli \
        ${workpath}/build/packages/bin/fdbbackup \
        ${destroot}${prefix}/bin
    foreach x {backup_agent dr_agent fastrestore_tool fdbdr fdbrestore} {
        ln -f -s fdbbackup ${destroot}${prefix}/bin/${x}
    }
    
    xinstall -d -m 755 ${destroot}${prefix}/lib
    xinstall -m 755 ${workpath}/build/packages/lib/libfdb_c.dylib \
        ${destroot}${prefix}/lib

    xinstall -d -m 755 ${destroot}${prefix}/libexec
    xinstall -m 755 ${workpath}/build/packages/bin/fdbmonitor \
        ${destroot}${prefix}/libexec

    xinstall -d -m 755 ${destroot}${prefix}/sbin
    xinstall -m 755 ${workpath}/build/packages/bin/fdbserver \
        ${destroot}${prefix}/sbin

    # install header files

    xinstall -d -m 755 ${destroot}${prefix}/include/foundationdb
    xinstall -m 755 ${worksrcpath}/bindings/c/foundationdb/fdb_c.h \
        ${workpath}/build/bindings/c/foundationdb/fdb_c_options.g.h \
        ${worksrcpath}/fdbclient/vexillographer/fdb.options \
        ${destroot}${prefix}/include/foundationdb

    # install sample config files and adjust paths

    xinstall -d -m 755 ${destroot}${prefix}/etc/foundationdb
    xinstall -m 755 ${worksrcpath}/packaging/osx/foundationdb.conf.new \
        ${destroot}${prefix}/etc/foundationdb

    reinplace "s|/usr/local/etc/|${prefix}/etc/|g" ${destroot}${prefix}/etc/foundationdb/foundationdb.conf.new
    reinplace "s|/usr/local/libexec/|${prefix}/sbin/|g" ${destroot}${prefix}/etc/foundationdb/foundationdb.conf.new
    reinplace "s|/usr/local/foundationdb/backup_agent/backup_agent|${prefix}/bin/backup_agent|g" ${destroot}${prefix}/etc/foundationdb/foundationdb.conf.new
    reinplace "s|/usr/local/foundationdb/|${prefix}/foundationdb/|g" ${destroot}${prefix}/etc/foundationdb/foundationdb.conf.new
}