#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-db4
rm -rf $PKG
mkdir -p $PKG

# Set initial variables:
VERSION=4.2.52
ARCH=${ARCH:-i486}
BUILD=2

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

cd $TMP
rm -rf db-$VERSION
tar xjvf $CWD/db-$VERSION.tar.bz2
cd db-$VERSION
zcat $CWD/patch.4.2.52.1.gz | patch -p0 --verbose
zcat $CWD/patch.4.2.52.2.gz | patch -p0 --verbose
chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
rm -rf build-dir
mkdir build-dir
cd build-dir
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
../dist/configure \
  --prefix=/usr \
  --enable-shared \
  --enable-rpc \
  --enable-compat185 \
  $ARCH-slackware-linux
make -j3
make install DESTDIR=$PKG
( 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
)
# 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
)

chmod 755 $PKG/usr/bin/*
chown -R root:bin $PKG/usr/bin

( 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}

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

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