#!/bin/bash
# Heavily based on the Slackware 12.0 SlackBuild
# http://equ.sourceforge.net/
# SlackBuild by gallows ~at~ tiscali ~dot~ it

NAME=eq-xmms
VERSION=0.7
SKINS_VERSION=0.2
[ -z "$ARCH" ] && ARCH=i486
[ -z "$TMP" ] && TMP=/tmp
BUILD=1gal
PKG=$TMP/package-$NAME
CWD=`pwd`

TARBALL=$NAME-$VERSION.tar.bz2
SKINS=$NAME-skins-${SKINS_VERSION}.tar.bz2
PACK=$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ $ARCH == "i486" ]; then
 OPTFLAGS="-O2 -march=i486 -mtune=i686"
elif [ $ARCH == "athlon-xp" ]; then
 OPTFLAGS="-O2 -march=athlon-xp -fomit-frame-pointer -pipe"
elif [ $ARCH == "pentium4" ]; then
 OPTFLAGS="-O2 -march=pentium4 -fomit-frame-pointer -pipe"
elif [ $ARCH == "pentiumm" ]; then
 OPTFLAGS="-O2 -march=pentium-m -fomit-frame-pointer -pipe"
fi

function get_sources()
{
  wget http://kent.dl.sourceforge.net/sourceforge/equ/$TARBALL || exit 1
}

function get_skins()
{
  wget http://surfnet.dl.sourceforge.net/sourceforge/equ/$SKINS || exit 1
}

TARGET=i486

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

[ ! -e $TARBALL ] && get_sources

tar xvjf $TARBALL -C $TMP
cd $TMP/$NAME-$VERSION

CFLAGS=${OPTFLAGS} \
sh configure --prefix=/usr \
             --program-prefix= \
	     --program-suffix= \
	     --build=$TARGET-slackware-linux
 
make -j2 || exit 1
make install DESTDIR=$PKG

cd $CWD

[ ! -e $SKINS ] && get_skins

mkdir -p $PKG/usr/lib/xmms/
tar xvjf $SKINS -C $PKG/usr/lib/xmms/

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

cd $TMP/$NAME-$VERSION

cp -a   COPYING \
        AUTHORS \
        ChangeLog \
        INSTALL \
        README* \
	TODO \
	NEWS \
        SKINS \
        BUGS \
$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

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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 ##
