#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# mod slackbuild by submax from original slackware slackbuild

NAME=alsa-lib
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME
CWD=`pwd`
VERSION=1.0.16
ARCH=${ARCH:-i486}
BUILD=1sm
SOURCE=ftp://ftp.alsa-project.org/pub/lib/$NAME-$VERSION.tar.bz2

if [ ! -e $CWD/$NAME-$VERSION.tar.bz2 ]; then
   wget -c $SOURCE
fi

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf $NAME-$VERSION
tar xjvf $CWD/$NAME-$VERSION.tar.bz2 || exit 1
cd $NAME-$VERSION
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
--program-prefix= \
--program-suffix= \
--build=$ARCH-slackware-linux
make -j4 || exit 1
make install DESTDIR=$PKG

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

# TODO
# ( cd src/pcm/ext
# make jack
# make jack-install )

# doc
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a INSTALL TODO $PKG/usr/doc/$NAME-$VERSION
cp -a COPYING $PKG/usr/doc/$NAME-$VERSION/COPYING.lib
cp -a aserver/COPYING $PKG/usr/doc/$NAME-$VERSION/COPYING.aserver
mkdir -p $PKG/usr/doc/$NAME-$VERSION/src
cp -a $CWD/$NAME.SlackBuild $CWD/slack-desc $PKG/usr/doc/$NAME-$VERSION/src

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

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/../$NAME-$VERSION-$ARCH-$BUILD.tgz

(cd $CWD/..
 md5sum $NAME-$VERSION-$ARCH-$BUILD.tgz > $NAME-$VERSION-$ARCH-$BUILD.tgz.md5
cat $PKG/install/slack-desc | grep "^$NAME" > $NAME-$VERSION-$ARCH-$BUILD.txt )

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