#!/bin/bash

# db4.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/l/db4
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
   armv3)   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 jxvf $CWD/db-$VERSION.tar.bz2
cd db-*

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

# Ensure sensible permissions in the unarchived source:
slackhousekeeping

# Configure:
mkdir build-dir
cd build-dir
CFLAGS="${ARCH_CFLAGS}" \
CXXFLAGS="${ARCH_CFLAGS}" \
../dist/configure \
  --prefix=/usr \
  --enable-shared \
  --enable-rpc \
  --enable-compat185 \
  $ARCH-slackware-linux || failconfig

# Make:
make || failmake
make install DESTDIR=$PKG || failinstall

# Remove WAY TOO LARGE (and misplaced) docs:
rm -rf $PKG/usr/docs
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

# Put libdb-4.2.so into /lib since it might be needed
# before /usr is mounted (eg, nssswitch.conf requires it)
mkdir -p $PKG/lib
mv $PKG/usr/lib/libdb-4.2.so $PKG/lib/libdb-4.2.so
( cd $PKG/usr/lib
  ln -sf /lib/libdb-4.2.so . )

( cd $PKG/usr/include
  chmod 644 *
  mkdir db4
  mv db.h db_185.h db_cxx.h db4
  ln -sf db4/db.h .
  ln -sf db4/db_185.h .
  ln -sf db4/db_cxx.h . )

( cd $PKG/usr/lib
  ln -sf libdb-4.2.a libdb-4.a
  ln -sf libdb-4.2.a libdb.a )

# Some things need this name.  Whatta mess!
( cd $PKG/usr/lib
  ln -sf libdb-4.a libdb4.a
  ln -sf libdb-4.so libdb4.so )

( cd $PKG/usr/bin
  mv berkeley_db_svc berkeley_db4_svc
  for file in db_* ; do
    mv $file db4_`echo $file | cut -f 2- -d _`
  done )

# Not the default DB version (yet):
rm -f $PKG/usr/include/{db.h,db_185.h,db_cxx.h}
rm -f $PKG/usr/lib/libdb.{a,so}

# Enter the package's root:
cd $PKG

# Slackware policies:
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 = armv3 ]; 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
