#!/bin/bash
# Heavily based on the Slackware 12.2 SlackBuild
# http://www.nongnu.org/tpb/

NAME="tpb"
VERSION="0.6.4"
ARCH="i486"
BUILD="3gal"
[ -z "$TMP" ] && TMP="/tmp"
PKG="$TMP/package-$NAME"
CWD="`pwd`"
OPTFLAGS="-O2 -march=$ARCH -mtune=i686"
TARGET="i486"

[ -e "$PKG" ] && rm -rf $PKG
[ -e "$TMP/$NAME-$VERSION" ] && rm -rf $TMP/$NAME-$VERSION

tar xvzf $NAME-$VERSION.tar.gz -C $TMP
cd $TMP/$NAME-$VERSION

CFLAGS=${OPTFLAGS} \
sh configure --prefix=/usr \
	     --sysconfdir=/etc \
	     --program-prefix= \
	     --program-suffix= \
	     --build=$TARGET-slackware-linux
 
make
make install DESTDIR=$PKG

mkdir -p -m 755 $PKG/usr/doc/$NAME-$VERSION

cp -a   COPYING \
	ABOUT-NLS \
 	ChangeLog \
	README \
	TODO \
	doc/callback_example.sh \
	doc/nvram.txt \
	$PKG/usr/doc/$NAME-$VERSION

cd $PKG
chown -R root:root .
chmod a+x $PKG/usr/doc/$NAME-$VERSION/callback_example.sh

find . -exec file {} \; | grep "executable" | cut -d : -f1 | xargs strip 2> /dev/null

gzip -9 $PKG/usr/man/*/*

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
echo
echo -n "making $NAME package.. "
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -c n -l y $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz &>/dev/null
echo "done"

[ "$1" == "--cleanup" ] && {
  echo -n "cleaning up.. "
  rm -rf $TMP/$NAME-$VERSION
  rm -rf $PKG
  echo "done"
}

## EOF ##
