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

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-nss_db
DB=$TMP/db4

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

if ls /var/log/packages/autoconf* 1> /dev/null 2> /dev/null ; then
  echo "Please temporarily remove autoconf and automake, since they b0rk the build."
  echo "Thank you.  :-)"
  read stuff;
fi

# First we need to build a static PIC version of Berkeley DB with unique
# symbols.  This will remove the dependancy on db3 and make it a lot harder
# to shoot yourself in the foot by installing nss_db without a suitable
# Berkeley DB library available.  Thanks to Red Hat for the great hints and
# patches in their SRPM.  :-)

# Clear the way:
rm -rf $DB
mkdir -p $DB

# Build Berkeley DB:
cd $DB
mkdir db4
tar xzvf $CWD/db-4.0.14.tar.gz
cd db-4.0.14/dist
./configure --disable-shared --with-pic --with-uniquename=_nssdb --prefix=$DB/db4
make
make install

# OK, now we build nss_db and include all the Berkeley DB functions into it:

cd $TMP
tar xzvf $CWD/nss_db-2.2.tar.gz
cd nss_db-2.2
zcat $CWD/nss_db-2.2-external.patch.gz | patch -p1
# This would be used to build a db1 compatible version:
#zcat $CWD/nss_db-2.2-compat.patch.gz | patch -p1

CFLAGS="-O2 -march=i386 -mcpu=i686" ./configure --prefix=/ \
            --enable-shared \
            --with-db=$DB/db4 \
            i386-slackware-linux
make

mkdir -p $PKG/lib
cat .libs/libnss_db.so.2.0.0 > $PKG/lib/libnss_db.so.2.0.0
chmod 755 $PKG/lib/libnss_db.so.2.0.0
( cd $PKG/lib ; rm -rf libnss_db.so.2 )
( cd $PKG/lib ; ln -sf libnss_db.so.2.0.0 libnss_db.so.2 )
mkdir -p $PKG/var/db
cat db-Makefile > $PKG/var/db/Makefile
strip makedb
mkdir -p $PKG/usr/bin
cat makedb > $PKG/usr/bin/makedb
chown -R root.bin $PKG/usr/bin
chmod 755 $PKG/usr/bin/makedb
mkdir -p $PKG/usr/doc/nss_db-$VERSION
cp -a AUTHORS COPYING COPYING.LIB NEWS README THANKS $DB/db-4.0.14/LICENSE \
  $PKG/usr/doc/nss_db-$VERSION
chown -R root.root $PKG/usr/doc/nss_db-$VERSION
chmod 644 $PKG/usr/doc/nss_db-$VERSION/*

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

cd $PKG
makepkg -l y -c n ../nss_db-$VERSION-$ARCH-$BUILD.tgz