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

VERSION=1.0.15
ARCH=${ARCH:-i586}
BUILD=1vl59
CONFIGURE_TRIPLATE=i486-slackware-linux

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fpic"
  CONFIGURE_TRIPLATE=x86_64-linux-pc
fi

cd $TMP
for file in \
alsa-lib-$VERSION.tar.bz2 \
alsa-oss-$VERSION.tar.bz2 \
alsa-plugins-$VERSION.tar.bz2 \
alsa-tools-$VERSION.tar.bz2 \
alsa-utils-$VERSION.tar.bz2 \
; do
  ( cd $TMP
    rm $(basename $file .tar.bz2)
    tar xjf $CWD/$file || exit 1
    cd $(basename $file .tar.bz2)

    ## Here's where we patch or set up any other package-specific stuff:
    if [ "$file" = "alsa-utils-$VERSION.tar.bz2" ]; then
      PACKAGE_SPECIFIC_OPTIONS="--disable-nls"
    fi

    chown -R root:root .
    find . \
     \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
     -exec chmod 755 {} \; -o \
     \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
     -exec chmod 644 {} \;
   
   if [ "$file" = "alsa-tools-$VERSION.tar.bz2" ]; then
    for dir in *; do
     if [ -d $dir ];then
      cd $dir
      ./configure --prefix=/usr \
      --sysconfdir=/etc \
      $PACKAGE_SPECIFIC_OPTIONS \
      --build=$CONFIGURE_TRIPLATE

      # Unset this now -- we're done with it
      unset PACKAGE_SPECIFIC_OPTIONS

      # I had a few things blow up with
      # -j > 1...
      make 
      make install
      make install DESTDIR=$PKG
      mkdir -p $PKG/usr/doc/$(basename $file .tar.bz2)
      # This will cause errors, but won't miss any docs:
      cp -a \
      AUTHORS BUGS MEMORY-LEAK COPYING COPYING.LIB COPYING_LIBS FAQ HACKING \
      INSTALL NEWS NOTES README README.Kiosk README.Plugins THANKS TODO \
      $PKG/usr/doc/$(basename $file .tar.bz2) 2> /dev/null \ || true
      cd ..
     fi
     echo $dir
    done
   else
    ./configure --prefix=/usr \
    --sysconfdir=/etc \
    $PACKAGE_SPECIFIC_OPTIONS \
    --build=$CONFIGURE_TRIPLATE
    # Unset this now -- we're done with it
    unset PACKAGE_SPECIFIC_OPTIONS

    # I had a few things blow up with
    # -j > 1...
    make || exit 1
    make install
    make install DESTDIR=$PKG
    mkdir -p $PKG/usr/doc/$(basename $file .tar.bz2)
    # This will cause errors, but won't miss any docs:
    cp -a \
    AUTHORS BUGS MEMORY-LEAK COPYING COPYING.LIB COPYING_LIBS FAQ HACKING \
    INSTALL NEWS NOTES README README.Kiosk README.Plugins THANKS TODO \
    $PKG/usr/doc/$(basename $file .tar.bz2) 2> /dev/null \ || true
   fi

    
  ) || exit 1
done || exit 1

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

# Le kludge r'mvoal
( cd $PKG/usr/share ; rm -f doc man )

# 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/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makeslapt --tlz $CWD/alsa-lib-$VERSION-$ARCH-$BUILD.tlz

