#!/bin/bash

if [ $# -ne 0 ]; then
  args="$@"
elif [ -r /etc/asound/driver_args ]; then
  args="`cat /etc/asound/driver_args`"
else
  args="--with-debug=full --with-isapnp=yes --with-sequencer=yes"
fi

if [ -r /usr/share/aclocal/alsa-lib.m4 ]; then
  rm -f /usr/share/aclocal/alsa-lib.m4
fi
if [ -r /usr/local/share/aclocal/alsa-lib.m4 ]; then
  rm -f /usr/local/share/aclocal/alsa-lib.m4
fi
#if [ -d ../alsa-lib ]; then
#  echo "Building hard symlinks with alsa-lib sources..."
#  cd kernel
#  pwd
#  cd ..
#fi

# check for alsa-kernel directory
if [ -d alsa-kernel -a -f alsa-kernel/last.c ]; then
  ALSAKERNELDIR="alsa-kernel"
else
  test -z $ALSAKERNELDIR && ALSAKERNELDIR=../alsa-kernel
  if [ ! -d $ALSAKERNELDIR -o ! -f $ALSAKERNELDIR/last.c ]; then
    echo "No alsa-kernel is found."
    echo "Please specify the correct location via ALSAKERNELDIR environment variable."
    exit 1
  fi
fi
make ALSAKERNELDIR="$ALSAKERNELDIR" all-deps
aclocal $ACLOCAL_FLAGS
autoconf
echo "./configure $args"
./configure $args || exit 1
if [ -z "$CVSCOMPILE_NO_MAKE" ]; then
  make dep || exit 1
  make
fi
