#!/bin/sh

# SlackBuild script borrowed from the Slamd64 project  -jmd

# 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:-sparc}
BUILD=0
# TUPLE=$ARCH-slackware-linux
TUPLE=$ARCH-splack-linux

CWD=`pwd`

PKG=/tmp/package-db3
rm -rf $PKG
rm -rf /tmp/db-$VERSION

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
CFLAGS="-O2 -fPIC" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib \
  --enable-compat185 \
  --enable-shared \
  --enable-rpc \
  $TUPLE
make -j3
make prefix=$PKG/usr libdir=$PKG/usr/lib install
# Remove misplaced docs:
rm -r $PKG/usr/docs
chmod 755 $PKG/usr/lib/libdb-3.3.so
( cd $PKG/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)
mkdir $PKG/lib
mv $PKG/usr/lib/libdb-3.3.so $PKG/lib/libdb-3.3.so
ln -sf /lib/libdb-3.3.so $PKG/usr/lib/libdb-3.3.so
( cd $PKG/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 $PKG/usr/doc/db-$VERSION
# Sorry, too much.
#cd ../docs
#cp -a * /usr/doc/db-$VERSION
cd ..
cp -a \
  LICENSE README \
  $PKG/usr/doc/db-$VERSION
cat << EOF > $PKG/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 $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
makepkg -l y -c n db3-$VERSION-$ARCH-$BUILD.tgz
echo Moving db3-$VERSION-$ARCH-$BUILD.tgz to /tmp
mv db3-$VERSION-$ARCH-$BUILD.tgz /tmp
