# -*- 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       muniversal 1.0

name            db53
# Increase the revision of p5-berkeleydb any time the db53 version changes.
# See https://trac.macports.org/ticket/56561
version         5.3.28
revision        0

categories      databases
license         Sleepycat
maintainers     nomaintainer

description     The Berkeley DB package, version 5.3
long_description \
    Version 5.3 of the Berkeley Data Base library which \
    offers (key/value) storage with optional concurrent \
    access or transactions interface.  This port will \
    install the AES (American Encryption Standard) \
    enabled version.
homepage        http://www.oracle.com/us/products/database/berkeley-db/db/overview/index.html

set branch      [join [lrange [split ${version} .] 0 1] {}]
master_sites    http://download.oracle.com/berkeley-db/
distname        db-${version}

checksums       rmd160  a4898908ef6dccb137df02e43843f7d3cfab6a3c \
                sha256  e0a992d740709892e81f9d93f06daf305cf73fb81b545afe72478043172c3628 \
                size    35090431

patchfiles      patch-src_dbinc_atomic.h \
                configure.patch

configure.dir   ${worksrcpath}/build_unix
build.dir       ${configure.dir}

configure.cmd   ../dist/configure
configure.args  --enable-cxx \
                --includedir=\\\${prefix}/include/db${branch} \
                --libdir=\\\${prefix}/lib/db${branch}

# for configure tests
configure.cflags-append \
                -Wno-error=implicit-int \
                -Wno-error=implicit-function-declaration

set cxx_stdlibflags {}
if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
    set cxx_stdlibflags -stdlib=${configure.cxx_stdlib}
}
configure.cxx ${configure.cxx} ${cxx_stdlibflags}

destroot.destdir \
    prefix=${destroot}${prefix} \
    docdir=${destroot}${prefix}/share/doc/${name}

post-destroot {
    foreach bin [glob -tails -directory ${destroot}${prefix}/bin/ db_*] {
        set newbin [regsub {^db_} ${bin} "db${branch}_"]
        move ${destroot}${prefix}/bin/${bin} \
            ${destroot}${prefix}/bin/${newbin}
    }
    if { [variant_isset sql] } {
        move ${destroot}${prefix}/bin/dbsql \
            ${destroot}${prefix}/bin/db${branch}sql
    }
}

variant java description {Build the Java API} {
    configure.args-append   --enable-java
    platform macosx {
        pre-configure {
            if {![file isfile "/System/Library/Frameworks/JavaVM.framework/Headers/jni.h"]} {
                ui_error "${name} requires the Java for Mac OS X development headers."
                if {${os.major} == 10} {
                    ui_error "Download the Java Developer Package from: <https://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?bundleID=20719>"
                } elseif {${os.major} == 9} {
                    ui_error "Download the Java Developer Package from: <https://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?bundleID=20720>"
                } else {
                    ui_error "Make sure your Xcode installation is complete."
                }
                return -code error "missing Java headers"
            }
        }
    }
}

if {${os.subplatform} eq "macosx" && ${os.major} < 11} {
    default_variants +java
}

variant sql description {Build the SQL API} {
    patchfiles-append       patch-sqlite.diff
    configure.args-append   --enable-sql
# build with -DSQLITE_ENABLE_COLUMN_METADATA
# required by libgda5 to enable Berkeley DB SQL support
    configure.cppflags-append -DSQLITE_ENABLE_COLUMN_METADATA
}
default_variants +sql

variant tcl description {Build Tcl API} {
    depends_lib-append      port:tcl
    configure.args-append   --enable-tcl --with-tcl=${prefix}/lib
}

variant umrw description {Mask harmless uninitialized memory read/writes for the benefit of memory debuggers such as Valgrind} {
    configure.args-append --enable-umrw
}

if {${universal_possible} && [variant_isset universal]} {
    # configure fails with aarch64 but it seems to work fine with arm
    set merger_host(arm64) arm-apple-${os.platform}${os.version}

    if {${os.arch} eq "i386"} {
        if { ${os.major} >= 10 } {
            set merger_configure_args(ppc) --with-mutex=Darwin/_spin_lock_try
        }
        set merger_configure_args(ppc64)   --with-mutex=Darwin/_spin_lock_try
    } else {
        set merger_configure_args(i386)    --with-mutex=x86/gcc-assembly
        set merger_configure_args(x86_64)  --with-mutex=x86_64/gcc-assembly
    }
}

livecheck.type  none
