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

VERSION=1.0.8
KVERSION=${KVERSION:-2.4.29}
ARCH=${ARCH:-i486}
BUILD=${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-driver-$VERSION
tar xjvf $CWD/alsa-driver-$VERSION.tar.bz2
cd alsa-driver-$VERSION
chown -R root.root .
CFLAGS="$SLKCFLAGS" \
 ./configure \
  --with-isapnp=yes \
  --with-sequencer=yes \
  --with-oss=yes
if [ "$(uname -r)" = "2.4.28" ]; then # I don't know what this is about, but maybe the failed patches are already there...
  make -i -j3
  make -i install DESTDIR=$PKG
else
  make -j3
  make install DESTDIR=$PKG
fi

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

mkdir -p $PKG/usr/doc/alsa-driver-$VERSION/alsa-kernel
cp -a \
  CARDS-STATUS COPYING FAQ INSTALL README TODO WARNING \
  doc \
  $PKG/usr/doc/alsa-driver-$VERSION
cp -a \
  alsa-kernel/Documentation/ \
  $PKG/usr/doc/alsa-driver-$VERSION/alsa-kernel
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

cd $PKG
makepkg -l y -c n $TMP/alsa-driver-${VERSION}_${KVERSION}-$ARCH-$BUILD.tgz