#!/bin/bash

rm -f /usr/include/db.h
VERSION=$1

# Extract source:
cd $SLACKTRACKSCRATCHDIR
tar jxvvf $CWD/db-$VERSION.tar.bz2
cd db-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/patch.3.3.11.1.gz | patch -p0 --verbose

# Configure:
cd dist
./configure \
  --prefix=/usr \
  --enable-compat185 \
  --enable-shared \
  --enable-rpc \
  $ARCH-slackware-linux || failconfig

# Make & install:
make || failmake
make install

# Tidy up:
rm -rf /usr/docs

( cd /usr/lib
  chmod 755 libdb-3.3.so
  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 -f /usr/lib/libdb-3.3.so /lib
( cd /usr/lib
  ln -sf ../../lib/libdb-3.3.so . )

# Headers:
( 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 )

# Install docs:
mkdir -p /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

# Install package description:
mkdir -p /install
install -m644 $CWD/slack-desc /install
