#!/bin/bash

# db31.SlackBuild
# Heavily based on the original Slackware build script.
# Modified by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# 21-Jun-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export ARCH_CFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export ARCH_CFLAGS="-O2" ;;
   sparc)   export ARCH_CFLAGS="-O2" ;;
   alpha)   export ARCH_CFLAGS="-O2" ;;
   x86_64)  export ARCH_CFLAGS="-O2" ;;
   i486)    export ARCH_CFLAGS="-O2" ;;
   mips)    export ARCH_CFLAGS="-O2" ;;
   *)       export ARCH_CFLAGS="-O2" ;;
esac

# Extract source:
tar zxvvf $CWD/db-$VERSION.tar.gz
cd db-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/db-3.1.17.mutex.diff.gz | patch -p1 -E --suffix=.orig --backup
cd dist

# Build the static library:
./configure \
  --prefix=/usr \
  --enable-compat185 \
  --disable-shared \
  --enable-rpc \
  $ARCH-slackware-linux || failconfig
make || failmake
mkdir -p $PKG/usr/lib
install -m644 libdb.a $PKG/usr/lib/libdb-3.1.a
make clean

# Build the shared library and everything else:
./configure \
  --prefix=/usr \
  --enable-compat185 \
  --enable-static \
  --enable-shared \
  --enable-rpc \
  $ARCH-slackware-linux || failconfig
make || failmake

# Patch db-3.1.la to show installed lib:
zcat $CWD/db-3.1.17.la.diff.gz | patch -p0
install -m644 libdb-3.1.la $PKG/usr/lib/libdb-3.1.la
cd .libs
mkdir -p $PKG/usr/bin
install -m755 berkeley_db_svc \
              db_{archive,checkpoint,deadlock,dump,load,printlog,recover,stat,upgrade,verify} \
              $PKG/usr/bin 

# Install shared objects:
mkdir -p $PKG/lib
install -m755 libdb-3.1.so $PKG/lib/libdb-3.1.so
cd ..

# Install headers:
mkdir -p $PKG/usr/include/db31
install -m644 db.h $PKG/usr/include/db31
cd ../include
install -m644 db_cxx.h db_185.h $PKG/usr/include/db31

# Install docs:
cd ..
mkdir -p $PKG/usr/doc/db-$VERSION
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 libdb4, please see
  the source tarball db-$VERSION.tar.bz2, which can be found
  in the Slackware source tree in source/l/db4/, or on
  Sleepycat's web site:  http://www.sleepycat.com.

EOF

# Make symlinks:
rm -f $PKG/lib/libdb.so $PKG/usr/lib/libdb.so $PKG/usr/lib/libdb-3.1.so $PKG/usr/include/db.h $PKG/usr/lib/libdb.a
( cd $PKG/lib
  ln -sf libdb-3.1.so libdb.so
  cd $PKG/usr/lib
  ln -sf /lib/libdb-3.1.so libdb.so
  ln -sf /lib/libdb-3.1.so libdb-3.1.so
  ln -sf libdb-3.1.a libdb.a
  cd $PKG/usr/include
  ln -sf db31/db.h db.h )

# This is not the default version of libdb anymore:
rm -f $PKG/lib/libdb.so $PKG/usr/lib/libdb.so $PKG/usr/lib/libdb.a $PKG/usr/include/*.h

# Just the lib, please:
rm -rf $PKG/usr/bin

# Slackware policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # set all files to root.root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   . $PORTCWD/arm/pkger
 else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
