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

NAME=alsa-driver
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME
KVERSION=${KVERSION:-$(uname -r)}
VERSION=1.0.16
PKG_ARCH=${PKG_ARCH:-i486}
BUILD=1sm
CWD=`pwd`
SOURCE=ftp://ftp.alsa-project.org/pub/driver/$NAME-$VERSION.tar.bz2

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

if [ "$PKG_ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$PKG_ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$PKG_ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$PKG_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
cd $NAME-$VERSION

chown -R root:root .
CFLAGS="$SLKCFLAGS" \
 ./configure \
  --with-isapnp=yes \
  --with-sequencer=yes \
  --with-oss=yes
make -j3
make install DESTDIR=$PKG

rm $PKG/lib/modules/${KVERSION}/*
( cd $PKG/lib/modules
  find . -type f -name "*.o" -exec gzip -9 {} \;
)

mkdir -p $PKG/usr/doc/$NAME-$VERSION/alsa-kernel
cp -a \
  CARDS-STATUS COPYING FAQ INSTALL README SUPPORTED_KERNELS TODO WARNING \
  doc \
  $PKG/usr/doc/$NAME-$VERSION
cp -a \
  alsa-kernel/Documentation/ \
  $PKG/usr/doc/$NAME-$VERSION/alsa-kernel
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
# Write out the doinst.sh:
cat << EOF > $PKG/install/doinst.sh
# A good idea whenever kernel modules are added or changed:
if [ -x sbin/depmod ]; then
  chroot . /sbin/depmod -a ${KVERSION} 1> /dev/null 2> /dev/null
fi
EOF

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/../$NAME-${VERSION}_${KVERSION}-$PKG_ARCH-$BUILD.tgz

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

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