#!/bin/sh
# mod slackbuild by submax from original slackware slackbuild

NAME=alsa-utils
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/utils/$NAME-$VERSION.tar.bz2

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

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=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
# apply patch alsaconf for slackware
zcat $CWD/alsa-utils.alsaconf.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
./configure \
--prefix=/usr \
--sysconfdir=/etc \
--mandir=/usr/man \
--program-prefix= \
--program-suffix= \
--build=$ARCH-slackware-linux
make -j4 || exit 1
make install DESTDIR=$PKG

# rc.alsa file slackware
mkdir -p $PKG/etc/rc.d
zcat $CWD/rc.alsa.gz > $PKG/etc/rc.d/rc.alsa
chmod 755 $PKG/etc/rc.d/rc.alsa

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

# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

# doc
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
  COPYING ChangeLog INSTALL TODO \
  seq/aconnect/README.aconnect \
  seq/aseqnet/README.aseqnet \
  $PKG/usr/doc/$NAME-$VERSION
cp -a alsamixer/README $PKG/usr/doc/$NAME-$VERSION/README.alsamixer
mkdir -p $PKG/usr/doc/$NAME-$VERSION/src
cp -a $CWD/$NAME.SlackBuild \
      $CWD/slack-desc \
      $CWD/alsa-utils.alsaconf.diff.gz \
      $CWD/rc.alsa.gz \
   $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 )
