#!/bin/sh

# This is in elflibs, so we shouldn't need to include it.
# If you need to build against it, it's in /pasture.
#./db-3.1.17.buildsharedlib

# Set initial variables:
VERSION=3.3.11
ARCH=${ARCH:-i486}
CWD=`pwd`

rm -f /usr/include/db.h
cd /tmp
tar xjvf $CWD/db-$VERSION.tar.bz2
cd db-$VERSION
zcat $CWD/patch.3.3.11.1.gz | patch -p0 --verbose
chown -R root.root .
find . -perm 775 | xargs chmod 755
find . -perm 444 | xargs chmod 644
cd dist
./configure \
  --prefix=/usr \
  --enable-compat185 \
  --enable-shared \
  --enable-rpc \
  $ARCH-slackware-linux
make -j3
make install
# Remove misplaced docs:
rm -r /usr/docs
chmod 755 /usr/lib/libdb-3.3.so
( cd /usr/lib
  rm -f libdb-3.so libdb.so
  ln -sf libdb-3.3.so libdb-3.so
  ln -sf libdb-3.3.so libdb.so
  rm -f libdb-3.a libdb.a
  ln -sf libdb-3.3.a libdb-3.a  
  ln -sf libdb-3.3.a libdb.a  
)
# Put libdb-3.3.so into /lib since it might be needed
# before /usr is mounted (eg, nssswitch.conf requires it)
mv /usr/lib/libdb-3.3.so /lib/libdb-3.3.so
ln -sf /lib/libdb-3.3.so /usr/lib/libdb-3.3.so
( cd /usr/include
  mkdir -p db3
  mv cxx_common.h cxx_except.h db.h db_185.h db_cxx.h db3
  ln -sf db3/db.h db.h
)
mkdir -p /usr/doc/db-$VERSION
# Sorry, too much.
#cd ../docs
#cp -a * /usr/doc/db-$VERSION
cd ..
cp -a \
  LICENSE README \
  /usr/doc/db-$VERSION
cat << EOF > /usr/doc/db-$VERSION/README-DOCS

  For a ton of additional documentation (too large to include
  here) on writing source code that uses libdb3, please see
  the source tarball db-$VERSION.tar.bz2, which can be found
  in the Slackware source tree in source/l/db3/, or on
  Sleepycat's web site:  http://www.sleepycat.com.

EOF
mkdir -p /install
cat $CWD/slack-desc > /install/slack-desc