dnl Initialize the autoconf settings

AC_INIT(esd.h)

AM_CONFIG_HEADER(config.h)

ESD_MAJOR_VERSION=0
ESD_MINOR_VERSION=2
ESD_MICRO_VERSION=29
ESD_VERSION=$ESD_MAJOR_VERSION.$ESD_MINOR_VERSION.$ESD_MICRO_VERSION

dnl set version info for libesd.so to package rev - $MAJOR + $MINOR : $MICRO : $MINOR
ESD_VERSION_INFO=`expr $ESD_MINOR_VERSION + $ESD_MAJOR_VERSION`:$ESD_MICRO_VERSION:$ESD_MINOR_VERSION

AC_SUBST(ESD_MAJOR_VERSION)
AC_SUBST(ESD_MINOR_VERSION)
AC_SUBST(ESD_MICRO_VERSION)
AC_SUBST(ESD_VERSION)
AC_SUBST(ESD_VERSION_INFO)

AM_INIT_AUTOMAKE(esound, $ESD_VERSION)

AM_MAINTAINER_MODE

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_ESDDSP_STDIO_EMU],
[
AC_MSG_CHECKING([for stdio emulation in esddsp])
AC_CACHE_VAL(ac_cv_esddsp_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_esddsp_stdio_emu=yes,
ac_cv_esddsp_stdio_emu=no)
])
AC_LANG_RESTORE
AC_MSG_RESULT($ac_cv_esddsp_stdio_emu)
if test "x$ac_cv_esddsp_stdio_emu" = "xyes"; then
  AC_DEFINE(ESDDSP_STDIO_EMU,1,
	[Define if you want to use glibc facilities to emulate stdio accesses in esddsp])
fi
])

AC_PROG_CC
AC_PROG_CPP
AM_PROG_LIBTOOL


dnl Check for tools

AC_CANONICAL_HOST
AC_PROG_MAKE_SET

AC_PROG_INSTALL

dnl Check for compiler environment

AC_C_CONST
AC_C_BIGENDIAN
AC_C_INLINE

dnl Check for system libs needed

AC_FUNC_ALLOCA
AC_CHECK_FUNCS(setenv putenv fchown fchmod gethostbyname2)
AC_CHECK_FUNC(connect,,[AC_CHECK_LIB(socket,connect)])
AC_CHECK_FUNC(gethostbyname,,[AC_CHECK_LIB(nsl,gethostbyname)])
AC_CHECK_FUNC(nanosleep,,[
  AC_CHECK_LIB(rt,nanosleep,,[AC_CHECK_LIB(posix4,nanosleep)])])
AC_CHECK_FUNCS(usleep)
AC_CHECK_FUNC(inet_aton,,[AC_CHECK_LIB(resolv,inet_aton)])
AC_CHECK_FUNCS(strtok_r)

case $ac_cv_func_nanosleep/$ac_cv_lib_rt_nanosleep/$ac_cv_lib_posix4_nanosleep in
no/no/no) ;;
*) AC_DEFINE(HAVE_NANOSLEEP)
esac

case $ac_cv_func_inet_aton/$ac_cv_lib_resolv_inet_aton in 
no/no) ;;
*) AC_DEFINE(HAVE_INET_ATON)
esac

dnl Check if INADDR_LOOPBACK exists
  
AC_TRY_COMPILE([
    #include<sys/types.h>
    #include<netinet/in.h>],
    [unsigned long ul = INADDR_LOOPBACK;],,
    [AC_DEFINE(INADDR_LOOPBACK, (u_int32_t)0x7f000001)])
    

dnl Check if SUN_LEN exists

AC_TRY_LINK([
    #include<sys/types.h>
    #include<sys/un.h>], 
    [struct sockaddr_un su; int i = SUN_LEN(&su);],
    [AC_DEFINE(HAVE_SUN_LEN)], )

dnl check for getopt_long, substitute the distributed versions if not

AC_CHECK_FUNC(getopt_long,[have_getopt_long=yes],[have_getopt_long=no])
AM_CONDITIONAL(NEED_GETOPT, test "x$have_getopt_long" = xno)

dnl Needed for FIONBIO on Solaris at least

AC_CHECK_HEADERS(sys/filio.h sys/ioctl.h)

dnl see if we want to enable insanely verbose debugging

AC_ARG_ENABLE(debugging,[  --enable-debugging      enable verbose diagnostic info [default=no]], , enable_debugging=no )

if test "x$enable_debugging" = "xyes"; then
  AC_DEFINE(ESDBG)
fi

dnl see if we intentionally want to disable local sound

AC_ARG_ENABLE(local_sound,[  --enable-local-sound    turn on local sound playing [default=yes]], , enable_local_sound=yes)

if test "x$enable_local_sound" = "xyes"; then
   found_sound=no

   dnl Check for audio header files needed

   echo "---------------------------------------------------------------------"
   echo "--- Checking to see which audio header files your system uses.";
   echo "--- Most of these checks should fail. Do not be alarmed.";
   AC_CHECK_HEADERS(soundcard.h sys/soundcard.h machine/soundcard.h sys/audio.h)
   AC_CHECK_HEADERS(sys/audioio.h sys/audio.io.h sun/audioio.h)
   AC_CHECK_HEADERS(dmedia/audio.h sys/soundlib.h sys/asoundlib.h alsa/asoundlib.h)
   # mme_api.h directly includes other files from the mme subdir
   xCPPFLAGS="$CPPFLAGS"
   CPPFLAGS="$CPPFLAGS -I/usr/include/mme"
   AC_CHECK_HEADERS(mme/mme_api.h)
   CPPFLAGS="$xCPPFLAGS"

   dnl Define the driver needed based on the first header file found

   if test "${ac_cv_header_sys_soundcard_h}" = "yes" || \
      test "${ac_cv_header_soundcard_h}" = "yes" || \
      test "${ac_cv_header_machine_soundcard_h}" = "yes"; then

      dnl Platform mklinux/powerpc needs special care and feeding
      found_sound=yes
      AC_DEFINE(DRIVER_OSS)
   fi

   if test "${ac_cv_header_sys_audio_h}" = "yes"; then
      case ${host_os} in
      hpux*)
          found_sound=yes
          AC_DEFINE(DRIVER_HPUX)
      ;;
      aix*)
          found_sound=yes
          AC_DEFINE(DRIVER_AIX)
      esac
   fi

   if test "${ac_cv_header_sys_audioio_h}" = "yes" || \
      test "${ac_cv_header_sys_audio_io_h}" = "yes" || \
      test "${ac_cv_header_sun_audioio_h}" = "yes"; then
      case ${host_os} in
      solaris*)
         found_sound=yes
         AC_DEFINE(DRIVER_SOLARIS)
         ;;
      esac
   fi

   case ${host_os} in
   os2*)
      found_sound=yes
      AC_DEFINE(DRIVER_DART)
      ;;
   esac

   if test "${ac_cv_header_dmedia_audio_h}" = "yes"; then
      case ${host_os} in
      irix*)
         found_sound=yes
         AC_DEFINE(DRIVER_IRIX)
      esac
   fi

   if test "${ac_cv_header_mme_mme_api_h}" = "yes"; then
      case ${host_os} in
      osf*)
	found_sound=yes
	AC_DEFINE(DRIVER_OSF)
	CPPFLAGS="$CPPFLAGS -I/usr/include/mme"
	SOUND_LIBS=-lmme
      esac
   fi

   AC_ARG_ENABLE(alsa,[  --enable-alsa           use alsa if available [default=yes]], , enable_alsa=yes)

   
   if test "x$enable_alsa" = "xyes"; then
      if test "${ac_cv_header_sys_soundlib_h}" = "yes"; then
         found_sound=yes
         AC_DEFINE(DRIVER_ALSA)

      elif test "${ac_cv_header_alsa_asoundlib_h}" = "yes"; then
         found_sound=yes
         AC_DEFINE(DRIVER_ALSA_09)

      elif test "${ac_cv_header_sys_asoundlib_h}" = "yes"; then
         found_sound=yes
         AC_DEFINE(DRIVER_NEWALSA)
      fi
   fi

   if test "$found_sound" = "no"; then
      AC_MSG_ERROR([Could not find a support sound driver])
   fi

   dnl Check for additional audio libs needed

   echo "---------------------------------------------------------------------"
   echo "--- Checking to see which audio libraries are required for linking.";
   echo "--- Most of these checks should also fail. Do not be alarmed.";
   AC_CHECK_FUNC(_oss_ioctl,,[AC_CHECK_LIB(ossaudio,_oss_ioctl)])
   AC_CHECK_FUNC(ALnewconfig,,[AC_CHECK_LIB(audio,ALnewconfig)])
   if test "x$enable_alsa" = "xyes"; then
      AC_CHECK_FUNC(snd_cards,,[AC_CHECK_LIB(sound,snd_cards)])
      AC_CHECK_FUNC(snd_cards,,[AC_CHECK_LIB(asound,snd_cards)])
      AC_CHECK_FUNC(snd_pcm_pause,,[AC_CHECK_LIB(asound,snd_pcm_pause)])
   fi
else
   AC_DEFINE(DRIVER_NONE)
fi

dnl offer daemon in serverdir

AC_ARG_WITH(esd-dir, [  --with-esd-dir=PATH     specify location of esound daemon ])

if test "x$with_esd_dir" = "x" ; then
  ESD_DIR='$(prefix)/bin'
  ESD_PC_DIR='${prefix}/bin'
else
  ESD_DIR=$with_esd_dir
  ESD_PC_DIR=$with_esd_dir
fi
AC_SUBST(ESD_DIR)
AC_SUBST(ESD_PC_DIR)

dnl make sure we have the library, -laudiofile

build_esdplay=false
AC_ARG_WITH(audiofile,
[  --with-audiofile        include support for audiofile related utilities and functions
                              ARG = { yes | no | check } [default=check]])

if test xno != x$with_audiofile ; then
echo "---------------------------------------------------------------------"
echo "--- Checking for the audiofile library.  NOTE: This library is now"
echo "--- REQUIRED from proper compilation of the esound package."

AM_PATH_AUDIOFILE(0.1.5, [

build_esdplay=true
echo "--- libaudiofile found. Building esound with audiofile support"
],[

echo "--- ERROR: The audiofile library was not found on your system."
echo "--- The audiofile library may be obtained from your favourite"
echo "--- gnome distribution center, or check the audiofile web page at:"
echo "--- http://www.68k.org/~michael/audiofile/"
echo "--- NOTE: The audiofile library is now REQUIRED for esound."

])

else

   echo "---------------------------------------------------------------------"
   echo "--- WARNING: Per configuration request, the audiofile library support"
   echo "--- not be included.  This may result in errors using some programs"
   echo "--- designed to operate with esd."
   echo "--- The audiofile library may be obtained from your favourite"
   echo "--- gnome distribution center, or check the audiofile web page at:"
   echo "--- http://www.68k.org/~michael/audiofile/"
   echo "--- NOTE: The audiofile library is now REQUIRED for esound."

fi

AM_CONDITIONAL(BUILD_ESDPLAY, $build_esdplay)

echo "---------------------------------------------------------------------"
echo "--- Checking for the documentation generation tools.  NOTE: These "
echo "--- are NOT required for proper compilation of the esound package."

DOCS=""
DISTDOCS=""
DB2HTML=true
AC_CHECK_PROG(have_db2html, db2html, yes, no)
if test "x$have_db2html" = "xyes"; then
   DOCS=esound/index.html
   DISTDOCS=docs/html
   DB2HTML=db2html
fi

DB2PS=true
AC_CHECK_PROG(have_db2ps, db2ps, yes, no)
if test "x$have_db2ps" = "xyes"; then
   DOCS="$DOCS esound.ps"
   DISTDOCS="$DISTDOCS docs/esound.ps"
   DB2PS=db2ps
fi

echo "---------------------------------------------------------------------"

AC_ARG_WITH(libwrap,[  --with-libwrap          use tcp wrappers [default=no]], , wrap_ok=no)

WRAP_LIB=
if test "x$with_libwrap" = "xyes"; then
   esound_save_LIBS="$LIBS"
   LIBS="$LIBS -lwrap"

   AC_MSG_CHECKING([for tcp_wrapper library and headers])

   wrap_ok=no
   AC_TRY_LINK(
[#include <tcpd.h>
#include <syslog.h>
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;],
[struct request_info *request; return hosts_access (request);],
      [AC_DEFINE(USE_LIBWRAP)
       WRAP_LIB="-lwrap"
       wrap_ok=yes],
      [LIBS="$LIBS -lnsl"
       AC_TRY_LINK(
[#include <tcpd.h>
#include <syslog.h>
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;],
[struct request_info *request; return hosts_access (request);],

         [AC_DEFINE(USE_LIBWRAP)
          WRAP_LIB="-lwrap -lnsl"
          wrap_ok=yes])])

   AC_MSG_RESULT($wrap_ok)

   LIBS="$esound_save_LIBS"
fi

dnl build the esddsp wrapper for systems, 
dnl that support LD_PRELOAD and RTLD_NEXT only
dsp_ok=no DL_LIB=

AC_MSG_CHECKING([if your platform supports esddsp])
case "$host_os" in
  linux* | freebsd* | bsdi4* )
    dsp_ok=yes
    ;;
esac
AC_MSG_RESULT($dsp_ok)

if test "x$dsp_ok" = xyes; then
   AC_CHECK_FUNC(dlopen, dsp_ok=yes,
      AC_CHECK_LIB(dl, dlopen, dsp_ok=yes DL_LIB=-ldl))
   ESDDSP=esddsp
   LIBESDDSP=libesddsp.la
   AC_CHECK_ESDDSP_STDIO_EMU
fi
AM_CONDITIONAL(BUILD_ESDDSP, test "x$dsp_ok" = xyes)

# set | sort; # just for double checking the script...

AC_SUBST(WRAP_LIB)
AC_SUBST(SOUND_LIBS)
AC_SUBST(AUDIOFILE_LIBS)
AC_SUBST(DL_LIB)
AC_SUBST(DOCS)
AC_SUBST(DISTDOCS)
AC_SUBST(DB2HTML)
AC_SUBST(DB2PS)

AC_OUTPUT([
Makefile
esd-config
esound.spec
esound.pc
esddsp
docs/Makefile
docs/esd.1
docs/esdcat.1
docs/esd-config.1
docs/esdctl.1
docs/esddsp.1
docs/esdfilt.1
docs/esdloop.1
docs/esdmon.1
docs/esdplay.1
docs/esdrec.1
docs/esdsample.1
])
dnl , [case "$CONFIG_FILES" in *esd-config*) chmod +x esd-config;; esac])

echo ""
echo "Esound Configure Settings:"
echo ""
echo "Debugging support: $enable_debugging" 
echo "libwrap support:   $wrap_ok"
echo ""
echo "LIBS=$LIBS"
echo ""

