dnl =======================================================
dnl FILE: configure.in.in
dnl =======================================================

# Original Author was Kalle@kde.org
# I lifted it in some mater. (Stephan Kulow)
# I used much code from Janos Farkas

dnl Process this file with autoconf to produce a configure script.
AC_INIT(acinclude.m4) dnl a source file from your sub dir

dnl This is so we can use kde-common
AC_CONFIG_AUX_DIR(admin)

dnl right now: install aRts in the same prefix as KDE3.x currently
KDE_SET_PREFIX_CORE

AC_CANONICAL_SYSTEM
AC_ARG_PROGRAM

dnl Version (we /might/ want to use libtool versioning, too)
ARTS_MAJOR_VERSION=1
ARTS_MINOR_VERSION=3
ARTS_MICRO_VERSION=2
ARTS_VERSION=$ARTS_MAJOR_VERSION.$ARTS_MINOR_VERSION.$ARTS_MICRO_VERSION

dnl Automake doc recommends to do this only here. (Janos)
AM_INIT_AUTOMAKE(arts, $ARTS_VERSION) dnl searches for some needed programs

AM_MAINTAINER_MODE

AC_PROG_INSTALL

dnl generate the config header
AM_CONFIG_HEADER(config.h) dnl at the distribution this done

dnl Checks for programs.
AC_CHECK_COMPILERS
AC_LIBLTDL_CONVENIENCE

AC_ENABLE_SHARED(yes)
AC_ENABLE_STATIC(no)
KDE_PROG_LIBTOOL

AC_LANG_SAVE
AC_LANG_C
KDE_LIB_LTDL
AC_LANG_RESTORE

# this checks for --with-extra-libs
KDE_CHECK_EXTRA_LIBS
KDE_MISC_TESTS
dnl KDE_CHECK_LIBDL
dnl AC_PROG_LIBTOOL
KDE_CHECK_QT_DIRECT

dnl for NLS support. Call them in this order!
dnl WITH_NLS is for the po files, GNU_GETTEXT for the sources
dnl AM_KDE_WITH_NLS
dnl AM_KDE_GNU_GETTEXT

dnl Checks for header files.
KDE_CHECK_STL
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h sys/cdefs.h fnmatch.h sysent.h strings.h sys/stat.h sys/select.h sys/socket.h linux/socket.h socketbits.h sigaction.h paths.h malloc.h monetary.h sys/param.h limits.h sys/mnttab.h mntent.h fstab.h)

KDE_CHECK_HEADERS(sys/soundcard.h)

dnl Checks for libraries.
AC_BASE_PATH_KDE([don't test]) dnl arts is a special case
AC_CHECK_LIB(compat, main, [LIBCOMPAT="-lcompat"]) dnl for FreeBSD
AC_CHECK_LIB(util, main, [LIBUTIL="-lutil"]) dnl for FreeBSD
AC_CHECK_FUNC(sem_init, , AC_CHECK_LIB(rt, sem_init, [LIBSEM="-lrt"]))

AM_CONDITIONAL(HAVE_LIBJPEG, test -n "$LIBJPEG" )
AM_CONDITIONAL(HAVE_LIBPNG, test -n "$LIBPNG" )
AM_CONDITIONAL(HAVE_LIBTIFF, test -n "$LIBTIFF")

# configure would do this very late. Too late for us!
test "x$prefix" = xNONE && prefix=$ac_default_prefix

AC_DEFINE_UNQUOTED(KDEDIR, "$prefix", [The prefix to use as fallback])

ac_cpp_safe=$ac_cpp
ac_cpp='$CXXCPP $CPPFLAGS $X_INCLUDES'
KDE_CHECK_HEADERS(X11/ICE/ICElib.h)
ac_cpp=$ac_cpp_safe

dnl Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_TIME
AC_C_BIGENDIAN

dnl check if the compiler has bool
AC_CHECK_BOOL
dnl AC_CHECK_GNU_EXTENSIONS

dnl Checks for library functions.
AC_CHECK_FUNCS(socket snprintf vsnprintf seteuid setegid random strfmon stpcpy mkstemp setmntent gettimeofday vasprintf)
AC_FUNC_VFORK
AC_CHECK_USLEEP
AC_CHECK_SETENV
AC_CHECK_GETHOSTNAME
AC_CHECK_RANDOM
AC_CHECK_S_ISSOCK
AC_CHECK_SOCKLEN_T

dnl output files
AC_SUBST(x_includes)
AC_SUBST(x_libraries)
AC_SUBST(LIBSOCKET)
AC_SUBST(LIBCOMPAT)
AC_SUBST(LIBUTIL)
AC_SUBST(LIBSEM)
AC_SUBST(LIBICE)

all_includes="$all_includes $INCLTDL"
AC_SUBST(all_includes)

AC_SUBST(EXTRA_SUBDIRS)

topdir=`cd $srcdir && pwd`
ac_configure_args="$ac_configure_args --with-auxdir=$topdir/admin"

CXXFLAGS="$CXXFLAGS $USE_RTTI"

dnl----------------------------------------------------------------------------
dnl aRts specific configure tests
dnl
dnl Check for libaudioio (which can be used to get solaris audio support)
AC_DEFUN([AC_CHECK_LIBAUDIOIO],
[
  ac_ldflags_save="$LDFLAGS"
  ac_CPPFLAGS_save="$CPPFLAGS"
  LDFLAGS="$all_libraries $LDFLAGS"
  CPPFLAGS="$CPPFLAGS $all_includes"
  arts_libaudioio_found=no
  AC_LANG_SAVE
  AC_LANG_C
  kde_have_libaudioio=no
  AC_CHECK_HEADER(libaudioio.h,
  [
    kde_have_libaudioio=yes
  ])
  if test "x$kde_have_libaudioio" = "xyes"; then
     AC_CHECK_LIB(audioio,AudioIOGetVersion,[
      dnl LDFLAGS in case it's in KDEDIR/lib
      LIBAUDIOIO="$LDFLAGS -laudioio"
      AC_DEFINE(HAVE_LIBAUDIOIO, 1,
        [Define if you have libaudioIO (required if you want to have libaudioio support)])
      arts_libaudioio_found=yes
    ])
  fi
  AC_SUBST(LIBAUDIOIO)
  AC_LANG_RESTORE
  CPPFLAGS="$ac_CPPFLAGS_save"
  LDFLAGS="$ac_ldflags_save"
])

dnl aRts specific configure tests
dnl
dnl Check for libaudio (which can be used to get network audio support)
AC_DEFUN([AC_CHECK_LIBAUDIONAS],
[
  ac_ldflags_save="$LDFLAGS"
  ac_CPPFLAGS_save="$CPPFLAGS"
  LDFLAGS="$all_libraries $LDFLAGS"
  CPPFLAGS="$CPPFLAGS $all_includes"

  AC_LANG_SAVE
  AC_LANG_C
  arts_libaudionas_include=no
  AC_CHECK_HEADER(audio/audiolib.h,
  [
    arts_libaudionas_include=yes
  ])
  if test "x$arts_libaudionas_include" = "xyes"; then
    AC_CHECK_LIB(audio,AuOpenServer,[
     dnl LDFLAGS in case it's in KDEDIR/lib
     LIBAUDIONAS="$LDFLAGS -laudio -lXt"
     AC_DEFINE(HAVE_LIBAUDIONAS, 1,
        [Define if you have libaudio (required if you want to have NAS support)])
   ])
  fi
  AC_SUBST(LIBAUDIONAS)
  AC_LANG_RESTORE
  CPPFLAGS="$ac_CPPFLAGS_save"
  LDFLAGS="$ac_ldflags_save"
])

dnl Check for libesd (for EsounD support)
AC_DEFUN([AC_CHECK_LIBESD],
[
  ac_ldflags_save="$LDFLAGS"
  ac_CPPFLAGS_save="$CPPFLAGS"
  LDFLAGS="$all_libraries $LDFLAGS"
  CPPFLAGS="$CPPFLAGS $all_includes"

  AC_LANG_SAVE
  AC_LANG_C
  arts_libaudionas_include=no
  AC_CHECK_HEADER(esd.h,
  [
    arts_libesd_include=yes
  ])
  if test "x$arts_libesd_include" = "xyes"; then
    AC_CHECK_LIB(esd,esd_open_sound,[
     dnl LDFLAGS in case it's in KDEDIR/lib
     LIBESD="$LDFLAGS -lesd"
     AC_DEFINE(HAVE_LIBESD, 1,
        [Define if you have libesd (required if you want EsounD support)])
   ])
  fi
  AC_SUBST(LIBESD)
  AC_LANG_RESTORE
  CPPFLAGS="$ac_CPPFLAGS_save"
  LDFLAGS="$ac_ldflags_save"
])

dnl libaudiofile is used for loading wave files
AC_DEFUN([AC_CHECK_LIBAUDIOFILE],
[
  ac_ldflags_save="$LDFLAGS"
  ac_CPPFLAGS_save="$CPPFLAGS"
  LDFLAGS="$all_libraries $LDFLAGS"
  CPPFLAGS="$CPPFLAGS $all_includes"
  arts_audiolib_found=no
  dnl WAV reading
  AC_LANG_SAVE
  AC_LANG_C
  kde_has_audio_lib=no
  AC_CHECK_HEADER(audiofile.h,
  [
     kde_has_audio_lib=yes
  ])
  if test "x$kde_has_audio_lib" = "xyes"; then
      KDE_CHECK_LIB(audiofile,afOpenFile,[
      dnl LDFLAGS in case it's in KDEDIR/lib
      LIBAUDIOFILE="$LDFLAGS -laudiofile"
      AC_DEFINE(HAVE_LIBAUDIOFILE, 1,
        [Define if you have libaudiofile (required for playing wavs with aRts)])
      arts_audiolib_found=yes
      ])
  fi
  AM_CONDITIONAL(HAVE_WAVSUPPORT,  test "x$arts_audiolib_found" = "xyes")
  AC_SUBST(LIBAUDIOFILE)
  AC_LANG_RESTORE
  CPPFLAGS="$ac_CPPFLAGS_save"
  LDFLAGS="$ac_ldflags_save"
])


dnl check if sgi libaudio is present
AC_DEFUN([AC_CHECK_SGILIBAUDIO],
[
  ac_ldflags_save="$LDFLAGS"
  ac_CPPFLAGS_save="$CPPFLAGS"
  LDFLAGS="$all_libraries $LDFLAGS"
  CPPFLAGS="$CPPFLAGS $all_includes"

  dnl SGI libaudio (required on IRIX)
  AC_LANG_SAVE
  AC_LANG_C
  arts_dmedia_include=no
  AC_CHECK_HEADER(dmedia/audio.h,
  [
     arts_dmedia_include=yes
  ])
  if test "x$arts_dmedia_include" = "xyes"; then
    KDE_CHECK_LIB(audio,alOpenPort,[
    SGILIBAUDIO="-laudio"
    AC_DEFINE(HAVE_SGILIBAUDIO, 1,
      [Define if you have libaudio (required for sound i/o on IRIX)])
    ])
  fi
  AC_SUBST(SGILIBAUDIO)
  AC_LANG_RESTORE
  CPPFLAGS="$ac_CPPFLAGS_save"
  LDFLAGS="$ac_ldflags_save"
])

dnl check if we want to build MAS support
AC_DEFUN([AC_CHECK_LIBMAS],
[
AC_PATH_PROG(MAS_CONFIG, mas-config, no)

if test "$MAS_CONFIG" != "no"; then
  AC_DEFINE(HAVE_LIBMAS, 1,
    [Define if you have libmas (required if you want MAS support)])
  MAS_CFLAGS="`$MAS_CONFIG --cflags`"
  tmp_mas_libs="`$MAS_CONFIG --libs` -lmas_jrtp"
  MAS_LDFLAGS="`for i in $tmp_mas_libs; do echo $i; done |grep ^-L`"
  MAS_LDFLAGS="$MAS_LDFLAGS `for i in $tmp_mas_libs; do echo $i; done |grep ^-L |sed s/^-L/-R/g`"
  LIBMAS="$MAS_LDFLAGS"
  for i in $tmp_mas_libs; do LIBMAS="$LIBMAS `echo $i |grep ^-l`"; done
fi
AC_SUBST(MAS_CFLAGS)
AC_SUBST(MAS_LDFLAGS)
AC_SUBST(LIBMAS)

])

dnl Posix realtime scheduling - this is helpful if you want arts to run with
dnl higher priority than everthing else

AC_DEFUN([ARTS_TRY_LINK_POSIX_REALTIME],
[
AC_TRY_LINK([
#include <sched.h>
],
[
sched_getscheduler(0);
],
arts_realtime_sched=yes,
arts_realtime_sched=no)
])

AC_DEFUN([AC_CHECK_REALTIME_SCHED],
[
AC_MSG_CHECKING([for posix realtime scheduling support])
save_LIBS="$LIBS"
LIBPOSIX4=""
ARTS_TRY_LINK_POSIX_REALTIME
if test "x$arts_realtime_sched" = "xno"; then
   LIBS="$LIBS -lposix4"
   ARTS_TRY_LINK_POSIX_REALTIME
   if test "x$arts_realtime_sched" = "xyes"; then
      LIBPOSIX4="-lposix4"
   fi
fi
LIBS="$save_LIBS"
AC_SUBST(LIBPOSIX4)
AC_MSG_RESULT($arts_realtime_sched)

if test "x$arts_realtime_sched" = "xyes"; then
  AC_DEFINE(HAVE_REALTIME_SCHED,1,
        [Define if your system supports realtime scheduling])
fi
])

dnl Type of the ioctl function test - after some tries, it seems that this
dnl not required for Linux vs. FreeBSD (for which this test was written), and
dnl that only the Linux documentation claims that it has an "int" as second
dnl argument. But maybe there will pop up systems for which the signature
dnl differs, later.

AC_DEFUN([AC_CHECK_IOCTL_TYPE],
[
  AC_MSG_CHECKING([ioctl type])
  AC_CACHE_VAL(ac_cv_ioctl_type,
  [
    AC_LANG_SAVE
    AC_LANG_C

    AC_TRY_COMPILE(
    [
      #include <unistd.h>
      #include <sys/ioctl.h>
	  int ioctl(int d, int request,...);
    ],
    [
    ],
	ac_cv_ioctl_type=1)
    AC_TRY_COMPILE(
    [
      #include <unistd.h>
      #include <sys/ioctl.h>
	  int ioctl(int d, unsigned long request,...);
    ],
    [
    ],
	ac_cv_ioctl_type=2)
    AC_TRY_COMPILE(
    [
      #include <unistd.h>
      #include <sys/ioctl.h>
	  int ioctl(int d, unsigned long int request,...);
    ],
    [
    ],
	ac_cv_ioctl_type=3)
     AC_LANG_RESTORE
  ])
  AC_MSG_RESULT($ac_cv_ioctl_type)
  

if test "$ac_cv_ioctl_type" = "1"; then
  AC_DEFINE(HAVE_IOCTL_INT_INT_DOTS,1,
	      [Define if ioctl is declared as int ioctl(int d, int request,...)])
fi
if test "$ac_cv_ioctl_type" = "2"; then
  AC_DEFINE(HAVE_IOCTL_INT_ULONG_DOTS,2,
	      [Define if ioctl is declared as int ioctl(int d, unsigned long request,...)])
fi
if test "$ac_cv_ioctl_type" = "3"; then
  AC_DEFINE(HAVE_IOCTL_INT_ULONGINT_DOTS,3,
	      [Define if ioctl is declared as int ioctl(int d, unsigned long int request,...)])
fi
])

dnl check whether we can do rouding using asm optimization

AC_DEFUN([AC_CHECK_X86_FLOAT_INT],
[
AC_MSG_CHECKING([for x86 float to int conversions])
AC_CACHE_VAL(ac_cv_x86_float_int,
[
AC_LANG_SAVE
AC_LANG_CPLUSPLUS
AC_TRY_LINK([
#if defined(__i386__)
static inline long QRound (float inval)
{
  long ret;
  __asm__ ("fistpl %0" : "=m" (ret) : "t" (inval) : "st");
  return ret;
}
#else
error
#endif
],
[
long i = QRound(41.93);  /* should be 42 */
],
ac_cv_x86_float_int=yes,
ac_cv_x86_float_int=no)
AC_LANG_RESTORE
])
AC_MSG_RESULT($ac_cv_x86_float_int)
if test "x$ac_cv_x86_float_int" = "xyes"; then
  AC_DEFINE(HAVE_X86_FLOAT_INT,1,
        [Define if you want to use optimized x86 float to int conversion code])
fi
])

dnl check if the assembler supports SSE instructions
AC_DEFUN([AC_CHECK_X86_SSE],
[
AC_MSG_CHECKING([for x86 SSE instructions])
AC_CACHE_VAL(ac_cv_x86_sse,
[
AC_TRY_COMPILE(,
[
#if defined(__GNUC__) && defined(__i386__)
__asm__("movups %xmm0, (%esp)");
#else
/* IRIX's cc treats #error as a warning, make sure it errors out here */
error Not gcc on x86
#endif
],
ac_cv_x86_sse=yes,
ac_cv_x86_sse=no)
])
AC_MSG_RESULT($ac_cv_x86_sse)
if test "x$ac_cv_x86_sse" = "xyes"; then
	AC_DEFINE(HAVE_X86_SSE,1,
		[Define if your assembler supports x86 SSE instructions])
fi
])

dnl check whether we should try to emulate stdio fopen/fclose via
dnl glibc facilities (this *really* depends on glibc internals)

AC_DEFUN([AC_CHECK_ARTSDSP_STDIO_EMU],
[
AC_MSG_CHECKING([for stdio emulation in artsdsp])
AC_CACHE_VAL(ac_cv_artsdsp_stdio_emu,
[
AC_LANG_SAVE
AC_LANG_C
AC_TRY_LINK([
#define _GNU_SOURCE
#include <stdio.h>
],
[
	cookie_io_functions_t fns = { 0, 0, 0, 0 };
	struct fd_cookie *fdc = 0;
	FILE *out = fopencookie (fdc,"w", fns);
	out->_fileno = 0;
],
ac_cv_artsdsp_stdio_emu=yes,
ac_cv_artsdsp_stdio_emu=no)
])
AC_LANG_RESTORE
AC_MSG_RESULT($ac_cv_artsdsp_stdio_emu)
if test "x$ac_cv_artsdsp_stdio_emu" = "xyes"; then
  AC_DEFINE(HAVE_ARTSDSP_STDIO_EMU,1,
	[Define if you want to use glibc facilities to emulate stdio accesses in artsdsp])
fi
])

dnl call the tests
AC_CHECK_LIBAUDIOIO
AC_CHECK_LIBAUDIONAS
AC_CHECK_LIBESD
AC_CHECK_LIBAUDIOFILE
AC_CHECK_SGILIBAUDIO
AC_CHECK_LIBMAS
AC_CHECK_REALTIME_SCHED
AC_CHECK_GETDOMAINNAME
AC_CHECK_IOCTL_TYPE
AC_CHECK_X86_FLOAT_INT
AC_CHECK_X86_SSE
AC_CHECK_ARTSDSP_STDIO_EMU
KDE_CHECK_THREADING

dnl somewhat more deeply nested template code than gcc understands by default
if test "$GXX" = "yes"; then
  KDE_CHECK_COMPILER_FLAG(ftemplate-depth-99,
  [
    CXXFLAGS="$CXXFLAGS -ftemplate-depth-99"
  ])
fi

dnl check for ALSA audio support
arts_with_alsa=yes
AC_ARG_WITH(alsa, [  --with-alsa             enable aRts ALSA support],
[arts_with_alsa=$withval])

if test "$arts_with_alsa" = "yes"; then
AC_DEFUN([AC_CHECK_LIBASOUND],
[
  ac_ldflags_save="$LDFLAGS"
  LDFLAGS="$all_libraries $LDFLAGS"
  kde_has_asoundlib=no

  AC_CHECK_HEADERS([ sys/asoundlib.h alsa/asoundlib.h ],
  [
    kde_has_asoundlib=yes
  ])

  dnl trial and error version check for ALSA 0.5.x / ALSA 0.9.x
  AC_LANG_SAVE
  AC_LANG_C
  if test "x$kde_has_asoundlib" = "xyes"; then
    AC_TRY_COMPILE([
      #include "confdefs.h"
      #ifdef HAVE_SYS_ASOUNDLIB_H
      #include <sys/asoundlib.h>
      #endif
      #ifdef HAVE_ALSA_ASOUNDLIB_H
      #include <alsa/asoundlib.h>
      #endif
    ],[
      #if (SND_LIB_MAJOR == 0) && (SND_LIB_MINOR == 9)
        /* we have ALSA 0.9.x */
      #else
        #error not ALSA 0.9.x
      #endif
    ],
    kde_has_alsa_0_9=yes,
    kde_has_alsa_0_9=no)
  fi

  if test "x$kde_has_asoundlib" = "xyes"; then
    AC_TRY_COMPILE([
      #include "confdefs.h"
      #ifdef HAVE_SYS_ASOUNDLIB_H
      #include <sys/asoundlib.h>
      #endif
      #ifdef HAVE_ALSA_ASOUNDLIB_H
      #include <alsa/asoundlib.h>
      #endif
    ],[
      #if (SND_LIB_MAJOR == 1) && (SND_LIB_MINOR == 0)
        /* we have ALSA 1.x */
      #else
        #error not ALSA 1.x
      #endif
    ],
    kde_has_alsa_1_0=yes,
    kde_has_alsa_1_0=no)
  fi

  if test "x$kde_has_asoundlib" = "xyes"; then
    AC_TRY_COMPILE([
      #include "confdefs.h"
      #ifdef HAVE_SYS_ASOUNDLIB_H
      #include <sys/asoundlib.h>
      #endif
      #ifdef HAVE_ALSA_ASOUNDLIB_H
      #include <alsa/asoundlib.h>
      #endif
    ],[
    #if (SND_LIB_MAJOR == 0) && (SND_LIB_MINOR == 5)
      /* we have ALSA 0.5.x */
    #else
      #error not ALSA 0.5.x
    #endif
    ],
    kde_has_alsa_0_5=yes,
    kde_has_alsa_0_5=no)
  fi
  AC_LANG_RESTORE

  if test "x$kde_has_asoundlib" = "xyes"; then
    AC_CHECK_LIB(asound,snd_seq_create_simple_port,[
      if test "x$kde_has_alsa_0_5" = "xyes"; then
        LIBASOUND="-lasound"
        AC_DEFINE(HAVE_LIBASOUND, 1,
        [Define if you have libasound.so.1 (required for ALSA 0.5.x support)])
      fi
      if test "x$kde_has_alsa_0_9" = "xyes"; then
        LIBASOUND="-lasound"
        AC_DEFINE(HAVE_LIBASOUND2, 1,
          [Define if you have libasound.so.2 (required for ALSA 0.9.x support)])
        AC_CHECK_LIB(asound,snd_pcm_resume,[
          AC_DEFINE(HAVE_SND_PCM_RESUME, 1,
            [Define if libasound has snd_pcm_resume()])])
      fi
      if test "x$kde_has_alsa_1_0" = "xyes"; then
        LIBASOUND="-lasound"
        AC_DEFINE(HAVE_LIBASOUND2, 1,
          [Define if you have libasound.so.2 (required for ALSA 0.9.x/1.x support)])
	AC_DEFINE(ALSA_PCM_OLD_SW_PARAMS_API, 1,
          [Define if you have alsa 1.x])
	AC_DEFINE(ALSA_PCM_OLD_HW_PARAMS_API, 1,
          [Define if you have alsa 1.x])
        AC_CHECK_LIB(asound,snd_pcm_resume,[
          AC_DEFINE(HAVE_SND_PCM_RESUME, 1,
            [Define if libasound has snd_pcm_resume()])])
      fi
    ])
  fi
  AC_SUBST(LIBASOUND)
  LDFLAGS="$ac_ldflags_save"
])
AC_CHECK_LIBASOUND
fi

AC_SUBST(LIBASOUND)

dnl check for IRIX audio support
AC_MSG_CHECKING([for IRIX])
if test `uname` = "IRIX" -o `uname` = "IRIX64" ; then
  AC_DEFINE(HAVE_IRIX, 1, [Define if you compile under IRIX])
  AC_MSG_RESULT("yes")
else
  AC_MSG_RESULT("no")
fi

dnl check wether we want to use libcsl style sound I/O
AC_ARG_ENABLE(csl,
[  --enable-csl      enable libcsl style sound I/O for aRts],
[arts_want_csl="$enableval"],[arts_want_csl="no"])

if test "$arts_want_csl" = "yes"; then
  LIBCSL="-lcsl"
  AC_DEFINE(HAVE_LIBCSL, 1,
        [Define if you have libcsl (required if you want to have CSL support)])
else
  LIBCSL=""
fi
AC_SUBST(LIBCSL)

dnl Check for pkg-config
AC_PATH_PROG(PKG_CONFIG, pkg-config, no)

if test "$PKG_CONFIG" = "no"; then
  AC_MSG_ERROR([
This package requires pkg-config.
])
fi

dnl Check for Glib-2.0
# GLIB_CFLAGS:  cflags for compiling glib dependant sources
# GLIB_LIBADD:  glib libraries (-l options)
# GLIB_LDFLAGS: flags containing path to glib libraries (-L options)

GLIB_PACKAGES="gmodule-2.0 gthread-2.0"
GLIB_VERSION="1.3.3"
AC_MSG_CHECKING(for GLib-2.0 (at least $GLIB_VERSION))

if $PKG_CONFIG --atleast-pkgconfig-version 0.15 ; then
   if $PKG_CONFIG --atleast-version $GLIB_VERSION $GLIB_PACKAGES >/dev/null 2>&1 ; then
       GLIB_CFLAGS="`$PKG_CONFIG --cflags $GLIB_PACKAGES`"
       GLIB_LIBADD="`$PKG_CONFIG --libs-only-l --libs-only-other $GLIB_PACKAGES`"
       GLIB_LDFLAGS="`$PKG_CONFIG --libs-only-L $GLIB_PACKAGES`"
       AC_MSG_RESULT(yes)
   fi  
else
   if $PKG_CONFIG --atleast-version $GLIB_VERSION $GLIB_PACKAGES >/dev/null 2>&1 ; then
       GLIB_CFLAGS="`$PKG_CONFIG --cflags $GLIB_PACKAGES`"
       GLIB_LIBADD="`$PKG_CONFIG --libs-only-l $GLIB_PACKAGES`"
       GLIB_LDFLAGS="`$PKG_CONFIG --libs-only-L $GLIB_PACKAGES`"
       AC_MSG_RESULT(yes)
       AC_MSG_WARN([you may need to run make LDFLAGS=-pthread to compile arts])
   fi
fi

if test -z "$GLIB_LIBADD"; then
	AC_MSG_RESULT(not installed)
	AC_ERROR([
	Please install glib-2.0 (see http://www.gtk.org).
	])
    DO_NOT_COMPILE="$DO_NOT_COMPILE gmcop"
fi

CPPFLAGS="$CPPFLAGS $GLIB_CFLAGS"
CFLAGS="$CFLAGS $GLIB_CFLAGS"

AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBADD)
AC_SUBST(GLIB_LDFLAGS)

AC_MSG_CHECKING(for Jack Audio Connection Kit)
if $PKG_CONFIG --atleast-version 0.90 jack >/dev/null 2>&1 ; then
  JACK_CFLAGS="`$PKG_CONFIG --cflags jack`"
  JACK_LIBADD="`$PKG_CONFIG --libs-only-l jack`"
  JACK_LDFLAGS="`$PKG_CONFIG --libs-only-L jack`"
  AC_MSG_RESULT(yes)
  AC_DEFINE(HAVE_LIBJACK, 1,
        [Define if you have libjack (required if you want Jack support)])
else
  AC_MSG_RESULT(not installed)
fi
AC_SUBST(JACK_CFLAGS)
AC_SUBST(JACK_LIBADD)
AC_SUBST(JACK_LDFLAGS)

dnl OpenBSD requires an -lossaudio and soundcard.h to provide OSS audio I/O
AC_CHECK_HEADERS(soundcard.h)
AC_CHECK_LIB(ossaudio, _oss_ioctl, [LIBOSSAUDIO="-lossaudio"])
AC_SUBST(LIBOSSAUDIO)

dnl Don't remove !
dnl AC_OUTPUT(artsc/artsc-config)

dnl Check if we are building as part of KDE. Arts can also be built as
dnl a standalone snapshot in which case KDE may not be present and things
dnl that depend on KDE should not be built.
dnl AC_MSG_CHECKING(if building standalone aRts snapshot without KDE)
dnl if test "$DCOPIDL" = ""; then
dnl	AC_MSG_RESULT(yes)
dnl	ARTS_BUILD_KDE=""
dnl	MOC="true"
dnl	DCOPIDL="true"
dnl	DCOPIDL2CPP="true"
dnl	AC_SUBST(MOC)
dnl	AC_SUBST(DCOPIDL)
dnl	AC_SUBST(DCOPIDL2CPP)
dnl else
dnl	AC_MSG_RESULT(no)
dnl	if test "x$kde_use_qt_emb" = "xyes"; then
dnl		ARTS_BUILD_KDE="qtmcop kde knotify message"
dnl	else
dnl		ARTS_BUILD_KDE="x11 qtmcop kde knotify message"
dnl	fi
dnl fi
dnl AC_SUBST(ARTS_BUILD_KDE)

MCOPIDL='$(top_builddir)/mcopidl/mcopidl'
AC_SUBST(MCOPIDL)

AC_SUBST(ARTS_MAJOR_VERSION)
AC_SUBST(ARTS_MINOR_VERSION)
AC_SUBST(ARTS_MICRO_VERSION)
AC_SUBST(ARTS_VERSION)

dnl Don't remove !
dnl AC_OUTPUT(artsc/artsdsp)
dnl AC_OUTPUT(soundserver/artsversion-new.h)
dnl AC_OUTPUT(flow/gsl/gslconfig.h)

dnl =======================================================
dnl FILE: ./flow/gsl/configure.in.in
dnl =======================================================

dnl Portability defines that help interoperate with classic and modern autoconfs
ifdef([AC_TR_SH],[
define([GLIB_TR_SH],[AC_TR_SH([$1])])
define([GLIB_TR_CPP],[AC_TR_CPP([$1])])
], [
define([GLIB_TR_SH],
       [patsubst(translit([[$1]], [*+], [pp]), [[^a-zA-Z0-9_]], [_])])
define([GLIB_TR_CPP],
       [patsubst(translit([[$1]],
                          [*abcdefghijklmnopqrstuvwxyz],
                          [PABCDEFGHIJKLMNOPQRSTUVWXYZ]),
                 [[^A-Z0-9_]], [_])])
])


dnl GLIB_SIZEOF (INCLUDES, TYPE, ALIAS [, CROSS-SIZE])
AC_DEFUN([GLIB_SIZEOF],
[pushdef([glib_Sizeof], GLIB_TR_SH([glib_cv_sizeof_$3]))dnl
AC_CACHE_CHECK([size of $2], glib_Sizeof,
[AC_TRY_RUN([#include <stdio.h>
#if STDC_HEADERS
#include <stdlib.h>
#include <stddef.h>
#endif
$1
main()
{
  FILE *f=fopen("conftestval", "w");
  if (!f) exit(1);
  fprintf(f, "%d\n", sizeof($2));
  exit(0);
}],
  [glib_Sizeof=`cat conftestval`  dnl''
],
  [glib_Sizeof=0],
  ifelse([$4], [], [], [glib_Sizeof=$4]))])
AC_DEFINE_UNQUOTED(GLIB_TR_CPP(glib_sizeof_$3), [$[]glib_Sizeof], [Size of $3])
popdef([glib_Sizeof])dnl
])

AC_DEFUN([AC_GSL_REQUIREMENTS],
[
    dnl === Ogg/Vorbis check ===
    AC_ARG_ENABLE(vorbis,
    [  --disable-arts-vorbis       disable arts Ogg Vorbis support],
    [arts_want_vorbis="$enableval"],[arts_want_vorbis="yes"])

    if test "$arts_want_vorbis" = "yes"; then
        ov_headers=0
        AC_CHECK_LIB(vorbisfile, ov_read_float,
          [AC_CHECK_HEADER(ogg/ogg.h, ov_headers=`expr $ov_headers + 1`)]
          [AC_CHECK_HEADER(vorbis/vorbisfile.h, ov_headers=`expr $ov_headers + 1`)]
          [AC_CHECK_HEADER(vorbis/vorbisenc.h, ov_headers=`expr $ov_headers + 1`)]
          ,
          ov_headers=0, -lvorbisenc -lvorbis -logg)
        if test $ov_headers = 3 ; then
          GSL_LIBS="$GSL_LIBS -lvorbisfile -lvorbisenc -lvorbis -logg"
          GSL_HAVE_OGGVORBIS=1
        else
          GSL_HAVE_OGGVORBIS=0
        fi
        dnl AC_DEFINE(GSL_HAVE_OGGVORBIS,1,[Define if you want Ogg Vorbis support unconditionally])
    else
      GSL_HAVE_OGGVORBIS=0
      dnl AC_MSG_WARN([failed to detect Ogg/Vorbis library (>=1.0rc3) or headers])
    fi
    AC_SUBST(GSL_HAVE_OGGVORBIS)

    dnl check for ov_read_float parameter count (differs in 1.0 vs. rc3)
    if test $GSL_HAVE_OGGVORBIS = 1; then
      AC_MSG_CHECKING([for ov_read_float() from rc3])
      AC_TRY_COMPILE([
        #include <vorbis/vorbisfile.h>
      ], [
        ov_read_float(0,0,0);
      ], [
        GSL_HAVE_OGGVORBIS_RC3=1
        AC_MSG_RESULT(yes)
      ], [
        GSL_HAVE_OGGVORBIS_RC3=0
        AC_MSG_RESULT(no)
      ])
      AC_SUBST(GSL_HAVE_OGGVORBIS_RC3)
    fi

    dnl === libmad MPEG decoder check ===
    AC_ARG_ENABLE(libmad,
    [  --disable-libmad       disable arts MAD mp3 decoder support],
    [arts_want_libmad="$enableval"],[arts_want_libmad="yes"])

    if test "$arts_want_libmad" = "yes"; then
        mad_detect=0
        AC_CHECK_LIB(mad, mad_synth_frame,
          [AC_CHECK_HEADER(mad.h, mad_detect=1)]
          ,
          mad_detect=0, -lmad)
        if test $mad_detect = 1 ; then
          GSL_LIBS="$GSL_LIBS -lmad"
          GSL_HAVE_LIBMAD=1
        else
          dnl AC_MSG_WARN([failed to detect libmad (MPEG I-III audio decoder) or headers])
          GSL_HAVE_LIBMAD=0
        fi
    else
      dnl AC_MSG_WARN([failed to detect libmad (MPEG I-III audio decoder) or headers])
      GSL_HAVE_LIBMAD=0
    fi
    AC_SUBST(GSL_HAVE_LIBMAD)

    dnl === Check library requirements ===
    AC_MSG_CHECKING([the required GSL-Loader library set])
    if true; then dnl echo $GSL_LIBS | grep ".*-lvorbis.*" >/dev/null ; then
      AC_MSG_RESULT([complete])
    else
      AC_MSG_RESULT([])
      AC_MSG_ERROR([Ogg/Vorbis is missing, but required])
    fi
    AC_SUBST(GSL_LIBS)

    dnl === sizeof types ===
    GLIB_SIZEOF([#include <pthread.h>], pthread_mutex_t, pth_mutex_t)
    GSL_SIZEOF_PTH_MUTEX_T="$glib_cv_sizeof_pth_mutex_t"
    AC_SUBST(GSL_SIZEOF_PTH_MUTEX_T)
    GLIB_SIZEOF([#include <pthread.h>], pthread_cond_t, pth_cond_t)
    GSL_SIZEOF_PTH_COND_T="$glib_cv_sizeof_pth_cond_t"
    AC_SUBST(GSL_SIZEOF_PTH_COND_T)
    GLIB_SIZEOF([#include <inttypes.h>], intmax_t, std_intmax_t)
    GSL_SIZEOF_STD_INTMAX_T="$glib_cv_sizeof_std_intmax_t"
    AC_SUBST(GSL_SIZEOF_STD_INTMAX_T)

    dnl === pthread_mutexattr_settype ===
    AC_MSG_CHECKING([for pthread_mutexattr_settype()])
    AC_TRY_COMPILE([
      #define _XOPEN_SOURCE   500
      #include <pthread.h>
    ], [
      int (*attr_settype) (pthread_mutexattr_t *__attr, int __kind) = pthread_mutexattr_settype;
      int val = PTHREAD_MUTEX_RECURSIVE;
      attr_settype = 0; val = 0;
    ],
      GSL_HAVE_MUTEXATTR_SETTYPE=1
      AC_MSG_RESULT(yes)
    ,
      GSL_HAVE_MUTEXATTR_SETTYPE=0
      AC_MSG_RESULT(no)
    )
    AC_SUBST(GSL_HAVE_MUTEXATTR_SETTYPE)
])

AC_DEFUN([AC_GSL_GLIB_REQUIREMENTS],
[
	AC_CHECK_FUNCS(getcwd)
	AC_FUNC_ALLOCA

	if test "x${ac_cv_working_alloca_h+set}" = xset ; then
		glib_header_alloca_h="$ac_cv_working_alloca_h"
	else
		glib_header_alloca_h="$ac_cv_header_alloca_h"
	fi
    if test "x$glib_header_alloca_h" = "xyes"; then
		AC_DEFINE(GLIB_HAVE_ALLOCA_H, 1, [Define if you have alloca.h])
    fi

dnl **********************
dnl *** va_copy checks ***
dnl **********************
dnl we currently check for all three va_copy possibilities, so we get
dnl all results in config.log for bug reports.
AC_CACHE_CHECK([for an implementation of va_copy()],glib_cv_va_copy,[
	AC_TRY_RUN([
	#include <stdarg.h>
	void f (int i, ...) {
	va_list args1, args2;
	va_start (args1, i);
	va_copy (args2, args1);
	if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
	  exit (1);
	va_end (args1); va_end (args2);
	}
	int main() {
	  f (0, 42);
	  return 0;
	}],
	[glib_cv_va_copy=yes],
	[glib_cv_va_copy=no],
	[])
])
AC_CACHE_CHECK([for an implementation of __va_copy()],glib_cv___va_copy,[
	AC_TRY_RUN([
	#include <stdarg.h>
	void f (int i, ...) {
	va_list args1, args2;
	va_start (args1, i);
	__va_copy (args2, args1);
	if (va_arg (args2, int) != 42 || va_arg (args1, int) != 42)
	  exit (1);
	va_end (args1); va_end (args2);
	}
	int main() {
	  f (0, 42);
	  return 0;
	}],
	[glib_cv___va_copy=yes],
	[glib_cv___va_copy=no],
	[])
])

if test "x$glib_cv_va_copy" = "xyes"; then
  g_va_copy_func=va_copy
else if test "x$glib_cv___va_copy" = "xyes"; then
  g_va_copy_func=__va_copy
fi
fi
])

AC_GSL_REQUIREMENTS
AC_GSL_GLIB_REQUIREMENTS

dnl get rid of lots and lots of useless warnings

GSL_CFLAGS="$CFLAGS"
for warn in -W -Wshadow -pedantic # -Wwrite-strings
do
	GSL_CFLAGS="`echo $GSL_CFLAGS | sed 's/ '$warn' / /g'`"
done
AC_SUBST(GSL_CFLAGS)

dnl enable aRts version of glib / thread support

GSL_USE_GSL_GLIB=1
GSL_USE_ARTS_THREADS=1
AC_SUBST(GSL_USE_GSL_GLIB)
AC_SUBST(GSL_USE_ARTS_THREADS)
KDE_CREATE_SUBDIRSLIST
AM_CONDITIONAL(libltdl_SUBDIR_included, test "x$libltdl_SUBDIR_included" = xyes)
AM_CONDITIONAL(mcop_SUBDIR_included, test "x$mcop_SUBDIR_included" = xyes)
AM_CONDITIONAL(mcopidl_SUBDIR_included, test "x$mcopidl_SUBDIR_included" = xyes)
AM_CONDITIONAL(flow_SUBDIR_included, test "x$flow_SUBDIR_included" = xyes)
AM_CONDITIONAL(mcop_mt_SUBDIR_included, test "x$mcop_mt_SUBDIR_included" = xyes)
AM_CONDITIONAL(soundserver_SUBDIR_included, test "x$soundserver_SUBDIR_included" = xyes)
AM_CONDITIONAL(artsc_SUBDIR_included, test "x$artsc_SUBDIR_included" = xyes)
AM_CONDITIONAL(examples_SUBDIR_included, test "x$examples_SUBDIR_included" = xyes)
AM_CONDITIONAL(tests_SUBDIR_included, test "x$tests_SUBDIR_included" = xyes)
AM_CONDITIONAL(doc_SUBDIR_included, test "x$doc_SUBDIR_included" = xyes)
AM_CONDITIONAL(gmcop_SUBDIR_included, test "x$gmcop_SUBDIR_included" = xyes)
AM_CONDITIONAL(qtmcop_SUBDIR_included, test "x$qtmcop_SUBDIR_included" = xyes)
AM_CONDITIONAL(x11_SUBDIR_included, test "x$x11_SUBDIR_included" = xyes)
AC_CONFIG_FILES([ Makefile ])
AC_CONFIG_FILES([ artsc/Makefile ])
AC_CONFIG_FILES([ doc/Makefile ])
AC_CONFIG_FILES([ examples/Makefile ])
AC_CONFIG_FILES([ flow/Makefile ])
AC_CONFIG_FILES([ flow/gsl/Makefile ])
AC_CONFIG_FILES([ flow/gslpp/Makefile ])
AC_CONFIG_FILES([ flow/mcopclass/Makefile ])
AC_CONFIG_FILES([ gmcop/Makefile ])
AC_CONFIG_FILES([ libltdl/Makefile ])
AC_CONFIG_FILES([ mcop/Makefile ])
AC_CONFIG_FILES([ mcop_mt/Makefile ])
AC_CONFIG_FILES([ mcopidl/Makefile ])
AC_CONFIG_FILES([ qtmcop/Makefile ])
AC_CONFIG_FILES([ soundserver/Makefile ])
AC_CONFIG_FILES([ tests/Makefile ])
AC_CONFIG_FILES([ x11/Makefile ])
AC_CONFIG_FILES([ artsc/artsc-config ])
AC_CONFIG_FILES([ artsc/artsdsp ])
AC_CONFIG_FILES([ soundserver/artsversion-new.h ])
AC_CONFIG_FILES([ flow/gsl/gslconfig.h ])
AC_OUTPUT
dnl put here things which have to be done as very last part of configure

if test "x$arts_audiolib_found" = "xno"; then
  echo ""
  echo "You're missing libaudiofile. aRts won't be able to load or play"
  echo "any samples without it, so please install it."
  echo "Have a look at http://www.68k.org/~michael/audiofile/ or find a"
  echo "binary package for your platform."
  echo ""
  all_tests=bad
fi
# Check if KDE_SET_PREFIX was called, and --prefix was passed to configure
if test -n "$kde_libs_prefix" -a -n "$given_prefix"; then
  # And if so, warn when they don't match
  if test "$kde_libs_prefix" != "$given_prefix"; then
    # And if kde doesn't know about the prefix yet
    echo ":"`kde-config --path exe`":" | grep ":$given_prefix/bin/:" 2>&1 >/dev/null
    if test $? -ne 0; then
      echo ""
      echo "Warning: you chose to install this package in $given_prefix,"
      echo "but KDE was found in $kde_libs_prefix."
      echo "For this to work, you will need to tell KDE about the new prefix, by ensuring"
      echo "that KDEDIRS contains it, e.g. export KDEDIRS=$given_prefix:$kde_libs_prefix"
      echo "Then restart KDE."
      echo ""
    fi
  fi
fi

if test "$all_tests" = "bad"; then
  if test ! "$cache_file" = "/dev/null"; then
    echo ""
    echo "Please remove the file $cache_file after changing your setup"
    echo "so that configure will find the changes next time."
    echo ""
  fi
else
  echo ""
  echo "Good - your configure finished. Start make now"
  echo ""
fi
