#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-bsd_db1

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
explodepkg $CWD/_bsd_db1.tar.gz

echo "+=========+"
echo "| db.1.85 |"
echo "+=========+"
cd $TMP
tar xzvf $CWD/db.1.85.tar.gz
cd db.1.85
# Add patches from the last version of glibc to include
# BSD DB libraries (2.1.3), and use that number as the
# library version number:
zcat $CWD/db.1.85.glibc.diff.gz | patch -p1 -E
mkdir -p $PKG/usr/doc/db.1.85
cp -a README man/*.3 $PKG/usr/doc/db.1.85
gzip -9 $PKG/usr/doc/db.1.85/*.3
chmod 644 $PKG/usr/doc/db.1.85/*
chown root.root $PKG/usr/doc/db.1.85/*
cd PORT/linux
make
strip libdb1.so.2.1.3 db_dump185
cat libdb1.so.2.1.3 > $PKG/lib/incoming/libdb1.so.2.1.3
cat db_dump185 > $PKG/usr/bin/db1_dump185
cat libdb.a > $PKG/usr/lib/libdb1.a
cat include/db.h > $PKG/usr/include/db1/db.h
cat include/mpool.h > $PKG/usr/include/db1/mpool.h
cat include/ndbm.h > $PKG/usr/include/db1/ndbm.h

# Build the package:
cd $PKG
tar czvf $TMP/bsd_db1.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

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