#!/bin/sh
PKGNAM=db42
VERSION=4.2.52
BUILD=6

. /etc/pkghelpers
pkghelpers_env

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 || exit 1
zcat $CWD/patch.4.2.52.2.gz | patch -p0 --verbose || exit 1
zcat $CWD/patch.4.2.52.3.gz | patch -p0 --verbose || exit 1
zcat $CWD/patch.4.2.52.4.gz | patch -p0 --verbose || exit 1
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 \
  --libdir=/usr/lib$LIBSUFFIX \
  --enable-shared \
  --enable-rpc \
  --enable-compat185 \
  --with-mutex=x86/gcc-assembly \
  $ARCH-$DISTRO-linux
make -j12 || exit 1
make install DESTDIR=$PKG
# 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 libdb42, please see
  the source tarball db-$VERSION.tar.bz2, which can be found
  in the Slamd64 source tree in source/l/db42/, or on
  Sleepycat's web site:  http://www.sleepycat.com.

EOF

# Put libdb-4.2.so into /lib$LIBSUFFIX since it might be needed
# before /usr is mounted.
mkdir -p $PKG/lib$LIBSUFFIX
mv $PKG/usr/lib$LIBSUFFIX/libdb-4.2.so $PKG/lib$LIBSUFFIX/libdb-4.2.so
( cd $PKG/usr/lib$LIBSUFFIX
  ln -sf /lib$LIBSUFFIX/libdb-4.2.so .
)

( cd $PKG/usr/include
  chmod 644 *
  mkdir db42
  mv db.h db_185.h db_cxx.h db42
  # Not any more... db-4.2 is on the way out.
  #ln -sf db42/db.h .
  #ln -sf db42/db_185.h .
  #ln -sf db42/db_cxx.h .
)

chmod 755 $PKG/usr/bin/*

( cd $PKG/usr/bin
  mv berkeley_db_svc berkeley_db42_svc
  for file in db_* ; do
    mv $file db42_`echo $file | cut -f 2- -d _`
  done
)

# Not the default DB version:
rm -f $PKG/usr/include/{db.h,db_185.h,db_cxx.h}
rm -f $PKG/usr/lib$LIBSUFFIX/lib$LIBSUFFIXdb-4.so
rm -f $PKG/usr/lib$LIBSUFFIX/lib$LIBSUFFIXdb.{a,so}
# We will not be linking statically against this, either.
# If you need to do that, drag up an old package from the Slackware 10.2 era...
rm -f $PKG/usr/lib$LIBSUFFIX/*.a

chmod 755 $PKG/usr/lib$LIBSUFFIX/*

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

cd $PKG
pkghelpers_fixup
pkghelpers_makepkg