#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# cmus
# http://cmus.sourceforge.net/
# The Italian Slackware Community
# http://www.slacky.eu/
# Packager anycolouryoulike

NAME=cmus
VERSION=2.2.0
ARCHIVE=tar.bz2
ARCH=i486
BUILD=1an
SOURCE=http://mirror.greaterscope.net/$NAME/$NAME-$VERSION.$ARCHIVE

CWD=$(pwd)
TMP=/tmp/tgz/$NAME-$VERSION-$BUILD
PKG=/tmp/package

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

download_source() {
if [ ! -f $NAME-$VERSION.$ARCHIVE ]; then
    wget $SOURCE
fi
}
clean_directories() {
if [ ! -d $TMP ]; then
    mkdir -p $TMP
else
    rm -rf $TMP/*
fi
if [ ! -d $PKG ]; then
    mkdir -p $PKG
else
    rm -rf $PKG/*
fi
}
explode_archive() {
case $ARCHIVE in
    tar.gz|tgz) tar xvzf $CWD/$NAME-$VERSION.$ARCHIVE ;;
    tar.bz2) tar xvjf $CWD/$NAME-$VERSION.$ARCHIVE ;;
esac
}
set_permissions() {
chown -R root:root .
find -perm 664 -exec chmod 644 {} \;
find -perm 600 -exec chmod 644 {} \;
find -perm 444 -exec chmod 644 {} \;
find -perm 400 -exec chmod 644 {} \;
find -perm 440 -exec chmod 644 {} \;
find -perm 777 -exec chmod 755 {} \;
find -perm 775 -exec chmod 755 {} \;
find -perm 511 -exec chmod 755 {} \;
find -perm 711 -exec chmod 755 {} \;
find -perm 555 -exec chmod 755 {} \;
}
copy_docs() {
if [ ! -d $PKG/usr/doc/$NAME-$VERSION ]; then
    mkdir -p $PKG/usr/doc/$NAME-$VERSION
fi
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
for FILE in AUTHORS ChangeLog COPYING DOCS HACKING INSTALL LICENSE NEWS README TODO
do
    if [ -s $FILE ]; then
	cp -a $FILE $PKG/usr/doc/$NAME-$VERSION/
    fi
done
mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
}
strip_files() {
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
find | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
gzip -9 usr/man/man?/*.? 2> /dev/null
}

download_source
clean_directories
cd $TMP || exit 1
explode_archive
cd $NAME-$VERSION || exit 1
set_permissions
sed -ie '21s/ffmpeg/libavcodec/;22,23s/ffmpeg/libavformat/' ffmpeg.c
CFLAGS=$SLKCFLAGS \
./configure \
    prefix=/usr \
    mandir=/usr/man \
    exampledir=/usr/doc/$NAME-$VERSION/examples \
    CONFIG_FLAC=y \
    CONFIG_MAD=y \
    CONFIG_MODPLUG=y \
    CONFIG_MIKMOD=n \
    CONFIG_MPC=y \
    CONFIG_VORBIS=y \
    CONFIG_TREMOR=n \
    CONFIG_WAV=y \
    CONFIG_WAVPACK=y \
    CONFIG_MP4=y \
    CONFIG_AAC=y \
    CONFIG_FFMPEG=y \
    CONFIG_ALSA=y \
    CONFIG_AO=y \
    CONFIG_ARTS=y \
    CONFIG_OSS=y \
    CONFIG_SUN=n \
    CONFIG_WAVEOUT=n
echo "Press enter to compile"
read
make || exit 1
make install DESTDIR=$PKG || exit 1
copy_docs
cd $PKG || exit 1
strip_files
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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

exit 0
