# Build recipe for the berkeley-db.
#
# Copyright (c) 2017-2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=db
version=6.2.38
release=1

# Define a category for the output of the package name
pkgcategory=db

pkgname=berkeley-db

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch="
 https://rsync.dragora.org/current/sources/$tarname
 https://mirror.cedia.org.ec/dragora/current/sources/$tarname
 https://download.oracle.com/berkeley-db/$tarname
"

description="
Berkeley database library.

Berkeley DB is a programmatic toolkit that provides embedded database
support for client/server applications.
"

homepage=https://www.oracle.com/database/berkeley-db/index.html
license=Custom

# Source documentation
docs="README LICENSE"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    cd build_unix
    ../dist/configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" \
    LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --enable-compat185 \
     --enable-dbm \
     --enable-cxx \
     --enable-stl \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Huge size for the package
    rm -rf "${destdir}/usr/docs"

    # Strip remaining binaries and libraries
    find "$destdir" -type f | xargs file | \
     awk '/ELF/ && /executable/ || /shared object/' | \
      cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

    find "$destdir" -type f | xargs file | awk '/current ar archive/' | \
     cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null || true

    cd ..

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"
}

