#MIN_CONFIG(3)

AM_INIT_AUTOMAKE(kmplayer,0.10.0c)

KDE_ENABLE_HIDDEN_VISIBILITY

AM_MAINTAINER_MODE

AC_ARG_VAR(PKGCONFIGFOUND, [Path to pkg-config])
AC_ARG_VAR(NSPRCONFIGFOUND, [Path to nspr-config])
AC_CHECK_PROG(PKGCONFIGFOUND, pkg-config,[yes])

AC_MSG_CHECKING(if koffice-plugin support is wanted)
AC_ARG_ENABLE(koffice-plugin,
[  --enable-koffice-plugin enable koffice plugin],
[if test "$enableval" = yes; then
        AC_MSG_RESULT(yes)
        want_koffice_plugin="yes"
else
        AC_MSG_RESULT(no)
fi], [AC_MSG_RESULT(no); want_koffice_plugin="no"])

KDE_CHECK_HEADER(koDocument.h,
  have_koffice=yes,
  have_koffice=no)

AC_MSG_CHECKING([if kmplayer can be compiled with koffice support])
AC_MSG_RESULT($have_koffice)

AM_CONDITIONAL(include_koffice_support, test "$want_koffice_plugin" = "yes" -a "$have_koffice" = "yes")
if test "$want_koffice_plugin" = "yes" -a "$have_koffice" = "yes"; then
        AC_DEFINE(HAVE_KOFFICE, 1, [If we have koffice installed])
        LIB_KOFFICE="-lkofficecore -lkofficeui"
        AC_SUBST(LIB_KOFFICE)
fi

AC_MSG_CHECKING(if expat XML parsing is wanted)
AC_ARG_ENABLE(expat,
[  --enable-expat use expat libs],
[if test "$enableval" = yes; then
        AC_MSG_RESULT(yes)
        want_expat="yes"
else
        AC_MSG_RESULT(no)
fi], [AC_MSG_RESULT(no); want_expat="no"])

KDE_CHECK_HEADER(expat.h,
  have_expat=yes,
  have_expat=no)

AC_MSG_CHECKING([if kmplayer can use expat its XML parser])
AC_MSG_RESULT($have_expat)

AM_CONDITIONAL(include_expat_support, test "$want_expat" = "yes" -a "$have_expat" = "yes")
if test x$have_expat = xyes -a x$want_expat = xyes; then
    AC_DEFINE(HAVE_EXPAT, 1, [If libexpat is installed])
    LIB_EXPAT="-lexpat"
    AC_SUBST(LIB_EXPAT)
fi

dnl -------------------------
dnl check for XTest extension
dnl -------------------------

AC_ARG_WITH([xtest], [AS_HELP_STRING([--without-xtest], [build without XTest support])],,
  [with_xtest=yes])

CFLAGS_XTEST=""
LIB_XTEST=""

if test "$with_xtest" != "no" ; then
	AC_MSG_CHECKING([for XTest extension])

	if "$PKG_CONFIG" --exists xtst ; then
		with_xtest=yes
		CFLAGS_XTEST=`"$PKG_CONFIG" --cflags xtst`
		LIB_XTEST=`"$PKG_CONFIG" --libs xtst`
		AC_DEFINE([HAVE_XTEST],,[XTest extension available])
		AC_MSG_RESULT([yes])
	else
		with_xtest=no
		AC_MSG_RESULT([no])
	fi
fi

AC_SUBST([CFLAGS_XTEST])
AC_SUBST([LIB_XTEST])

KDE_CHECK_HEADER(xine.h,
   have_xine=yes,
   have_xine=no)

AC_MSG_CHECKING([if kxineplayer can be compiled])
AC_MSG_RESULT($have_xine)

AC_ARG_WITH(xine,
    AC_HELP_STRING([--without-xine],[build KMPlayer without Xine [default=with]]),
    [build_xine=$withval],
    [build_xine=yes]
)

if test "$build_xine" != "no"; then
    if test "$have_xine" = "yes"; then
      vers=`xine-config --version 2>/dev/null | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
      if test -n "$vers" && test "$vers" -ge 1000000
      then
            AC_DEFINE(HAVE_XINE, 1, [If we have libxine installed])
            LIB_XINE="`xine-config --libs`"
            AC_SUBST(LIB_XINE)
            CFLAGS_XINE="`xine-config --cflags`"
            AC_SUBST(CFLAGS_XINE)
      else
           have_xine="no"
           AC_MSG_WARN([Your xine installation is too old (1.0.0 or later required)])
      fi
    fi
else
    have_xine="no"
fi

AM_CONDITIONAL(include_kxineplayer, test "$have_xine" = "yes")

AC_ARG_WITH(gstreamer,
    AC_HELP_STRING([--without-gstreamer],[build KMPlayer without GStreamer [default=with]]),
    [build_gstreamer=$withval],
    [build_gstreamer=yes]
)

if test "$build_gstreamer" != "no"; then
    if test "$PKGCONFIGFOUND" = "yes" ; then
        # check for GStreamer
        dnl Now we're ready to ask for gstreamer libs and cflags
        dnl And we can also ask for the right version of gstreamer

        GST_MAJORMINOR=0.10
        GST_REQ=0.10.0

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

        PKG_CHECK_MODULES(GST_PLUGINS, gstreamer-plugins-base-$GST_MAJORMINOR >= $GST_REQ, \
            have_gst_plugins=yes,have_gst_plugins=no)
        LIB_GST=""
        CFLAGS_GST=""
        LIB_GST_PLUGINS=""
        dnl Give error if we don't have gstreamer
        if test "x$have_gst" = "xyes"; then
            AC_SUBST(GST_MAJORMINOR)
            AC_DEFINE(HAVE_GSTREAMER, 1, [have GStreamer])
            LIB_GST=$GST_LIBS
            CFLAGS_GST=$GST_CFLAGS
            LIB_GST_PLUGINS=$GST_INTERFACES_LIBS
        fi
        AC_SUBST(LIB_GST)
        AC_SUBST(CFLAGS_GST)
        AC_SUBST(LIB_GST_PLUGINS)
    fi
fi
AC_MSG_CHECKING([if kgstplayer can be compiled])
if test "x$have_gst" = "xyes" && test "x$have_gst_plugins" = "xyes"; then
    AC_MSG_RESULT(yes)
else
    AC_MSG_RESULT(no)
fi

AM_CONDITIONAL(include_kgstplayer, [test "x$have_gst" = "xyes" && test "x$have_gst_plugins" = "xyes"])

buildnpp=yes
LIBNSPR_LIBS=""
LIBNSPR_CFLAGS=""
LIBQTDBUS=""
LIBDBUS_CFLAGS=""
AC_ARG_ENABLE(nspr, [  --enable-npplayer    Enables building with npp support],
[if test "x$enableval" == "xno"; then
        buildnpp=no
    fi])
if test "$buildnpp" = "yes"; then
    if test "$PKGCONFIGFOUND" = "yes" ; then
        DBUS_REQ=1.0.0
        PKG_CHECK_MODULES(DBUS, dbus-1 >= $DBUS_REQ,
                          have_dbus=yes,have_dbus=no)
        if test "x$have_dbus" = "xyes"; then
            have_nspr=yes
            AC_DEFINE(HAVE_DBUS, 1, [have DBus])
            LIBQTDBUS="`$PKG_CONFIG --libs dbus-1` -ldbus-tqt-1"
	    LIBDBUS_CFLAGS="`$PKG_CONFIG --cflags dbus-1`"
            AC_DEFINE(HAVE_NSPR, 1, [build Netscape plugin loader])
            LIBNSPR_LIBS="`$PKG_CONFIG --libs gtk+-x11-2.0` `$PKG_CONFIG --libs dbus-glib-1` `$PKG_CONFIG --libs gthread-2.0`"
            LIBNSPR_CFLAGS="`$PKG_CONFIG --cflags gtk+-x11-2.0` `$PKG_CONFIG --cflags dbus-glib-1`"
        fi
    fi
fi
AC_SUBST(LIBNSPR_LIBS)
AC_SUBST(LIBNSPR_CFLAGS)
AC_SUBST(LIBQTDBUS)
AC_SUBST(LIBDBUS_CFLAGS)

AM_CONDITIONAL(include_knpplayer, test "$have_nspr" = "yes")

hascairo=yes
LIBCAIRO_LIBS=""
LIBCAIRO_CFLAGS=""
AC_ARG_ENABLE(cairo, [  --enable-cairo     Enables building with cairo support],
[if test "x$enableval" == "xno"; then
        hascairo=no
    fi])
if test $hascairo = yes; then
        AC_MSG_CHECKING([Checking for cairo with pkg-config])
        hascairo=no
        if test -n "$PKG_CONFIG"; then
                LIBCAIRO_LIBS="`$PKG_CONFIG cairo --libs`"
                LIBCAIRO_CFLAGS="`$PKG_CONFIG cairo --cflags`"
                if test -n "$LIBCAIRO_LIBS" || test -n "$LIBCAIRO_CFLAGS"; then
                        AC_MSG_RESULT([found])
                        hascairo=yes
                else
                        AC_MSG_RESULT([not found])
                fi
        else
                AC_MSG_RESULT([failed: pkg-config not found])
        fi
        if test $hascairo = yes; then
                AC_DEFINE_UNQUOTED(HAVE_CAIRO, 1, [Defines if your system has the cairo library])
        else
                AC_MSG_WARN([Couldn't find a usable cairo])
        fi
fi
AC_SUBST(LIBCAIRO_LIBS)
AC_SUBST(LIBCAIRO_CFLAGS)

KDE_CHECK_BINUTILS

AC_MSG_CHECKING([for KDE version])

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

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 3, 91 ) )
#error KDE 3.3
#endif
],
    need_trinity3_compat="no"
,
    need_trinity3_compat="yes"
)

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 2, 90 ) )
#error KDE 3.2
#endif
],
    need_trinity2_compat="no"
,
    need_trinity2_compat="yes"
)

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 1, 90 ) )
#error KDE 3.1
#endif
],
    need_trinity1_compat="no"
,
    need_trinity1_compat="yes"
)

CXXFLAGS="$kdeversion_save_CXXFLAGS"
LIBS="$kdeversion_safe_LIBS"
AC_LANG_RESTORE

if test "$need_trinity2_compat" = "yes"; then
    AC_MSG_RESULT([KDE 3.2.x])
fi

if test "$need_trinity1_compat" = "yes"; then
    AC_MSG_RESULT([KDE 3.1.x])
fi

AM_CONDITIONAL(need_trinity3_compatibility, test "$need_trinity3_compat" = "yes")
AM_CONDITIONAL(need_trinity2_compatibility, test "$need_trinity2_compat" = "yes")
AM_CONDITIONAL(need_trinity1_compatibility, test "$need_trinity1_compat" = "yes")