# -*- 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            db46
version         4.6.21
#subport db46-java revision has been bumped, reset it on next version update
revision        10
set branch      [join [lrange [split ${version} .] 0 1] {}]
categories      databases
license         Sleepycat
maintainers     nomaintainer
description     The Berkeley DB package, version 4.6
long_description \
    Version 4.6 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/
patch_sites     http://download.oracle.com/berkeley-db/patches/db/${version}/
patchfiles      patch.${version}.1 \
                patch.${version}.2 \
                patch.${version}.3 \
                patch.${version}.4

# https://trac.macports.org/ticket/66244
if {${os.platform} eq "darwin" && ${os.major} >= 22 } {
    depends_patch-append port:gpatch
    patch.cmd   ${prefix}/bin/gpatch
    patch.pre_args-append --read-only=ignore
}

distname        db-${version}
dist_subdir     db4/${version}_6
checksums       ${distname}${extract.suffix} \
                md5     718082e7e35fc48478a2334b0bc4cd11 \
                sha1    5be3beb82e42a78ff671a3f5a4c30e5652972119 \
                rmd160  ccf9a4b85cc0464b2f3c2f2da29d99328fd4978e \
                patch.${version}.1 \
                md5     4878872edfc53c6ecb871b1062a4bdaf \
                sha1    c7c155705687e4de03d06c2ea86940f573fdac0b \
                rmd160  8c11e9b991ac6559f22ece2d93617b16126049e8 \
                patch.${version}.2 \
                md5     55074e53d3acae2dcbeae8322f96e522 \
                sha1    a694b71088ba99b74042e7568f395fe467bb6590 \
                rmd160  f8abf554552db668037e046dea54700ce3340bf8 \
                patch.${version}.3 \
                md5     7a6e11b54712caf752f9c4a52babe60c \
                sha1    d52aa498943cfaa339ad6bf2cf7b836a40627701 \
                rmd160  4e6b7ed1e1c69cba54351d76a0b8543107a655b8 \
                patch.${version}.4 \
                md5     ae7d3d587355fe85b512ef09b9a77d19 \
                sha1    e2190185c667edb97e800495ce6eb4f95f43eb87 \
                rmd160  f1d18e59c311fb4e1f00a3b6220269fca17df312

# Generate libtool that understands -stdlib.
use_autoreconf          yes
autoreconf.dir          ${worksrcpath}/dist
autoreconf.cmd          ${autoreconf.dir}/s_config
autoreconf.args         --force --verbose --install \
                        --include=${prefix}/share/aclocal \
                        --include=${autoreconf.dir}/aclocal \
                        --include=${autoreconf.dir}/aclocal_java

patchfiles-append       autoreconf-fix.patch \
                        implicit.patch
depends_build-append    port:autoconf port:automake port:libtool

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

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

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

pre-extract {
    if {[file exists "/usr/local/include/db.h"]} {
            ui_error "You have a conflicting install of Berkeley DB in /usr/local, please move it aside before building ${name}"
            return -code error "Berkeley DB in /usr/local is in the way"
    }
}

subport ${name}-java {
    PortGroup   java 1.0

    revision 10
    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 --enable-dump185

    platform macosx {
        pre-configure {
            if {${os.major} < 11 && ![file isfile "/System/Library/Frameworks/JavaVM.framework/Headers/jni.h"]} {
                ui_error "${subport} 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 {$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
    }

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

    variant compat185 description {build with db185 compatibility mode} {
        configure.args-append   --enable-compat185
    }
} else {
    # 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 [lsort [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 [lsort [glob -tails -directory ${p}/lib/${name} *]] {
            if {${f} ne "db.jar"
                && [string compare -length 10 "libdb_java" ${f}]} {
                delete ${p}/lib/${name}/${f}
            }
        }
    }
}

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