dnl Process this file with autoconf to produce a configure script.
dnl
dnl Copyright (c) by Jaroslav Kysela <perex@suse.cz>

AC_INIT(kernel/sound.c)
AM_CONDITIONAL(INSTALL_M4, test "x${ACLOCAL}" = "xaclocal")
AC_PREFIX_DEFAULT(/usr)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_RANLIB
AC_PROG_INSTALL

dnl Checks for header files.
AC_HEADER_STDC
AC_CONFIG_HEADER(include/config.h include/config1.h include/version.h)
AC_CHECK_HEADERS(linux/fs.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_HEADER_TIME

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL

dnl Check for directory with kernel source...
AC_MSG_CHECKING(for directory with kernel source)
AC_ARG_WITH(kernel,
  [  --with-kernel=dir       give the directory with kernel sources]
  [                        [/usr/src/linux]],
  kerneldir="$withval", kerneldir="/usr/src/linux")
AC_DEFINE_UNQUOTED(CONFIG_SND_KERNELDIR, "$kerneldir")
CONFIG_SND_KERNELDIR="$kerneldir"
AC_SUBST(CONFIG_SND_KERNELDIR)
AC_MSG_RESULT($kerneldir)

dnl Check for kernel version...
AC_MSG_CHECKING(for kernel version)
kaversion=`grep UTS_RELEASE $CONFIG_SND_KERNELDIR/include/linux/version.h 2>/dev/null | cut -d\" -f2`
kversion=`echo $kaversion | cut -d . -f 1`
kpatchlevel=`echo $kaversion | cut -d . -f 2`
ksublevel=`echo $kaversion | cut -d . -f 3 | cut -d - -f 1`
kextraversion=`echo $kaversion | cut -d . -f 3 | cut -d - -f 2-`
if test "$kextraversion" = "$ksublevel"; then
  kextraversion=""
else
  kextraversion=`echo -$kextraversion`
fi
if echo $ksublevel | grep -E "[[[:digit:]]]+a" > /dev/null; then
  kextraversion=a`echo $ksublevel | cut -d a -f 2`
  ksublevel=`echo $ksublevel | cut -d a -f 1`
fi
if echo $ksublevel | grep -E "[[[:digit:]]]+pre" > /dev/null; then
  kextraversion=a`echo $ksublevel | cut -d e -f 2`
  ksublevel=`echo $ksublevel | cut -d p -f 1`
fi
kversion=`expr $kversion + 0`
kpatchlevel=`expr $kpatchlevel + 0`
ksublevel=`expr $ksublevel + 0`
if test -z "$kversion" || test -z "$kpatchlevel" || test -z "$ksublevel"; then
  echo "failed (probably missing $CONFIG_SND_KERNELDIR/include/linux/version.h)"
  exit 1
fi
kaversion="$kversion.$kpatchlevel.$ksublevel$kextraversion"
newkernel="0"
if test $kversion -gt 2; then
  newkernel="1"
else
  if test $kversion -eq 2 && test $kpatchlevel -gt 0; then
    newkernel="1"
  fi
fi
AC_SUBST(kaversion)
AC_SUBST(kversion)
AC_SUBST(kpatchlevel)
AC_SUBST(ksublevel)
AC_SUBST(kextraversion)
AC_SUBST(newkernel)
AC_MSG_RESULT($kaversion)

dnl Check for modversions...
AC_MSG_CHECKING(for kernel module symbol versions)
modversion="0"
AC_TRY_RUN([
#include "$CONFIG_SND_KERNELDIR/include/linux/autoconf.h"
int main( void ) {
#ifndef CONFIG_MODVERSIONS
  exit(1);
#else
  exit(0);
#endif
}
],
  AC_MSG_RESULT("yes");mversion="1",
  AC_MSG_RESULT("no");mversion="0",
  AC_MSG_RESULT("uknown");mversion="0"
)
CONFIG_SND_MVERSION=$mversion
AC_SUBST(CONFIG_SND_MVERSION)
GENKSYMS="/sbin/genksyms"
if test "$newkernel" = "1"; then
  GENKSYMS="$GENKSYMS -k $kversion.$kpatchlevel.$ksublevel"
fi

dnl Check for PCI support...
AC_MSG_CHECKING(for PCI support in kernel)
CONFIG_SND_PCI=`grep -E "^#define[ |\t]+CONFIG_PCI[ |\t]+1" $CONFIG_SND_KERNELDIR/include/linux/autoconf.h | wc -l`
CONFIG_SND_PCI=`expr $CONFIG_SND_PCI + 0`
AC_SUBST(CONFIG_SND_PCI)
if test $CONFIG_SND_PCI -eq 1; then
  AC_MSG_RESULT("yes")
else
  AC_MSG_RESULT("no")
fi

dnl Check for SGI/MIPS (HAL2) support...
AC_MSG_CHECKING(for SGI/MIPS (HAL2) architecture)
CONFIG_SND_SGI=0
if test -r $CONFIG_SND_KERNELDIR/include/asm/sgihpc.h; then
  CONFIG_SND_SGI=1
fi
AC_SUBST(CONFIG_SND_SGI)
if test $CONFIG_SND_SGI -eq 1; then
  AC_MSG_RESULT("yes")
else
  AC_MSG_RESULT("no")
fi


dnl Directory for modules
AC_MSG_CHECKING(for directory to store kernel modules)
AC_ARG_WITH(moddir,
  [  --with-moddir=/path     give the path for the alsa driver kernel modules]
  [                        [/lib/modules/<KVER>/misc]],
  moddir="$withval", 
# Special test for RH 5.1
  if test -d /lib/modules/preferred; then
    moddir="/lib/modules/preferred/misc"
  else
    moddir="/lib/modules/$kaversion/misc"
  fi
)
AC_SUBST(moddir)
AC_MSG_RESULT($moddir)

dnl Debug level
AC_MSG_CHECKING(for debug level)
AC_ARG_WITH(debug,
  [  --with-debug=level      give the debug level (none,basic,memory,full)],
  debug="$withval", debug="none")
case "$debug" in
  basic)
    AC_MSG_RESULT(basic)
    AC_DEFINE(CONFIG_SND_DEBUG)
    ;;
  memory)
    AC_MSG_RESULT(memory)
    AC_DEFINE(CONFIG_SND_DEBUG)
    AC_DEFINE(CONFIG_SND_DEBUG_MEMORY)
    ;;
  full)
    AC_MSG_RESULT(full)
    AC_DEFINE(CONFIG_SND_DEBUG)
    AC_DEFINE(CONFIG_SND_DEBUG_MEMORY)
    AC_DEFINE(CONFIG_SND_DEBUG_FULL)
    ;;    
  detect)
    AC_MSG_RESULT(detect)
    AC_DEFINE(CONFIG_SND_DEBUG)
    AC_DEFINE(CONFIG_SND_DEBUG_MEMORY)
    AC_DEFINE(CONFIG_SND_DEBUG_FULL)
    AC_DEFINE(CONFIG_SND_DEBUG_DETECT)
    ;;
  *)
    AC_MSG_RESULT(none)
    ;;
esac
AC_SUBST(moddir)

dnl Check processor type...
AC_MSG_CHECKING(for processor type)
AC_ARG_WITH(processor,
  [  --with-processor=cpu    give the processor type]
  [                        (i386,i486,i586,i686,alpha,ppc,mips,auto)],
  processor="$withval", processor="auto")
if test "$processor" = "auto"; then
  CPUID=`cat /proc/cpuinfo | grep cpu | grep -v "cpu family" | grep -v cpuid | grep -v "cpu MHz" | grep -v "cpu model" | cut -d : -f 2 | colrm 1 1 | head -n 1`
  if test "$CPUID""x" = "x"; then
    CPUID=`cat /proc/cpuinfo | grep "cpu family" | cut -d : -f 2 | colrm 1 1 | head -n 1`
    if test "$CPUID" != "alpha" && test "$CPUID" != "Alpha"; then
      CPUID="i""$CPUID""86"
    fi
  else
    if test "$CPUID" != "alpha" && test "$CPUID" != "Alpha" && test "$CPUID" != "MIPS" ; then
      CPUID="i""$CPUID"
    fi
  fi
  processor="$CPUID"
fi
c_opts=""
case "$processor" in
  i386)
    c_opts="-m386 -DCPU=386"
    AC_MSG_RESULT(i386)    
    ;;
  i486)
    c_opts="-m486 -DCPU=486"
    AC_MSG_RESULT(i486)    
    ;;
  i586)
    c_opts="-m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2 -DCPU=586"
    AC_MSG_RESULT(i586)    
    ;;
  i686)
    c_opts="-m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2 -DCPU=686"
    AC_MSG_RESULT(i686)    
    ;;
  alpha|Alpha)
    c_opts="-fno-strength-reduce -mno-fp-regs"
    if test "$newkernel" = "1"; then
      c_opts="$c_opts -ffixed-8"
    fi
    AC_MSG_RESULT(alpha)
    processor="alpha"
    ;;
  ppc)
    AC_MSG_RESULT(ppc)
    ;;
  mips|MIPS)
    c_opts="-mcpu=r4600 -mips2 -fno-pic -mno-abicalls -mlong-calls"
    AC_MSG_RESULT(mips)
    processor="mips"
    ;;
  *)
    AC_MSG_RESULT(unknown)
    processor="unknown"
    ;;
esac
c_opts="-O2 $c_opts"
AC_SUBST(processor)

dnl Check for SMP...
AC_MSG_CHECKING(for SMP)
AC_ARG_WITH(smp,
  [  --with-smp=yes,no,auto  driver will (not) be compiled with SMP support],
  smp="$withval", smp="auto")
msmp=""
if test "$smp" = "auto"; then
  if test $kversion -ge 2 && test $kpatchlevel -ge 2; then
    msmp=`grep -E "^#define[ |\t]+CONFIG_SMP[ |\t]+1" $CONFIG_SND_KERNELDIR/include/linux/autoconf.h | wc -l`
    msmp=`expr $msmp + 0`
  else
    CPUNUM=`grep -E "^SMP.+=.+1" $CONFIG_SND_KERNELDIR/Makefile`
    if test "$CPUNUM""x" = "x"; then
      CPUNUM=`cat /proc/cpuinfo | grep ^processor | wc -l`
      CPUNUM=`expr $CPUNUM + 0`
      if test $CPUNUM -gt 1; then
        msmp="1"
      fi
    else
      msmp="1"
    fi
  fi
fi
if test "$smp" = "yes"; then
  msmp="1"
fi
if test "$msmp" = "1"; then
  if test "$newkernel" = "1"; then
    GENKSYMS="$GENKSYMS -p smp_"
  fi
  c_opts="$c_opts -D__SMP__ -DCONFIG_SMP"
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi
AC_SUBST(msmp)

dnl Check for ISA PnP driver in kernel...
AC_MSG_CHECKING(for ISA PnP driver in kernel)
CONFIG_ISAPNP_KERNEL="0"
if test -r $CONFIG_SND_KERNELDIR/include/linux/isapnp.h; then
  AC_MSG_RESULT(yes)
  AC_DEFINE(CONFIG_ISAPNP_KERNEL)
  CONFIG_ISAPNP_KERNEL="1"
else
  AC_MSG_RESULT(no)  
fi
AC_SUBST(CONFIG_ISAPNP_KERNEL)

dnl Check for ISA PnP support...
AC_MSG_CHECKING(for ISA PnP support)
AC_ARG_WITH(isapnp,
  [  --with-isapnp=yes,no,auto  driver will (not) be compiled with ISA PnP support],
  pnp="$withval", pnp="auto")
CONFIG_ISAPNP="0"
if test "$pnp" = "auto"; then
  if test -f /proc/isapnp; then
    CONFIG_ISAPNP="1"
  fi
else
  if test "$pnp" = "yes"; then
    CONFIG_ISAPNP="1"
  fi
fi
AC_SUBST(CONFIG_ISAPNP)
if test "$CONFIG_ISAPNP" = "1"; then
  AC_DEFINE(CONFIG_ISAPNP)
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi

dnl Check for version...
AC_MSG_CHECKING(for driver version)
CONFIG_SND_VERSION=`cat $srcdir/version`
AC_DEFINE_UNQUOTED(CONFIG_SND_VERSION, "$CONFIG_SND_VERSION")
AC_SUBST(CONFIG_SND_VERSION)
AC_MSG_RESULT($CONFIG_SND_VERSION)

dnl Check for sequencer support...
AC_MSG_CHECKING(for sequencer support)
AC_ARG_WITH(sequencer,
  [  --with-sequencer=yes,no  driver will (not) be compiled with sequencer support],
  sequencer="$withval", sequencer="no")
CONFIG_SND_SEQUENCER="0"
if test "$sequencer" = "yes"; then
  CONFIG_SND_SEQUENCER="1"
fi
AC_SUBST(CONFIG_SND_SEQUENCER)
if test "$CONFIG_SND_SEQUENCER" = "1"; then
  AC_DEFINE(CONFIG_SND_SEQUENCER)
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi

dnl Check for OSS/Lite emulation...
AC_MSG_CHECKING(for OSS/Lite emulation)
AC_ARG_WITH(oss,
  [  --with-oss=no,yes       driver will (not) be compiled with OSS/Lite emulation],
  ossemul="$withval", ossemul="yes")
CONFIG_SND_OSSEMUL="0"
if test "$ossemul" = "yes"; then
  CONFIG_SND_OSSEMUL="1"
fi
AC_SUBST(CONFIG_SND_OSSEMUL)
if test "$CONFIG_SND_OSSEMUL" = "1"; then
  AC_DEFINE(CONFIG_SND_OSSEMUL)
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi

dnl Check for which cards to compile driver for...
ALSA_CARDS_INIT
ALSA_CARDS_SELECT

dnl Some late substitutes...
c_opts="$c_opts -DLINUX"
AC_SUBST(c_opts)
AC_SUBST(GENKSYMS)

dnl Output files...
AC_OUTPUT(Makefile.conf snddevices utils/alsa-driver.spec cards.config)

dnl Make right rights for scripts
chmod 755 $srcdir/snddevices
