# ----------------------------------------------------------
#
# pkg-config check
#
# ----------------------------------------------------------

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

# ----------------------------------------------------------
#
# common header files path for kipi-plugins
#
# ----------------------------------------------------------

KIPI_PLUGINS_COMMON_INCLUDE="-I\$(top_srcdir)/kipi-plugins/common/include"
AC_SUBST(KIPI_PLUGINS_COMMON_INCLUDE)

KIPI_PLUGINS_COMMON_LDFLAGS="-L\$(top_builddir)/kipi-plugins/common/libkipiplugins"
AC_SUBST(KIPI_PLUGINS_COMMON_LDFLAGS)

# -----------------------------------------------------------------
#
# enable hidden visibility only if kde >= 3.3.2 and tdelibs has
# been compiled with visibility enabled
#
# -----------------------------------------------------------------

AC_LANG_PUSH(C++)
kipiplugins_save_cppflags=$CPPFLAGS
CPPFLAGS="$CPPFLAGS $all_includes"  
AC_MSG_CHECKING([if hidden visibility should be enabled])
AC_COMPILE_IFELSE(
 [
   #include <tdeversion.h>
   #include <kdemacros.h>
   int other_func( void )
   { 
      #if KDE_IS_VERSION(3,3,2)
      #else
        iam dying;
      #endif
      #ifdef __KDE_HAVE_GCC_VISIBILITY
      #else
        no, iam really dead;
      #endif      
      return 0;
   }
 ], 
 [ AC_MSG_RESULT([yes])
   kipiplugins_enable_hidden_visibility="yes" ],
 [ AC_MSG_RESULT([no]) ] 
)
CPPFLAGS=$kipiplugins_save_cppflags
AC_LANG_POP(C++)

if test "x$kipiplugins_enable_hidden_visibility" = "xyes"; then
   KDE_ENABLE_HIDDEN_VISIBILITY 
fi

# ----------------------------------------------------------
#
# General kipi-plugin tests
#
# ----------------------------------------------------------

AC_MSG_CHECKING([for libkipi in our sources])
if test -d $srcdir/libkipi; then
    have_libkipi=yes
    LIBKIPI_CFLAGS='-I$(top_builddir)/libkipi -I$(top_srcdir)/libkipi'
    LIBKIPI_LIBS='$(top_builddir)/libkipi/libkipi/libkipi.la'
    LIBKIPI_LIBS_DEP='$(LIBKIPI_LIBS)'
    AC_MSG_RESULT([found])
    AC_SUBST(LIBKIPI_CFLAGS)
    AC_SUBST(LIBKIPI_LIBS)
    AC_SUBST(LIBKIPI_LIBS_DEP)
    AC_MSG_RESULT([found])
else
    AC_MSG_RESULT([not found in sources])
    if test "$PKGCONFIGFOUND" = "yes" ; then
        # check for libkipi
        have_libkipi=no

	KDE_PKG_CHECK_MODULES(LIBKIPI, libkipi >= 0.1.5,
		have_libkipi=yes, have_libkipi=no)

	if test "x$have_libkipi" = "xno"; then
	   LIBKIPI_CFLAGS=""
	   LIBKIPI_LIBS=""
	fi
     else
        LIBKIPI_CFLAGS=""
	LIBKIPI_LIBS=""
     fi
     AC_SUBST(LIBKIPI_CFLAGS)
     AC_SUBST(LIBKIPI_LIBS)
fi

if test "x$have_libkipi" != "xyes"; then
     AC_MSG_ERROR([libkipi library is required by kipi-plugins.])
     DO_NOT_COMPILE="$DO_NOT_COMPILE kipi-plugins"
fi

# --------------------------------------------------------------------
#
# Check for ICU CXXFLAGS
#
# --------------------------------------------------------------------
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])
])
if test -n "$PKGCONFIG"; then
  ICU_VERSION=`$PKGCONFIG icu-uc --modversion 2>/dev/null`
  if test -n "$ICU_VERSION"; then
    ICU_CXXFLAGS=`$PKGCONFIG icu-uc --variable=CXXFLAGS`
  fi
fi
if test -z "$ICU_VERSION"; then
  KDE_FIND_PATH(icu-config, ICU_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/local/bin /opt/local/bin], [
    AC_MSG_WARN([Could not find icu-config])
  ])
  if test -n "$ICU_CONFIG"; then
    ICU_CXXFLAGS=`$ICU_CONFIG --cxxflags`
  fi
fi
AC_SUBST(ICU_CXXFLAGS)

# --------------------------------------------------------------------
#
# Check for libkexiv2
#
# --------------------------------------------------------------------

AC_MSG_CHECKING([for libkexiv2 in our sources])
if test -d $srcdir/libkexiv2; then
    have_libkexiv2=yes
    LIBKEXIV2_CFLAGS='-I$(top_srcdir)/libkexiv2'
    LIBKEXIV2_LIBS='$(top_builddir)/libkexiv2/libkexiv2/libkexiv2.la'
    LIBKEXIV2_LIBS_DEP='$(LIBKEXIV2_LIBS)'
    AC_MSG_RESULT([found])
    AC_SUBST(LIBKEXIV2_CFLAGS)
    AC_SUBST(LIBKEXIV2_LIBS)
    AC_SUBST(LIBKEXIV2_LIBS_DEP)
else
    AC_MSG_RESULT([not found in sources])
    if test "$PKGCONFIGFOUND" = "yes" ; then
        # check for libkexiv2
        have_libkexiv2=no

        KDE_PKG_CHECK_MODULES(LIBKEXIV2, libkexiv2 >= 0.1.5,
                have_libkexiv2=yes, have_libkexiv2=no)

        if test "x$have_libkexiv2" = "xno"; then
           LIBKEXIV2_CFLAGS=""
           LIBKEXIV2_LIBS=""
        fi
     else
        LIBKEXIV2_CFLAGS=""
        LIBKEXIV2_LIBS=""
     fi
     AC_SUBST(LIBKEXIV2_CFLAGS)
     AC_SUBST(LIBKEXIV2_LIBS)
fi

if test x$have_libkexiv2 != xyes; then
  AC_MSG_ERROR([libkexiv2 library is required by kipi-plugins.])
  DO_NOT_COMPILE="$DO_NOT_COMPILE kipi-plugins"
fi

# --------------------------------------------------------------------
#
# Check for libkdcraw
#
# --------------------------------------------------------------------

AC_MSG_CHECKING([for libkdcraw in our sources])
if test -d $srcdir/libkdcraw; then
    have_libkdcraw=yes
    LIBKDCRAW_CFLAGS='-I$(top_builddir)/libkdcraw -I$(top_srcdir)/libkdcraw'
    LIBKDCRAW_LIBS='$(top_builddir)/libkdcraw/libkdcraw/libkdcraw.la'
    LIBKDCRAW_LIBS_DEP='$(LIBKDCRAW_LIBS)'
    AC_MSG_RESULT([found])
    AC_SUBST(LIBKDCRAW_CFLAGS)
    AC_SUBST(LIBKDCRAW_LIBS)
    AC_SUBST(LIBKDCRAW_LIBS_DEP)
    AC_MSG_RESULT([found])
else
    AC_MSG_RESULT([not found in sources])
    if test "$PKGCONFIGFOUND" = "yes" ; then
        # check for libkdcraw
        have_libkdcraw=no

        KDE_PKG_CHECK_MODULES(LIBKDCRAW, libkdcraw >= 0.1.4,
                have_libkdcraw=yes, have_libkdcraw=no)

        if test "x$have_libkdcraw" = "xno"; then
           LIBKDCRAW_CFLAGS=""
           LIBKDCRAW_LIBS=""
        fi
     else
        LIBKDCRAW_CFLAGS=""
        LIBKDCRAW_LIBS=""
     fi
     AC_SUBST(LIBKDCRAW_CFLAGS)
     AC_SUBST(LIBKDCRAW_LIBS)
fi

if test x$have_libkdcraw != xyes; then
  AC_MSG_ERROR([libkdcraw library is required by kipi-plugins.])
  DO_NOT_COMPILE="$DO_NOT_COMPILE kipi-plugins"
fi

#------------------------------------------------------------------
#
# Ability to fine-tune what plugins to build
#
#------------------------------------------------------------------

# Following macro sets up individual kipi-plugins. It generates AC_ARG_ENABLE.
# Third argument, plugin_dependencies is an optional space-separated list of dependencies.
# In case the plugin is enabled, it sets build_${dependency}=yes (for every dependency)
# KIPI_PLUGIN_PRE(plugin_directory, plugin_name[, plugin_depencencies])
AC_DEFUN([KIPI_PLUGIN_PRE], [
  AC_ARG_ENABLE([$1],
    AC_HELP_STRING([--disable-$1], [$2 kipi plugin.] m4_if([$3], [], [], [[ Requires $3.]]) [ [[enable]]]),
    [enable_$1="$enableval"],
    [enable_$1="check"],
  )
  if test "${enable_$1}" != "yes" -a "${enable_$1}" != "no"; then
    enable_$1="check"
  fi
  kipi_plug_deps_$1="$3"
  kipi_plug_name_$1="$2"
  if test "$enable_$1" != "no" -a -n "$3"; then
    for dep in $3; do
      eval build_${dep}="yes"   # we must use eval because we use shell variable inside variable name
    done
  fi
])

# Add entry for every new conditional dependency here
build_gphoto=no
build_libgpod=no
build_libkcal=no
build_tiff=no
build_xslt=no
build_opengl=no

# Please keep this list sorted and add all new plugins
KIPI_PLUGIN_PRE([acquireimages], [Acquire Images], [tiff])
KIPI_PLUGIN_PRE([batchprocessimages], [Batch Process Images])
KIPI_PLUGIN_PRE([calendar], [Calendar], [libkcal])
KIPI_PLUGIN_PRE([cdarchiving], [CD Archiving])
KIPI_PLUGIN_PRE([findimages], [Find Images])
KIPI_PLUGIN_PRE([flickrexport], [Flickr Exporter])
KIPI_PLUGIN_PRE([galleryexport], [Remote Gallery Export])
KIPI_PLUGIN_PRE([gpssync], [GPS Synchronization])
KIPI_PLUGIN_PRE([htmlexport], [HTML Gallery], [xslt])
#KIPI_PLUGIN_PRE([imagesgallery], [Images Gallery])      # ???
KIPI_PLUGIN_PRE([imageviewer], [Viewer], [opengl])
KIPI_PLUGIN_PRE([ipodexport], [iPod Export], [libgpod])
KIPI_PLUGIN_PRE([jpeglossless], [JPEG Lossless]) # Does it depend on tiff?
KIPI_PLUGIN_PRE([kameraklient], [Kamera Klient], [gphoto])
KIPI_PLUGIN_PRE([metadataedit], [Metadata Editor])
KIPI_PLUGIN_PRE([mpegencoder], [MPEG Encoder])
KIPI_PLUGIN_PRE([picasawebexport], [Picasaweb Exporter])
KIPI_PLUGIN_PRE([printwizard], [Print Wizard])
KIPI_PLUGIN_PRE([rawconverter], [RAW converter], [tiff])
KIPI_PLUGIN_PRE([sendimages], [Send Images])
KIPI_PLUGIN_PRE([simpleviewerexport], [Simple Viewer])
KIPI_PLUGIN_PRE([slideshow], [Slideshow], [opengl])
#KIPI_PLUGIN_PRE([sync], [Sync])   # ???
KIPI_PLUGIN_PRE([timeadjust], [Date & Time Adjust])
KIPI_PLUGIN_PRE([wallpaper], [Wallpaper])

#------------------------------------------------------------------
#
# Check for libtiff
#
#------------------------------------------------------------------

have_tiff=no

if test "x$build_tiff" != "xno"; then
  AC_CHECK_LIB(tiff, TIFFWriteScanline,
    have_tiff=yes,
    AC_MSG_WARN([TIFF library not found]),
    $all_libraries -ljpeg -lz -lm)

  if test "x$have_tiff" = "xyes"; then
    KDE_CHECK_HEADER(tiffio.h, have_tiff=yes, have_tiff=no)
  fi

  if test "x$have_tiff" != "xyes"; then
    AC_WARN([TIFF library not found, some plugins will not be compiled.])
  else
    LIB_TIFF="-ltiff"
    AC_SUBST(LIB_TIFF)
  fi
fi

#---------------------------------------------------------
#
# gphoto2 detection - KameraKlient plugin
#
#---------------------------------------------------------

have_gphoto=no

if test "x$build_gphoto" != "xno"; then
  AC_PATH_PROG(GPHOTO_CONFIG,gphoto2-config)
  AC_PATH_PROG(GPHOTO_PORT_CONFIG,gphoto2-port-config)
  if test -n "${GPHOTO_CONFIG}"; then
    GPHOTO_CFLAGS="`$GPHOTO_CONFIG --cflags`"
    AC_SUBST(GPHOTO_CFLAGS)
    LIB_GPHOTO="`$GPHOTO_CONFIG --libs` `$GPHOTO_PORT_CONFIG --libs`"
    AC_SUBST(LIB_GPHOTO)
    have_gphoto=yes
    AC_CHECK_LIB(gphoto2_port, gp_port_info_get_name, have_gphoto2_5=yes, have_gphoto2_5=no)
    if test "x$have_gphoto2_5" != "xno"; then
      AC_DEFINE(HAVE_GPHOTO2_5, 1, [have gphoto 2.5])
    fi
  else
    AC_MSG_WARN([gPhoto2 not found, some plugins will not be compiled.])
  fi
fi

#---------------------------------------------------------
#
# xslt detection - HTML export plugin
#
#---------------------------------------------------------

have_xslt=no

if test "x$build_xslt" != "xno"; then
  AC_PATH_PROG(XSLT_CONFIG,xslt-config)
  if test -n "${XSLT_CONFIG}"; then
    LIBXSLT_CFLAGS="`$XSLT_CONFIG --cflags`"
    AC_SUBST(LIBXSLT_CFLAGS)
    LIBXSLT_LIBS="`$XSLT_CONFIG --libs`"
    AC_SUBST(LIBXSLT_LIBS)
    have_xslt=yes
  else
    AC_MSG_WARN([libxslt library not found, some plugins will not be compiled])
  fi
fi

#------------------------------------------------------------------
#
# Check for libgpod - ipod export plugin
#
#------------------------------------------------------------------

have_libgpod=no

if test "x$build_libgpod" != "xno"; then
    if test "$PKGCONFIGFOUND" = "yes" ; then

        KDE_PKG_CHECK_MODULES(LIBGPOD, libgpod-1.0 >= 0.4.2 gobject-2.0, have_libgpod=yes,have_libgpod=no)

        if test "x$have_libgpod" = "xyes"; then
            AC_DEFINE(HAVE_LIBGPOD, 1, [have libgpod])
            ac_cppflags_save=$CPPFLAGS
            ac_cflags_save=$CFLAGS
            ac_ldflags_save=$LDFLAGS

            CPPFLAGS="$CPPFLAGS $LIBGPOD_INCLUDES"
            CFLAGS="$CFLAGS $LIBGPOD_CFLAGS"
            LDFLAGS="$LDFLAGS $LIBGPOD_LIBS"
        else
            AC_MSG_WARN([libgpod is required for the iPod export plugin.])
        fi
    fi
fi

#------------------------------------------------------------------
#
# Check for libkcal - iCalendar support library
#
#------------------------------------------------------------------

have_libkcal=no

if test "x$build_libkcal" != "xno"; then
  KDE_CHECK_HEADERS(libkcal/calendarlocal.h, have_libkcal=yes, have_libkcal=no)
  if test "x$have_libkcal" != "xyes"; then
    AC_WARN([libkcal not found, some plugins will not be compiled.])
  else
    LIB_KCAL="-llibkcal"
    AC_SUBST(LIB_KCAL)
  fi
fi

# ----------------------------------------------------------
#
# Qt OpenGl check (fudged from Amarok) - Slideshow plugin
#
# ----------------------------------------------------------

have_opengl=no

if test "x$build_opengl" != "xno"; then
    AC_MSG_CHECKING(for Qt with OpenGL support)
    AC_CACHE_VAL(ac_cv_kde_qt_has_opengl,
    [
    AC_LANG_SAVE
    AC_LANG_CPLUSPLUS

    save_CXXFLAGS="$CXXFLAGS"
    save_LIBS="$LIBS"
    save_LDFLAGS="$LDFLAGS"

    CXXFLAGS="$CXXFLAGS -I$qt_incdir $all_includes"
    LDFLAGS="$LDFLAGS -L$qt_libdir $all_libraries $USER_LDFLAGS $KDE_MT_LDFLAGS"
    LIBS="$LIBS $LIBTQT $KDE_MT_LIBS"

    AC_TRY_LINK([
    #include <tqgl.h>
    ],
    [
    (void)new TQGLWidget((TQWidget*)0, "qgl");
    ],
    ac_cv_kde_qt_has_opengl=yes,
    ac_cv_kde_qt_has_opengl=no)

    CXXFLAGS="$save_CXXFLAGS"
    LIBS="$save_LIBS"
    LDFLAGS="$save_LDFLAGS"
    AC_LANG_RESTORE
    ])
    AC_MSG_RESULT($ac_cv_kde_qt_has_opengl)

    if test x$ac_cv_kde_qt_has_opengl = xyes; then
       have_opengl="yes"
       GL_LIBS="-lGL"
    else
       have_opengl="no"
       GL_LIBS=""
    fi

    AC_SUBST(GL_LIBS)
fi

# ----------------------------------------------------------
#
# Second stage of kipi-plugin fine-tuning.
#
# ----------------------------------------------------------

# Following macro checks if particular plugin should be enabled and for
# its dependencies. (have_$dependency must be either yes or no). Sets
# AM_CONDITIONAL accordingly. KIPI_PLUGIN_PRE must precede it.
# KIPI_PLUGIN_POST([plugin_directory])
AC_DEFUN([KIPI_PLUGIN_POST], [
  test="test $enable_$1 != no"
  if test $enable_$1 != no; then
    for dep in $kipi_plug_deps_$1; do
      test="${test} -a \$have_${dep} = yes"
      if eval test \$have_${dep} != "yes"; then   # we must use eval cos we use shell variable inside variable name
        AC_MSG_WARN([$dep could not be found, $kipi_plug_name_$1 ($1) plugin will not be built.])
      fi
    done
  fi
  if eval $test; then
    enabled_kipi_plugins="$enabled_kipi_plugins $1"
  else
    disabled_kipi_plugins="$disabled_kipi_plugins $1"
    if test $enable_$1 = yes; then
      AC_MSG_ERROR([Failing as hard-enabled plugin cannot be compiled.])
    fi
  fi
  AM_CONDITIONAL([compile_]m4_toupper($1), [eval $test])
])

enabled_kipi_plugins=""
disabled_kipi_plugins=""

# Please keep this list sorted and add all new plugins
KIPI_PLUGIN_POST([acquireimages])
KIPI_PLUGIN_POST([batchprocessimages])
KIPI_PLUGIN_POST([calendar])
KIPI_PLUGIN_POST([cdarchiving])
KIPI_PLUGIN_POST([findimages])
KIPI_PLUGIN_POST([flickrexport])
KIPI_PLUGIN_POST([galleryexport])
KIPI_PLUGIN_POST([gpssync])
KIPI_PLUGIN_POST([htmlexport])
#KIPI_PLUGIN_POST([imagesgallery])   # ???
KIPI_PLUGIN_POST([imageviewer])
KIPI_PLUGIN_POST([ipodexport])
KIPI_PLUGIN_POST([jpeglossless])
KIPI_PLUGIN_POST([kameraklient])
KIPI_PLUGIN_POST([metadataedit])
KIPI_PLUGIN_POST([mpegencoder])
KIPI_PLUGIN_POST([picasawebexport])
KIPI_PLUGIN_POST([printwizard])
KIPI_PLUGIN_POST([rawconverter])
KIPI_PLUGIN_POST([sendimages])
KIPI_PLUGIN_POST([simpleviewerexport])
KIPI_PLUGIN_POST([slideshow])
#KIPI_PLUGIN_POST([sync])   # ???
KIPI_PLUGIN_POST([timeadjust])
KIPI_PLUGIN_POST([wallpaper])
