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

VERSION=1.0.5
ARCH=${ARCH:-i486}
BUILD=1

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=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
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
)
gzip -9 $PKG/usr/man/man?/*.?
( cd $PKG/usr/man/man1
  rm -f arecord.1
  ln -sf aplay.1.gz arecord.1.gz
)
chown -R root.bin $PKG/usr/bin $PKG/usr/sbin
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