#!/bin/sh
# Set initial variables:
VERSION=1.85
ARCH=i386
BUILD=1

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-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

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
# -O2 -march=i386 -mcpu=i686:
zcat $CWD/db.1.85.cflags.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 --force $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
mkdir -p $PKG/lib
strip db_dump185
strip -g libdb1.so.2.1.3
cat libdb1.so.2.1.3 > $PKG/lib/libdb1.so.2.1.3
chmod 755 $PKG/lib/libdb1.so.2.1.3
mkdir -p $PKG/usr/bin
cat db_dump185 > $PKG/usr/bin/db1_dump185
chmod 755 $PKG/usr/bin/db1_dump185
chown -R root.bin $PKG/usr/bin
mkdir -p $PKG/usr/lib
cat libdb.a > $PKG/usr/lib/libdb1.a
mkdir -p $PKG/usr/include/db1
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
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Add links:
( cd $PKG
  ( cd lib ; rm -rf libdb.so.2 )
  ( cd lib ; ln -sf libdb1.so.2.1.3 libdb.so.2 )
  ( cd lib ; rm -rf libdb1.so.2 )
  ( cd lib ; ln -sf libdb1.so.2.1.3 libdb1.so.2 )
  ( cd usr/lib ; rm -rf libdb1.so )
  ( cd usr/lib ; ln -sf ../../lib/libdb1.so.2 libdb1.so )
)

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

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