#MIN_CONFIG(3.3)

# Remember to synchronize the version number with the file koffice/lib/kofficecore/kofficeversion.h
AM_INIT_AUTOMAKE(koffice, "1.5.2")

CXXFLAGS="$CXXFLAGS $KDE_DEFAULT_CXXFLAGS"

AC_CHECK_HEADERS(unistd.h sys/param.h floatingpoint.h paths.h)
AC_C_BIGENDIAN
AC_CHECK_KDEMAXPATHLEN

KDE_INIT_DOXYGEN([The KOffice API Reference], [Version $VERSION])


# Check for ImageMagick...

have_imagemagick=no
KDE_FIND_PATH(Magick-config, MAGICK_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/local/bin /opt/local/bin], [
  AC_MSG_WARN([Could not find ImageMagick anywhere, check http://www.imagemagick.org/ for ImageMagick >= 5.5.2.])
])

if test -n "$MAGICK_CONFIG"; then
  vers=`$MAGICK_CONFIG --version 2>/dev/null | $AWK 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
  if test -n "$vers" && test "$vers" -ge 5005002 
  then
     if test "$vers" -ge 6000003
     then
        AC_DEFINE(HAVE_MAGICK6, 1, [ImageMagick Version 6])
     fi
     if test "$vers" -ge 6001000
     then
        AC_DEFINE(HAVE_MAGICK61, 1, [ImageMagick Version 6.1])
     fi
     LIBMAGICK_LIBS="`$MAGICK_CONFIG --libs`"
     LIBMAGICK_LDFLAGS="`$MAGICK_CONFIG --ldflags`"
     LIBMAGICK_RPATH=
     for args in $LIBMAGICK_LIBS; do
	  case $args in
	    -L*)
  	       LIBMAGICK_RPATH="$LIBMAGICK_RPATH $args"
 	       ;;
          esac
     done
     LIBMAGICK_RPATH=`echo $LIBMAGICK_RPATH | $SED -e "s/-L/-R/g"`
     LIBMAGICK_CPPFLAGS="`$MAGICK_CONFIG --cppflags`"
     AC_DEFINE(HAVE_MAGICK,1, [ImageMagick is available])
     have_imagemagick=yes
  else
     AC_MSG_WARN([You need at least ImageMagick 5.5.2])
  fi
fi

if test ! "$USE_RPATH" = "yes"; then
    LIBMAGICK_RPATH=
fi

AC_SUBST(LIBMAGICK_LIBS)
AC_SUBST(LIBMAGICK_LDFLAGS)
AC_SUBST(LIBMAGICK_CPPFLAGS)
AC_SUBST(LIBMAGICK_RPATH)
AM_CONDITIONAL(include_imagemagick_filter, test "$have_imagemagick" = "yes" -a HAVE_MAGICK61)

# End of ImageMagick check

##########################################################################
# This last check is copied from kdenonbeta/gsf/configure.in.in
##########################################################################
# KOFFICE_PKG_CHECK_MODULES(GSTUFF, gtk+-2.0 >= 1.3 glib = 1.3.4, action-if, action-not)
# defines GSTUFF_LIBS, GSTUFF_CFLAGS, see pkg-config man page
# also defines GSTUFF_PKG_ERRORS on error
# Note: This is specially tweaked for karbon's fontconfig check. Please fix
# it before using it for other tests :-)
AC_DEFUN([KOFFICE_PKG_CHECK_MODULES], [
  succeeded=no

  if test -z "$PKG_CONFIG"; then
    AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
  fi

  if test "$PKG_CONFIG" = "no" ; then
     echo "*** The pkg-config script could not be found. Make sure it is"
     echo "*** in your path, or set the PKG_CONFIG environment variable"
     echo "*** to the full path to pkg-config."
     echo "*** Or see http://www.freedesktop.org/software/pkgconfig to get pkg-config."
     echo "***"
     echo "*** Due to that we can't perform the check for fontconfig..."    # added for karbon (Werner)
  else
     PKG_CONFIG_MIN_VERSION=0.9.0
     if $PKG_CONFIG --atleast-pkgconfig-version $PKG_CONFIG_MIN_VERSION; then
        AC_MSG_CHECKING(for $2)

        if $PKG_CONFIG --exists "$2" ; then
            AC_MSG_RESULT(yes)
            succeeded=yes

            AC_MSG_CHECKING($1_CFLAGS)
            $1_CFLAGS=`$PKG_CONFIG --cflags "$2"`
            AC_MSG_RESULT($$1_CFLAGS)

            AC_MSG_CHECKING($1_LIBS)
            $1_LIBS=`$PKG_CONFIG --libs "$2"`
            AC_MSG_RESULT($$1_LIBS)
        else
            $1_CFLAGS=""
            $1_LIBS=""
            ## If we have a custom action on failure, don't print errors, but 
            ## do set a variable so people can do so.
            $1_PKG_ERRORS=`$PKG_CONFIG --errors-to-stdout --print-errors "$2"`
            ifelse([$4], ,echo $$1_PKG_ERRORS,)
        fi

        AC_SUBST($1_CFLAGS)
        AC_SUBST($1_LIBS)
     else
        echo "*** Your version of pkg-config is too old. You need version $PKG_CONFIG_MIN_VERSION or newer."
        echo "*** See http://www.freedesktop.org/software/pkgconfig"
     fi
  fi

  if test $succeeded = yes; then
     ifelse([$3], , :, [$3])
#  else                           # removed for karbon (Werner)
#    ifelse([$4], , AC_MSG_ERROR([Library requirements ($2) not met; consider adjusting the PKG_CONFIG_PATH environment variable if your libraries are in a nonstandard prefix so pkg-config can find them.]), [$4])
  fi
])

# --- Check for KDE 3.2 or 3.3 ---

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, 2, 90 ) )
#error KDE 3.2
#endif
],
	need_kde32_compat="no"
,
	need_kde32_compat="yes"
)

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

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

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 5, 2 ) )
#error KDE 3.5.x (x < 2)
#endif
],
	need_kde351_compat="no"
,
	need_kde351_compat="yes"
)
CXXFLAGS="$kdeversion_save_CXXFLAGS"
LIBS="$kdeversion_safe_LIBS"
AC_LANG_RESTORE

if test "$need_kde32_compat" = "yes"; then
	AC_MSG_RESULT([KDE 3.2.x])
else
	if test "$need_kde33_compat" = "yes"; then
		AC_MSG_RESULT([KDE 3.3.x])
	else 
		if test "$need_kde34_compat" = "yes"; then
			AC_MSG_RESULT([KDE 3.4.x])
                else
                        if test "$need_kde351_compat" = "yes"; then
                                AC_MSG_RESULT([KDE 3.5.x (x < 2)])
                        else
			        AC_MSG_RESULT([KDE 3.5.x (x >=2) or SVN trunk])
                        fi
		fi
	fi
fi

AM_CONDITIONAL(need_kde32_compatibility, test "$need_kde32_compat" = "yes")
AM_CONDITIONAL(need_kde33_compatibility, test "$need_kde33_compat" = "yes")
AM_CONDITIONAL(need_kde34_compatibility, test "$need_kde34_compat" = "yes")
AM_CONDITIONAL(need_kde351_compatibility, test "$need_kde351_compat" = "yes")

# Keep the old KDE 3.1 test, as long as it is still used
AM_CONDITIONAL(need_kde31_compatibility, test "supported" = "no")

# --- End KDE 3.2 check ---

# --- OpenEXR check ---

KDE_FIND_PATH(pkg-config, PKGCONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
  AC_MSG_WARN([Could not find pkg-config])
])

AC_MSG_CHECKING([for OpenEXR])

if test -n "$PKGCONFIG"; then
  vers=`$PKGCONFIG OpenEXR --modversion 2>/dev/null`
  if test -n "$vers"
  then
     OPENEXR_LIBS="`$PKGCONFIG OpenEXR --libs`"
     OPENEXR_RPATH=
     for args in $OPENEXR_LIBS; do
          case $args in
            -L*)
               OPENEXR_RPATH="$OPENEXR_RPATH $args"
               ;;
          esac
     done
     OPENEXR_RPATH=`echo $OPENEXR_RPATH | $SED -e "s/-L/-R/g"`
     OPENEXR_CFLAGS="`$PKGCONFIG OpenEXR --cflags`"

     AC_DEFINE_UNQUOTED(HAVE_OPENEXR, 1, [Defines if your system has the OpenEXR library])
  fi
fi

if test ! "$USE_RPATH" = "yes"; then
    OPENEXR_RPATH=
fi

if test -n "$OPENEXR_LIBS"
then
  AC_MSG_RESULT([yes])
else
  AC_MSG_RESULT([not found])
fi

AC_SUBST(OPENEXR_LIBS)
AC_SUBST(OPENEXR_CFLAGS)
AC_SUBST(OPENEXR_RPATH)

AM_CONDITIONAL(have_openexr, test -n "$OPENEXR_LIBS")

# --- End of OpenEXR check ---
