#!/bin/sh
# Set initial variables:
VERSION=3.1.17
ARCH=${ARCH:-i486}
BUILD=1

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-db31
if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

echo "+===========+"
echo "| db-3.1.17 |"
echo "+===========+"
cd $TMP
rm -rf db-3.1.17
tar xzvf $CWD/db-3.1.17.tar.gz
cd db-3.1.17
chown -R root.root .
find . -perm 775 | xargs chmod 755
find . -perm 444 | xargs chmod 644
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
make -j3
mkdir -p $PKG/usr/lib
strip -g libdb.a
cat 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
  # --enable-dump185 # this doesn't build so hot
make -j3
# Patch db-3.1.la to show installed lib:
zcat $CWD/db-3.1.17.la.diff.gz | patch -p0
cat libdb-3.1.la > $PKG/usr/lib/libdb-3.1.la
cd .libs
mkdir -p $PKG/usr/bin
cat berkeley_db_svc > $PKG/usr/bin/berkeley_db_svc
cat db_archive > $PKG/usr/bin/db_archive
cat db_checkpoint > $PKG/usr/bin/db_checkpoint
cat db_deadlock > $PKG/usr/bin/db_deadlock
cat db_dump > $PKG/usr/bin/db_dump
cat db_load > $PKG/usr/bin/db_load
cat db_printlog > $PKG/usr/bin/db_printlog
cat db_recover > $PKG/usr/bin/db_recover
cat db_stat > $PKG/usr/bin/db_stat
cat db_upgrade > $PKG/usr/bin/db_upgrade
cat db_verify > $PKG/usr/bin/db_verify
chown -R root:bin $PKG/usr/bin
chmod 755 $PKG/usr/bin/*
mkdir -p $PKG/lib
cat libdb-3.1.so > $PKG/lib/libdb-3.1.so
chmod 755 $PKG/lib/libdb-3.1.so
cd ..
mkdir -p $PKG/usr/include/db31
cat db.h > $PKG/usr/include/db31/db.h
cd ../include
cat db_185.h > $PKG/usr/include/db31/db_185.h
cat db_cxx.h > $PKG/usr/include/db31/db_cxx.h

#cd ../docs
#mkdir -p $PKG/usr/doc/db-3.1.17
#cp -a * $PKG/usr/doc/db-3.1.17

cd ..
cp -a LICENSE README $PKG/usr/doc/db-3.1.17

cat << EOF > $PKG/usr/doc/db-3.1.17/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

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

# 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
)

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# 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

# Build the package:
cd $PKG
makepkg -l y -c n ../db31-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/db-3.1.17
  rm -rf $PKG
fi
