#!/bin/sh
CWD=`pwd` 
TMP=/tmp
PKG=$TMP/package-alsa-utils
rm -rf $PKG
mkdir -p $PKG

VERSION=1.0.11
ARCH=${ARCH:-i486}
BUILD=2

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

cd $TMP
rm -rf alsa-utils-$VERSION
tar xjvf $CWD/alsa-utils-$VERSION.tar.bz2
cd alsa-utils-$VERSION
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
make -j3
make install DESTDIR=$PKG
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
( 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

mkdir -p $PKG/usr/doc/alsa-utils-$VERSION
cp -a \
  COPYING ChangeLog INSTALL TODO \
  seq/aconnect/README.aconnect \
  seq/aseqnet/README.aseqnet \
  $PKG/usr/doc/alsa-utils-$VERSION
cp -a alsamixer/README $PKG/usr/doc/alsa-utils-$VERSION/README.alsamixer
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n $TMP/alsa-utils-$VERSION-$ARCH-$BUILD.tgz

