#!/bin/bash
# Heavily based on the Slackware 12.1 SlackBuild
# http://adesklets.sourceforge.net/

NAME="pinball"
VERSION="0.3.1"
ARCH="i486"
BUILD="3pv"
CHOST="i486"
TMP="/tmp/build-$NAME"
PKG="$TMP/pkg"
CWD="`pwd`"
OPTFLAGS="-O2 -march=$ARCH -mtune=i686"

rm -rf $TMP
mkdir $TMP

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

CFLAGS=${OPTFLAGS} \
CXXFLAGS=${OPTFLAGS} \
sh configure --prefix=/usr \
--program-prefix= \
--program-suffix= \
--build=$CHOST-slackware-linux
make -j3
make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION/htmldoc

cp -a 	AUTHORS \
	COPYING \
	ChangeLog \
	INSTALL	\
	NEWS \
	README \
	$PKG/usr/doc/$NAME-$VERSION

chown root:root $PKG/usr/bin
rm -rf $PKG/usr/info
rm -rf $PKG/usr/man
mkdir -p $PKG/usr/info
mkdir -p $PKG/usr/man/man1
cp -a doc/*.info $PKG/usr/info
mv $TMP/pinball-0.3.1/* $PKG/usr/doc/$NAME-$VERSION/htmldoc

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

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

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION
chown -R root:root $PKG/usr/doc/$NAME-$VERSION
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
echo -n "making $NAME package.. "
makepkg -c n -l y $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz
echo "done"

if [ "$1" == "--cleanup" ]; then
  echo -n "cleaning up.. "
  rm -rf $TMP
  echo "done"
fi

## EOF ##
