#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-db48
VERSION=4.8.26
ARCH=alpha
BUILD=1
TARGET=$ARCH-alphaslack-linux

rm -rf $PKG
mkdir -p $PKG
( cd $TMP
rm -rf db-$VERSION
tar xzf $CWD/db-$VERSION.tar.gz
cd db-$VERSION
# Official patches:
#zcat $CWD/patch.4.8.26.1.gz | patch -p0 --verbose || exit 1
#zcat $CWD/patch.4.8.26.2.gz | patch -p0 --verbose || exit 1
#zcat $CWD/patch.4.8.26.3.gz | patch -p0 --verbose || exit 1
#zcat $CWD/patch.4.8.26.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
../dist/configure \
 --prefix=/usr \
 --enable-shared \
 --enable-diagnostic \
 --enable-cxx \
 --enable-tcl \
 --with-tcl=/usr/lib \
 --enable-compat185 \
 --enable-test \
 --build=$TARGET
make || exit 1
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 libdb48, please see
  the source tarball db-$VERSION.tar.bz2, which can be found
  in the AlphaSlack source tree in source/l/db48/, or on
  Oracle's website: http://download.oracle.com/berkeley-db/.

EOF
# Move include files:
( cd $PKG/usr/include
 mkdir db48
 mv *.h db48
  for file in db48/* ; do
    ln -sf $file .
  done
  # Better add this symlink, too, just to be safe...
 ln -sf db48 db4
)
# Put libdb-4.8.so into /lib since it might be needed
# before /usr is mounted (eg, nsswitch.conf can be set up to
# use databases instead of flat files)
mkdir -p $PKG/lib
mv $PKG/usr/lib/libdb-4.8.so $PKG/lib/libdb-4.8.so
( cd $PKG/usr/lib
 ln -sf /lib/libdb-4.8.so .
)
# Some things might look for these libraries by other names.
( cd $PKG/usr/lib
 ln -sf libdb-4.8.a libdb-4.a
 ln -sf libdb-4.8.a libdb4.a
 ln -sf libdb-4.8.a libdb.a
 ln -sf libdb-4.8.a libdb-48.a
 ln -sf libdb-4.8.a libdb48.a
 ln -sf libdb_cxx-4.8.a libdb_cxx-4.a
 ln -sf libdb_cxx-4.8.a libdb_cxx-48.a
 ln -sf libdb_cxx-4.8.a libdb_cxx.a
 ln -sf libdb_tcl-4.8.a libdb_tcl-4.a
 ln -sf libdb_tcl-4.8.a libdb_tcl-48.a
 ln -sf libdb_tcl-4.8.a libdb_tcl.a
 ln -sf libdb-4.8.so libdb4.so
 ln -sf libdb-4.8.so libdb48.so
 ln -sf libdb-4.8.so libdb.so
)
chmod 755 $PKG/usr/bin/*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
makepkg -l y -c n $TMP/db48-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/db48.build.log
