#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://bogofilter.sourceforge.net/

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

NAME=bogofilter
VERSION=1.2.0
ARCH=i486
BUILD=1g3n
PKG=$TMP/package-$NAME

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG
fi

echo "+===================+"
echo "| $NAME-$VERSION |"
echo "+===================+"

cd $TMP
tar xjvf $CWD/$NAME-$VERSION.tar.bz2
cd $NAME-$VERSION
CFLAGS="-O2 -march=i486 -mtune=i686" \
./configure --prefix=/usr \
--with-included-gsl \
--sysconfdir=/etc/bogofilter \
--mandir=/usr/man \
--program-prefix= \
--program-suffix= \
--build=i486-slackware-linux
make
make install DESTDIR=$PKG
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS COPYING INSTALL NEWS README* TODO \
$PKG/usr/doc/$NAME-$VERSION

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

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
gzip $PKG/usr/man/man1/*
cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION
cd $PKG
chown root:root .
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME-$VERSION
  rm -rf $PKG
fi
