#!/bin/bash
# Heavily based on the Slackware 12.0 SlackBuild
# http://icculus.org/physfs/
# SlackBuild by gallows ~at~ tiscali ~dot~ it

NAME=physfs
VERSION=1.1.1
[ -z "$ARCH" ] && ARCH=i686
[ -z "$TMP" ] && TMP=/tmp
BUILD=1gal
PKG=$TMP/package-$NAME
CWD=`pwd`

TARBALL=$NAME-$VERSION.tar.gz
PACK=$NAME-$VERSION-$ARCH-$BUILD.tgz

function get_sources() 
{
  wget http://icculus.org/physfs/downloads/$TARBALL || exit 1
}

TARGET=i486

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

[ ! -e $TARBALL ] && get_sources

tar xvzf $TARBALL -C $TMP
cd $TMP/$NAME-$VERSION
cmake .
make
make install DESTDIR=$PKG
mv $PKG/usr/local/* $PKG/usr
rm -rf $PKG/usr/local

mkdir -p -m 755 $PKG/usr/doc/$NAME-$VERSION
cp -a CHANGELOG.txt CREDITS.txt INSTALL.txt LICENSE.txt TODO.txt $PKG/usr/doc/$NAME-$VERSION

cd $PKG
chmod 644 $PKG/usr/doc/$NAME-$VERSION/*

cp -a $CWD/slack-desc $CWD/$NAME.SlackBuild $PKG/usr/doc/$NAME-$VERSION

chown -R root:root .

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

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 -ne "\nmaking $NAME package.. "
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -c n -l y $CWD/$PACK &>/dev/null
echo "done"

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

## EOF ##
