##
##  db.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_version  4.7.25
%define       V_revision 4

#   package information
Name:         db
Summary:      Berkeley-DB Library
URL:          http://www.oracle.com/database/berkeley-db.html
Vendor:       Keith Bostic
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Database
License:      BSD
Version:      %{V_version}.%{V_revision}
Release:      20160305

#   package options
%option       with_pthreads no
%option       with_compat   no
%option       with_cxx      no

#   package options sanity check
%if "%{with_pthreads}" == "yes"
    %{warn: WARNING: enabling Pthreads will potentially let other packages fail to build! }
%endif

#   list of sources
Source0:      http://download-uk.oracle.com/berkeley-db/db-%{V_version}.tar.gz
Source1:      db.pc
Patch0:       db.patch
Patch1:       http://www.oracle.com/technology/products/berkeley-db/db/update/%{V_version}/patch.%{V_version}.1
Patch2:       http://www.oracle.com/technology/products/berkeley-db/db/update/%{V_version}/patch.%{V_version}.2
Patch3:       http://www.oracle.com/technology/products/berkeley-db/db/update/%{V_version}/patch.%{V_version}.3
Patch4:       http://www.oracle.com/technology/products/berkeley-db/db/update/%{V_version}/patch.%{V_version}.4

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_cxx}" == "yes"
BuildPreReq:  gcc::with_cxx = yes
%endif

%description
    Berkeley DB is a programmatic toolkit that provides high-performance
    built-in database support for desktop and server applications and
    for information appliances. The Berkeley DB access methods include
    B+tree, Extended Linear Hashing, Fixed and Variable-length records,
    and Queues. Berkeley DB provides full transactional support,
    database recovery, online backups, and separate access to locking,
    logging and shared memory caching subsystems.

%track
    prog db = {
        disabled
        comment   = "DB 4.8 seems to require Pthread unconditionally (not just for mutex)"
        version   = %{V_version}
        url       = http://www.oracle.com/technology/software/products/berkeley-db/index.html
        regex     = Berkeley\s+DB\s+(__VER__)\s+\.tar\.gz
    }

%prep
    %setup -q -n db-%{V_version}
    chmod -R u+rw .
    %patch -p0 -P 0
    %patch -p0 -P 1
    %patch -p0 -P 2
    %patch -p0 -P 3
    %patch -p0 -P 4

    %{l_shtool} subst \
        -e 's;\($(ar) cr .*\);\1 _udivdi3.o _muldi3.o _clz.o;' \
        -e 's;install_utilities install_docs;install_utilities;' \
        dist/Makefile.in

%build
    #   configure library
%if "%{with_pthreads}" == "yes"
    mutex="POSIX/pthreads"
%else
    mutex="no"
    case "%{l_platform -p}" in
        amd64-*  ) mutex="x86_64/gcc-assembly";;
        ix86-*   ) mutex="x86/gcc-assembly"   ;;
        ia64-*   ) mutex="ia64/gcc-assembly"  ;;
        sparc*-* ) mutex="Sparc/gcc-assembly" ;;
        ppc*-*   ) mutex="PPC/gcc-assembly"   ;;
        alpha*-* ) mutex="ALPHA/gcc-assembly" ;;
        *        ) mutex="UNIX/fcntl"         ;;
    esac
%endif
    cd build_unix
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
%if "%{with_cxx}" == "yes"
    CXX="%{l_cxx}" \
    CXXFLAGS="%{l_cxxflags -O}" \
%endif
    GREP="grep" \
    ../dist/configure \
        --prefix=%{l_prefix} \
%if "%{with_compat}" == "yes"
        --enable-compat185 \
%else
        --disable-compat185 \
%endif
%if "%{with_cxx}" == "yes"
        --enable-cxx \
%else
        --disable-cxx \
%endif
        --disable-java \
        --with-mutex="$mutex" \
%if "%{with_pthreads}" == "yes"
        --enable-pthread_api \
%endif
        --disable-shared

    #   build library
    ar -vx `%{l_cc} -print-libgcc-file-name` _udivdi3.o _muldi3.o _clz.o
    %{l_make} %{l_mflags -O}

%install
    #   install library
    ( cd build_unix
      %{l_make} %{l_mflags} install \
          prefix=$RPM_BUILD_ROOT%{l_prefix}
    ) || exit $?

    #   strip down installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/libdb-[0-9]*.a
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
%if "%{with_compat}" == "no"
    rm -f $RPM_BUILD_ROOT%{l_prefix}/include/db_185.h
%endif
%if "%{with_cxx}" == "no"
    rm -f $RPM_BUILD_ROOT%{l_prefix}/include/db_cxx.h
    rm -f $RPM_BUILD_ROOT%{l_prefix}/include/cxx_*.h
%endif

    #   install pkg-config configuration
    libs="-ldb"
    case "%{l_platform -t}" in
        *-sunos* ) libs="$libs -lrt -lsocket -lnsl" ;;
    esac
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/pkgconfig
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@version@;%{version};" \
        -e "s;@libs@;$libs;" \
        %{SOURCE db.pc} \
        $RPM_BUILD_ROOT%{l_prefix}/lib/pkgconfig/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

