%define java_arches %{ix86} alpha ia64 ppc s390 s390x
%define nptl_arches %{ix86}

# compatibility with legacy versions of rpm
%{!?_lib:%define _lib   lib}

%define __soversion     4.0
%define _libdb_a        libdb-%{__soversion}.a
%define _libcxx_a       libdb_cxx-%{__soversion}.a

Summary: The Berkeley DB database library (version 4) for C.
Name: db4
Version: 4.0.14
Release: 20
Source: http://www.sleepycat.com/update/%{version}/db-%{version}.tar.gz
Source1: http://www.sleepycat.com/update/1.85/db.1.85.tar.gz
Patch10: http://www.sleepycat.com/update/1.85/patch.1.1
Patch11: http://www.sleepycat.com/update/1.85/patch.1.2
Patch12: http://www.sleepycat.com/update/1.85/patch.1.3
Patch13: http://www.sleepycat.com/update/1.85/patch.1.4
Patch14: db-1.85-errno.patch
Patch15: db4-4.0.14-configure.patch
Patch16: db4-4.0.14-recover.patch
URL: http://www.sleepycat.com/
License: GPL
Group: System Environment/Libraries
Obsoletes: db1, db1-devel
BuildPreReq: perl
BuildPreReq: tcl
%ifarch %{java_arches}
BuildPreReq: jdkgcj >= 0.1.0
%endif
BuildRoot: %{_tmppath}/%{name}-root

%description
The Berkeley Database (Berkeley DB) is a programmatic toolkit that
provides embedded database support for both traditional and
client/server applications. The Berkeley DB includes B+tree, Extended
Linear Hashing, Fixed and Variable-length record access methods,
transactions, locking, logging, shared memory caching, and database
recovery. The Berkeley DB supports C, C++, Java, and Perl APIs. It is
used by many applications, including Python and Perl, so this should
be installed on all systems.

%package utils
Summary: Command line tools for managing Berkeley DB (version 4) databases.
Group: Applications/Databases
Requires: db4 = %{version}-%{release}
Obsoletes: db2-utils, db3-utils

%description utils
The Berkeley Database (Berkeley DB) is a programmatic toolkit that
provides embedded database support for both traditional and
client/server applications. Berkeley DB includes B+tree, Extended
Linear Hashing, Fixed and Variable-length record access methods,
transactions, locking, logging, shared memory caching, and database
recovery. DB supports C, C++, Java and Perl APIs.

This package contains command line tools for managing Berkeley DB
(version 4) databases.

%package devel
Summary: Development files for the Berkeley DB (version 4) library.
Group: System Environment/Libraries
Requires: db4 = %{version}-%{release}
Obsoletes: db2-devel, db3-devel

%description devel
The Berkeley Database (Berkeley DB) is a programmatic toolkit that
provides embedded database support for both traditional and
client/server applications. This package contains the header files,
libraries, and documentation for building programs which use the
Berkeley DB.

%package java
Summary: Development files for using the Berkeley DB (version 4) with Java.
Group: System Environment/Libraries
Requires: %{name} = %{version}-%{release}

%description java
The Berkeley Database (Berkeley DB) is a programmatic toolkit that
provides embedded database support for both traditional and
client/server applications. This package contains the libraries
for building programs which use the Berkeley DB in Java.

%prep
%setup -q -n db-%{version} -a 1
%patch15 -p1 -b .configure
pushd mp
%patch16 -p0 -b .recover
popd
pushd dist
libtoolize --copy --force
popd
pushd db.1.85/PORT/linux
%patch10 -p0 -b .1
cd ../..
%patch11 -p0 -b .1
%patch12 -p0 -b .2
%patch13 -p0 -b .3
%patch14 -p1 -b .errno
popd

# Remove tags files which we don't need.
find . -name tags | xargs rm -f
# Define a shell function for fixing HREF references in the docs, which
# would otherwise break when we split the docs up into subpackages.
fixup_href() {
    for doc in $@ ; do
        chmod u+w ${doc}
        sed -e 's,="../api_c/,="../../%{name}-devel-%{version}/api_c/,g' \
            -e 's,="api_c/,="../%{name}-devel-%{version}/api_c/,g' \
            -e 's,="../api_cxx/,="../../%{name}-devel-%{version}/api_cxx/,g' \
            -e 's,="api_cxx/,="../%{name}-devel-%{version}/api_cxx/,g' \
            -e 's,="../api_java/,="../../%{name}-devel-%{version}/api_java/,g' \
            -e 's,="api_java/,="../%{name}-devel-%{version}/api_java/,g' \
            -e 's,="../api_tcl/,="../../%{name}-devel-%{version}/api_tcl/,g' \
            -e 's,="api_tcl/,="../%{name}-devel-%{version}/api_tcl/,g' \
            -e 's,="../examples_c/,="../../%{name}-devel-%{version}/examples_c/,g' \
            -e 's,="examples_c/,="../%{name}-devel-%{version}/examples_c/,g' \
            -e 's,="../examples_cxx/,="../../%{name}-devel-%{version}/examples_cxx/,g' \
            -e 's,="examples_cxx/,="../%{name}-devel-%{version}/examples_cxx/,g' \
            -e 's,="../ref/,="../../%{name}-devel-%{version}/ref/,g' \
            -e 's,="ref/,="../%{name}-devel-%{version}/ref/,g' \
            -e 's,="../sleepycat/,="../../%{name}-devel-%{version}/sleepycat/,g' \
            -e 's,="sleepycat/,="../%{name}-devel-%{version}/sleepycat/,g' \
            -e 's,="../images/,="../../%{name}-%{version}/images/,g' \
            -e 's,="images/,="../%{name}-%{version}/images/,g' \
            -e 's,="../utility/,="../../%{name}-%{version}/utility/,g' \
            -e 's,="utility/,="../%{name}-%{version}/utility/,g' ${doc} > ${doc}.new
        touch -r ${doc} ${doc}.new
        cat ${doc}.new > ${doc}
        touch -r ${doc}.new ${doc}
        rm -f ${doc}.new
    done
}

set +x  # XXX painful to watch
# Fix all of the HTML files.
fixup_href `find . -name "*.html"`
set -x  # XXX painful to watch

%build
CFLAGS="$RPM_OPT_FLAGS"; export CFLAGS

# Static link with old db-185 libraries.
make -C db.1.85/PORT/%{_os} OORG="$CFLAGS"
/bin/sh libtool --mode=compile %{__cc} $RPM_OPT_FLAGS -Idb.1.85/PORT/%{_os}/include -D_REENTRANT -c db_dump185/db_dump185.c -o dist/db_dump185.lo
/bin/sh libtool --mode=link    %{__cc} -o dist/db_dump185 dist/db_dump185.lo db.1.85/PORT/%{_os}/libdb.a

# XXX --enable-posixmutexes is useful for threads but only gives you interprocess locking if you have NPTL.
# XXX --enable-diagnostic should be disabled for production (but is useful).
# XXX --enable-debug_{r,w}op should be disabled for production.
# XXX --enable-java
pushd dist
%configure \
        --enable-compat185 --enable-dump185 \
        --enable-shared --enable-static --enable-rpc \
        --enable-tcl --with-tcl=%{_libdir} \
        --enable-cxx \
%ifarch %{java_arches}
        --enable-java \
%else
        --disable-java \
%endif
        --enable-test \
%ifarch %{nptl_arches}
        --enable-posixmutexes \
%endif
        # --enable-diagnostic \
        # --enable-debug --enable-debug_rop --enable-debug_wop \

make libdb=%{_libdb_a} libcxx=%{_libcxx_a} LIBSO_LIBS='$(LIBS)'
popd

%install
rm -rf ${RPM_BUILD_ROOT}
mkdir -p ${RPM_BUILD_ROOT}%{_includedir}
mkdir -p ${RPM_BUILD_ROOT}%{_libdir}
%makeinstall -C dist libdb=%{_libdb_a} libcxx=%{_libcxx_a}
chmod +x $RPM_BUILD_ROOT/%{_libdir}/*.so*

# On Linux systems, move the main shared library to lib directory, but leave the
# unversioned .so symlink in the usual place to link against.
%ifos linux
if [ "%{_libdir}" != "/%{_lib}" ]; then
        mkdir -p $RPM_BUILD_ROOT/%{_lib}
        mv $RPM_BUILD_ROOT/%{_libdir}/libdb-%{__soversion}.so $RPM_BUILD_ROOT/%{_lib}/
        touch $RPM_BUILD_ROOT/rootfile
        root=..
        while [ ! -e $RPM_BUILD_ROOT/%{_libdir}/${root}/rootfile ] ; do
                root=${root}/..
        done
        rm $RPM_BUILD_ROOT/rootfile
        ln -sf ${root}/%{_lib}/libdb-%{__soversion}.so $RPM_BUILD_ROOT/%{_libdir}/libdb.so
        ln -sf ${root}/%{_lib}/libdb-%{__soversion}.so $RPM_BUILD_ROOT/%{_libdir}/
fi
%endif

# Move the header files to a subdirectory, in case we're deploying on a
# system with multiple versions of DB installed.
mkdir -p $RPM_BUILD_ROOT/%{_includedir}/db4
mv $RPM_BUILD_ROOT/%{_includedir}/*.h $RPM_BUILD_ROOT/%{_includedir}/db4/

# Rename the utilities s/db_/dbN_/ (for multiple versions of DB installed)
#for p in berkeley_db_svc db_dump185 \
#        db_archive db_checkpoint db_deadlock db_dump db_load \
#        db_printlog db_recover db_stat db_upgrade db_verify; do
#    q="`echo $p | sed -e 's,db_,%{name}_,'`"
#    mv ${RPM_BUILD_ROOT}%{_bindir}/$p ${RPM_BUILD_ROOT}%{_bindir}/$q
#done

# Create symlinks to includes so that "use <db.h> and link with -ldb" works.
for i in db.h db_cxx.h db_185.h cxx_common.h cxx_except.h; do
        ln -s db4/$i $RPM_BUILD_ROOT%{_includedir}
done

%ifarch %{java_arches}
# Move java jar file to the correct place
mkdir -p $RPM_BUILD_ROOT%{_datadir}/java
mv $RPM_BUILD_ROOT%{_libdir}/*.jar $RPM_BUILD_ROOT%{_datadir}/java
%endif

# Eliminate installed doco
rm -rf ${RPM_BUILD_ROOT}%{_prefix}/docs

%clean
rm -rf ${RPM_BUILD_ROOT}

%post -p /sbin/ldconfig

%postun -p /sbin/ldconfig

%files
%defattr(-,root,root)
%doc LICENSE README docs/images
%ifos linux
/%{_lib}/libdb-%{__soversion}.so
%{_libdir}/libdb-%{__soversion}.so
%else
%{_libdir}/libdb-%{__soversion}.so
%endif
%{_libdir}/libdb_cxx-%{__soversion}.so

%files utils
%defattr(-,root,root)
%doc    docs/utility
%{_libdir}/libdb_tcl.so
%{_libdir}/libdb_tcl-%{__soversion}.a
%{_libdir}/libdb_tcl-%{__soversion}.la
%{_libdir}/libdb_tcl-%{__soversion}.so
%{_bindir}/berkeley_db*_svc
%{_bindir}/db*_archive
%{_bindir}/db*_checkpoint
%{_bindir}/db*_deadlock
%{_bindir}/db*_dump*
%{_bindir}/db*_load
%{_bindir}/db*_printlog
%{_bindir}/db*_recover
%{_bindir}/db*_stat
%{_bindir}/db*_upgrade
%{_bindir}/db*_verify

%files devel
%defattr(-,root,root)
%doc    docs/api_c docs/api_cxx docs/api_tcl docs/index.html
%doc    docs/ref docs/sleepycat
%doc    examples_c examples_cxx
%{_libdir}/libdb.so
%{_libdir}/libdb_cxx.so
%{_libdir}/libdb-%{__soversion}.la
%{_libdir}/libdb_cxx-%{__soversion}.la
%{_libdir}/%{_libdb_a}
%{_libdir}/%{_libcxx_a}
%{_includedir}/%{name}
%{_includedir}/*.h

%ifarch %{java_arches}
%files java
%defattr(-,root,root)
%doc docs/api_java
%{_libdir}/libdb_java*
%{_datadir}/java/*.jar
%endif

%changelog
* Wed Feb  5 2003 Nalin Dahyabhai <nalin@redhat.com> 4.0.14-20
- add dynamic libdb-4.0.so link back to %%{_libdir} so that dynamically
  linking with -ldb-4.0 will work again

* Tue Feb  4 2003 Nalin Dahyabhai <nalin@redhat.com> 4.0.14-19
- rebuild to use link the shared object with the same libraries we use
  for the bundled utils, should pull in libpthread when needed
- move libdb.so from /%%{_lib} to %%{_libdir} where the linker can find it

* Sun Feb 02 2003 Florian La Roche <Florian.LaRoche@redhat.de> 4.0.14-18
- add java for s390x

* Wed Jan 22 2003 Tim Powers <timp@redhat.com> 4.0.14-17
- rebuilt

* Wed Nov 20 2002 Florian La Roche <Florian.LaRoche@redhat.de> 4.0.14-16
- add java for s390

* Tue Oct  8 2002 Nalin Dahyabhai <nalin@redhat.com> 4.0.14-15
- add java bits back in for x86 boxes

* Fri Sep 20 2002 Than Ngo <than@redhat.com> 4.0.14-14.1
- Added better fix for s390/s390x/x86_64

* Thu Sep 05 2002 Arjan van de Ven
- remove java bits for x86-64

* Tue Aug 27 2002 Jeff Johnson <jbj@redhat.com> 4.0.14-14
- include libdb_tcl-4.1.a library.
- obsolete db1 packages.

* Tue Aug 13 2002 Nalin Dahyabhai <nalin@redhat.com> 4.0.14-13
- include patch to avoid db_recover (#70362)

* Sat Aug 10 2002 Elliot Lee <sopwith@redhat.com>
- rebuilt with gcc-3.2 (we hope)

* Tue Jul 23 2002 Nalin Dahyabhai <nalin@redhat.com> 4.0.14-11
- own %%{_includedir}/%{name}

* Tue Jul 23 2002 Tim Powers <timp@redhat.com> 4.0.14-10
- build using gcc-3.2-0.1

* Fri Jun 21 2002 Tim Powers <timp@redhat.com>
- automated rebuild

* Wed Jun 12 2002 Bernhard Rosenkraenzer <bero@redhat.com> 4.0.14-8
- Add java bindings
- Fix C++ bindings

* Thu May 23 2002 Tim Powers <timp@redhat.com>
- automated rebuild

* Thu May  9 2002 Jeff Johnson <jbj@redhat.com>
- re-enable db.h symlink creation, db_util names, and db[23]-devel obsoletes.
- make sure that -ldb is functional.

* Thu Feb 21 2002 Jeff Johnson <jbj@redhat.com>
- avoid db_util name collisions with multiple versions installed.

* Mon Feb 18 2002 Nalin Dahyabhai <nalin@redhat.com> 4.0.14-3
- remove relocatability stuffs
- swallow a local copy of db1 and build db185_dump statically with it, to
  remove the build dependency and simplify bootstrapping new arches

* Mon Jan 27 2002 Nalin Dahyabhai <nalin@redhat.com> 4.0.14-2
- have subpackages obsolete their db3 counterparts, because they conflict anyway

* Tue Jan  8 2002 Jeff Johnson <jbj@redhat.com> db4-4.0.14-1
- upgrade to 4.0.14.

* Sun Aug  5 2001 Nalin Dahyabhai <nalin@redhat.com>
- fix dangling docs symlinks
- fix dangling doc HREFs (#33328)
- apply the two patches listed at http://www.sleepycat.com/update/3.2.9/patch.3.2.9.html

* Tue Jun 19 2001 Bill Nottingham <notting@redhat.com>
- turn off --enable-debug

* Thu May 10 2001 Than Ngo <than@redhat.com>
- fixed to build on s390x

* Mon Mar 19 2001 Jeff Johnson <jbj@redhat.com>
- update to 3.2.9.

* Tue Dec 12 2000 Jeff Johnson <jbj@redhat.com>
- rebuild to remove 777 directories.

* Sat Nov 11 2000 Jeff Johnson <jbj@redhat.com>
- don't build with --enable-diagnostic.
- add build prereq on tcl.
- default value for %%_lib macro if not found.

* Tue Oct 17 2000 Jeff Johnson <jbj@redhat.com>
- add /usr/lib/libdb-3.1.so symlink to %%files.
- remove dangling tags symlink from examples.

* Mon Oct  9 2000 Jeff Johnson <jbj@redhat.com>
- rather than hack *.la (see below), create /usr/lib/libdb-3.1.so symlink.
- turn off --enable-diagnostic for performance.

* Fri Sep 29 2000 Jeff Johnson <jbj@redhat.com>
- update to 3.1.17.
- disable posix mutexes Yet Again.

* Tue Sep 26 2000 Jeff Johnson <jbj@redhat.com>
- add c++ and posix mutex support.

* Thu Sep 14 2000 Jakub Jelinek <jakub@redhat.com>
- put nss_db into a separate package

* Wed Aug 30 2000 Matt Wilson <msw@redhat.com>
- rebuild to cope with glibc locale binary incompatibility, again

* Wed Aug 23 2000 Jeff Johnson <jbj@redhat.com>
- remove redundant strip of libnss_db* that is nuking symbols.
- change location in /usr/lib/libdb-3.1.la to point to /lib (#16776).

* Thu Aug 17 2000 Jeff Johnson <jbj@redhat.com>
- summaries from specspo.
- all of libdb_tcl* (including symlinks) in db3-utils, should be db3->tcl?

* Wed Aug 16 2000 Jakub Jelinek <jakub@redhat.com>
- temporarily build nss_db in this package, should be moved
  into separate nss_db package soon

* Wed Jul 19 2000 Jakub Jelinek <jakub@redhat.com>
- rebuild to cope with glibc locale binary incompatibility

* Wed Jul 12 2000 Prospector <bugzilla@redhat.com>
- automatic rebuild

* Sun Jun 11 2000 Jeff Johnson <jbj@redhat.com>
- upgrade to 3.1.14.
- create db3-utils sub-package to hide tcl dependency, enable tcl Yet Again.
- FHS packaging.

* Mon Jun  5 2000 Jeff Johnson <jbj@redhat.com>
- disable tcl Yet Again, base packages cannot depend on libtcl.so.

* Sat Jun  3 2000 Jeff Johnson <jbj@redhat.com>
- enable tcl, rebuild against tcltk 8.3.1 (w/o pthreads).

* Tue May 30 2000 Matt Wilson <msw@redhat.com>
- include /lib/libdb.so in the devel package

* Wed May 10 2000 Jeff Johnson <jbj@redhat.com>
- put in "System Environment/Libraries" per msw instructions.

* Tue May  9 2000 Jeff Johnson <jbj@redhat.com>
- install shared library in /lib, not /usr/lib.
- move API docs to db3-devel.

* Mon May  8 2000 Jeff Johnson <jbj@redhat.com>
- don't rename db_* to db3_*.

* Tue May  2 2000 Jeff Johnson <jbj@redhat.com>
- disable --enable-test --enable-debug_rop --enable-debug_wop.
- disable --enable-posixmutexes --enable-tcl as well, to avoid glibc-2.1.3
  problems.

* Mon Apr 24 2000 Jeff Johnson <jbj@redhat.com>
- add 3.0.55.1 alignment patch.
- add --enable-posixmutexes (linux threads has not pthread_*attr_setpshared).
- add --enable-tcl (needed -lpthreads).

* Sat Apr  1 2000 Jeff Johnson <jbj@redhat.com>
- add --enable-debug_{r,w}op for now.
- add variable to set shm perms.

* Sat Mar 25 2000 Jeff Johnson <jbj@redhat.com>
- update to 3.0.55

* Tue Dec 29 1998 Jeff Johnson <jbj@redhat.com>
- Add --enable-cxx to configure.

* Thu Jun 18 1998 Jeff Johnson <jbj@redhat.com>
- Create.