# -*- 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
PortGroup  clang_dependency 1.0

name            db48
# Increase the revision of p5-berkeleydb any time the db48 version changes.
# See https://trac.macports.org/ticket/56561
version         4.8.30
revision        5
set branch      [join [lrange [split ${version} .] 0 1] {}]
categories      databases
license         Sleepycat
maintainers     {jmr @jmroot} openmaintainer
description     The Berkeley DB package, version 4.8
long_description \
    Version 4.8 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
master_sites    http://download.oracle.com/berkeley-db/

distname        db-${version}

checksums       md5     f80022099c5742cd179343556179aa8c \
                sha1    ab36c170dda5b2ceaad3915ced96e41c6b7e493c \
                rmd160  dd2fcd4c9b857a91e2f491fd4fadb0c51b993a9c

if {${os.platform} eq "darwin" && ${os.major} < 11 && ${cxx_stdlib} eq "libc++"} {
    # This port is in the dependency chain for clang 3.7 and later
    clang_dependency.extra_versions 3.7
}

patchfiles      patch-dbinc_atomic.h patch-dist_configure.diff

# Don't link with "-flat_namespace -undefined suppress" on Yosemite and
# later (#45705).
patchfiles-append   yosemite-libtool.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}

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.args       docdir=${prefix}/share/doc/${name}

if {$subport eq $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}
        }
        delete ${destroot}${prefix}/share/doc/${name}/java
    }
}

subport db48-java {
    PortGroup   java 1.0

    revision    0

    description Java bindings for Berkeley DB, version 4.8
    long_description    Java bindings for version 4.8 of the \
                        Berkeley Data Base library.

    depends_lib-append      port:${name}

    patchfiles-append   java_vers.patch

    java.version    1.5+

    pre-configure {
        configure.cppflags-append   -I${java.home}/include -I${java.home}/include/${os.platform}
    }
    configure.args-append   --enable-java
    configure.args-delete   --enable-cxx
    if {${os.platform} eq "darwin" && ${os.major} < 15} {
        set java_ver 1.5
    } elseif {${os.platform} eq "darwin" && ${os.major} < 18} {
        set java_ver 1.6
    } else {
        set java_ver 1.7
    }
    configure.env-append    "JAVACFLAGS=-source ${java_ver} -target ${java_ver}"

    # There is no Java specific install target, so delete everything
    # but the Java related files.
    post-destroot {
        set p ${destroot}${prefix}

        delete ${p}/bin
        delete ${p}/include

        # Install the docs into the main ports's doc directory so that
        # the link in index.html to java/index.html works.
        foreach f [glob -tails -directory ${p}/share/doc/${name} *] {
            if {${f} ne "java"} {
                delete ${p}/share/doc/${name}/${f}
            }
        }
        ln -s ${name} ${p}/share/doc/${subport}

        foreach f [glob -tails -directory ${p}/lib/${name} *] {
            if {${f} ne "db.jar"
                && ![string match libdb_java* ${f}]} {
                delete ${p}/lib/${name}/${f}
            }
        }
    }
}

subport db48-tcl {
    revision    0
    description Tcl bindings for Berkeley DB, version 4.8
    long_description    Tcl bindings for version 4.8 of the \
                        Berkeley Data Base library.
    depends_lib-append      port:${name} port:tcl
    configure.args-append   --enable-tcl --with-tcl=${prefix}/lib
    configure.args-delete   --enable-cxx

    post-destroot {
        set p ${destroot}${prefix}

        delete ${p}/bin
        delete ${p}/include
        delete ${p}/share/doc/${name}

        foreach f [glob -tails -directory ${p}/lib/${name} *] {
            if {![string match libdb_tcl* ${f}]} {
                delete ${p}/lib/${name}/${f}
            }
        }
    }
}

if {$subport eq $name} {
notes "The Java and Tcl bindings are now provided by the db48-java and
db48-tcl subports."
}

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

if { [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
