#MIN_CONFIG(3.2)

### AM_INIT_AUTOMAKE(kttsd,0.2.0)

dnl ================================================================================

dnl Check for GStreamer >= 0.8.7

AC_ARG_WITH(gstreamer,
  [AC_HELP_STRING(--with-gstreamer,
    [enable support for GStreamer @<:@default=no@:>@])],
  [], with_gstreamer=no)

have_gst=no
if test "x$with_gstreamer" != xno; then
  # pkg-config seems to have a bug where it masks needed -L entries when it
  # shouldn't, so disable that.

  PKG_CONFIG_ALLOW_SYSTEM_LIBS=1
  export PKG_CONFIG_ALLOW_SYSTEM_LIBS

  dnl start with 0.8
  GST_MAJORMINOR=0.8
  dnl Actually need 0.8.7, but plugins were version 0.8.5.  argh!
  GST_REQ=0.8.5

  PKG_CHECK_MODULES(GST, \
    gstreamer-$GST_MAJORMINOR >= $GST_REQ \
    gstreamer-control-$GST_MAJORMINOR >= $GST_REQ \
    gstreamer-libs-$GST_MAJORMINOR >= $GST_REQ,
    have_gst=yes, have_gst=no)

  if test "x$with_gstreamer" != xcheck && test "x$have_gst" != xyes; then
    AC_MSG_ERROR([--with-gstreamer was given, but test for GStreamer >= 0.8.5 failed])
  fi
fi

if test "x$have_gst" = "xno"; then
  GST_CFLAGS=""
  LDADD_GST=""
  LDFLAGS_GST=""
  AC_DEFINE(HAVE_GSTREAMER, 0, [have GStreamer])
else
  LDADD_GST=`$PKG_CONFIG --libs-only-l gstreamer-$GST_MAJORMINOR`
  LDFLAGS_GST=`$PKG_CONFIG --libs-only-other gstreamer-$GST_MAJORMINOR`

  # Append -L entries, since they are masked by --libs-only-l and
  # --libs-only-other
  LIBDIRS_GST=`$PKG_CONFIG --libs-only-L gstreamer-$GST_MAJORMINOR`
  LDADD_GST="$LDADD_GST $LIBDIRS_GST"

  AC_MSG_NOTICE([GStreamer version >= $GST_REQ found.])
  AC_DEFINE(HAVE_GSTREAMER, 1, [have GStreamer])

  if test "x$with_gstreamer" != xcheck && test "x$have_gst" != xyes; then
    AC_MSG_ERROR([--with-gstreamer was given, but test for GStreamer >= 0.8.7 failed])
  fi
fi

AC_SUBST(GST_CFLAGS)
AC_SUBST(LDADD_GST)
AC_SUBST(LDFLAGS_GST)

AM_CONDITIONAL(include_kttsd_gstplayer, [test "x$have_gst" = "xyes"])

dnl ================================================================================

dnl Do not compile artsplayer plugin if user specifies --without-arts

AM_CONDITIONAL(include_kttsd_artsplayer, [test "x$build_arts" = "xyes"])

dnl ================================================================================

dnl Check for ALSA.
dnl TODO: Don't know if 0.5 works or not.

AC_DEFUN([KDE_CHECK_ALSA],
[
  have_alsa=no

  KDE_CHECK_HEADERS([sys/asoundlib.h alsa/asoundlib.h],
    [have_alsa=yes])

  KDE_CHECK_LIB(asound, snd_seq_create_simple_port,
    [:], [have_alsa=no])

  AC_LANG_SAVE
  AC_LANG_C
  if test "x$have_alsa" = 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
    ],
    have_alsa_0_5=yes)

    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
    ],
    have_alsa_0_9=yes)

    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)
        /* we have ALSA 1.x */
      #else
        #error not ALSA 1.x
      #endif
    ],
    have_alsa_1=yes)
  fi
  AC_LANG_RESTORE

  if test "x$have_alsa_0_9" = xyes || test "x$have_alsa_1" = xyes; then
    # for kmix/ and akode/
    LIBASOUND="-lasound"
    AC_DEFINE(HAVE_LIBASOUND2, 1, [Define if you have libasound.so.2 (required for ALSA 0.9.x/1.x support)])

    # for arts/
    ARTS_LIBASOUND="-lasound"
    AC_DEFINE(HAVE_ARTS_LIBASOUND2, 1, [Define if you have libasound.so.2 (required for ALSA 0.9.x/1.x support)])
  fi

  if test "x$have_alsa_0_5" = xyes; then
    # for arts/
    ARTS_LIBASOUND="-lasound"
    AC_DEFINE(HAVE_ARTS_LIBASOUND, 1, [Define if you have libasound.so.1 (required for ALSA 0.5.x support)])
  fi

  AC_SUBST(LIBASOUND)
  AC_SUBST(ARTS_LIBASOUND)
])

AC_ARG_WITH(alsa,
  [AC_HELP_STRING(--with-alsa,
    [enable support for ALSA @<:@default=check@:>@])],
  [], with_alsa=check)

have_alsa=no
if test "x$with_alsa" != xno; then
  KDE_CHECK_ALSA

  if test "x$with_alsa" != xcheck && test "x$have_alsa" != xyes; then
    AC_MSG_ERROR([--with-alsa was given, but test for ALSA failed])
  fi
fi

AM_CONDITIONAL(include_kttsd_alsaplayer, [test "x$have_alsa" = "xyes"])

dnl ================================================================================

dnl Check for aKode library.  Note: As of about 16 Jul 2005, it got moved
dnl from kdemultimedia to kdesupport.

AC_DEFUN([KDE_CHECK_AKODE],
[
  AC_PATH_PROG(AKODE_CONFIG, akode-config, [no], [$PATH:$prefix/bin])

  if test "x$AKODE_CONFIG" != xno; then
    AC_DEFINE(HAVE_AKODE, 1, [define if you have aKodelib installed])
    akode_includes=`$AKODE_CONFIG --cflags`
    akode_libs=`$AKODE_CONFIG --libs`
    have_akode=yes
  else
    akode_includes=""
    akode_libs=""
    have_akode=no
  fi

  AC_SUBST(akode_includes)
  AC_SUBST(akode_libs)
])

AC_ARG_WITH(akode,
    [AC_HELP_STRING([--with-akode],
        [enable the aKode decoder @<:@default=no@:>@])],
    [], with_akode=no)

have_akode=no
if test "x$with_akode" != xno; then
  KDE_CHECK_AKODE

  if test "x$with_akode" != xcheck && test "x$have_akode" != xyes; then
    AC_MSG_ERROR([--with-akode was given, but test for aKode failed])
  fi
fi

AM_CONDITIONAL(include_kttsd_akodeplayer, [test "x$have_akode" = "xyes"])

dnl ================================================================================

dnl Check whether to include sys/time.h and time.h, or just sys/time.h.
dnl Defines TIME_WITH_SYS_TIME and HAVE_SYS_TIME_H, which are used in
dnl alsaplayer.cpp.

AC_HEADER_TIME
AC_CHECK_HEADERS(sys/time.h)

dnl ================================================================================

dnl See if the latest kspeech.h is installed and if not, compile against
dnl kttsd/compat directory.

if test "$KTTS_KSPEECH_DIR" = ""; then
    KDE_CHECK_HEADER(kspeech.h, ktts_have_kspeech_h=yes, ktts_have_kspeech_h=no)
    have_latest_kspeech=no
    if test "x$ktts_have_kspeech_h" = xyes; then
        AC_MSG_CHECKING([whether installed kspeech.h is latest version])
        ktts_save_cppflags=$CPPFLAGS
        AC_LANG_SAVE
        CPPFLAGS="$all_includes $CPPFLAGS"
        AC_LANG_CPLUSPLUS
        AC_TRY_COMPILE(
            [#include <kspeech.h>],
            [
            if (4 == KSpeech::mtHtml);
            ],
            have_latest_kspeech=yes,
            have_latest_kspeech=no)
        AC_MSG_RESULT($have_latest_kspeech)
        CPPFLAGS=$ktts_save_cppflags
        AC_LANG_RESTORE
    fi
    if test "x$have_latest_kspeech" = xyes; then
        KTTS_KSPEECH_DIR='$(kde_includes)'
        KTTS_KSPEECH_INCLUDE=""
        KTTS_INTERFACES_DIR=""
    else
        KTTS_KSPEECH_DIR='$(top_srcdir)/kttsd/compat/interfaces/kspeech'
        KTTS_KSPEECH_INCLUDE='-I$(top_srcdir)/kttsd/compat/interfaces/kspeech'
        KTTS_INTERFACES_DIR="interfaces"
        AC_MSG_WARN([Latest kspeech.h not installed. Compiling using kttsd/compat directory.])
    fi

    AC_SUBST(KTTS_KSPEECH_DIR)
    AC_SUBST(KTTS_KSPEECH_INCLUDE)
    AC_SUBST(KTTS_INTERFACES_DIR)
fi

dnl ================================================================================

# --- Check for KDE < 3.5 ---
# If so, install icons.

AC_MSG_CHECKING([for KDE version])

AC_LANG_SAVE
AC_LANG_CPLUSPLUS
kdeversion_save_CXXFLAGS="$CXXFLAGS"
kdeversion_save_LIBS="$LIBS"
LIBS="$LIBS $X_EXTRA_LIBS"
CXXFLAGS="$CXXFLAGS $all_includes"

KTTS_ICONS_DIR="icons"

CXXFLAGS="$kdeversion_save_CXXFLAGS"
LIBS="$kdeversion_save_LIBS"
AC_LANG_RESTORE

if test "$KTTS_ICONS_DIR" = ""; then
    AC_MSG_RESULT([KDE 3.4.x or less])
else
    AC_MSG_RESULT([KDE 3.5 or later])
fi

AC_SUBST(KTTS_ICONS_DIR)
