dnl KDE_FIND_GPHOTO2 - Find gphoto2 libraries and include files
dnl
dnl Adapted from tdebase/nsplugins/configure.in.in

AC_DEFUN([KDE_FIND_GPHOTO2],
[



# Clear working variables
gphoto2_includes=
gphoto2_libraries=



# Process user input to configure
AC_ARG_WITH(kamera,
AC_HELP_STRING([--without-kamera],[do not build kamera (gphoto2 required)]),
[if test "$withval" = "no" ; then
  gphoto2_includes=none
  gphoto2_libraries=none
fi])dnl

AC_ARG_WITH(gphoto2-includes,
AC_HELP_STRING([--with-gphoto2-includes=DIR],[gphoto2 include files are in DIR]),
gphoto2_includes="$withval")

AC_ARG_WITH(gphoto2-libraries,
AC_HELP_STRING([--with-gphoto2-libraries=DIR],[gphoto2 libraries are in DIR]),
gphoto2_libraries="$withval")

AC_MSG_CHECKING(for gPhoto2)
# the pkg-config way first, if user did not use --with-
AC_CHECK_PROG(gphoto2_config,gphoto2-config,gphoto2-config,no)
AC_CHECK_PROG(gphoto2_port_config,gphoto2-port-config,gphoto2-port-config,no)
if test "$gphoto2_includes" = "" -a "$gphoto2_libraries" = "" -a "$gphoto2_config" != "no" -a "$gphoto2_port_config" != "no"
then
	with_kamera="yes"
	GPHOTO2_INCS="`$gphoto2_config --cflags` `$gphoto2_port_config --cflags`"
	GPHOTO2_LIBS="`$gphoto2_config --libs` `$gphoto2_port_config --libs`"
else
#
# Search for gphoto2 include files.
#
	if test "$gphoto2_includes" = ""; then
		AC_CACHE_VAL(ac_cv_gphoto2_includes, [
			ac_gphoto2_save_LIBS="$LIBS"
			LIBS="-lgphoto2 $LIBS"
			ac_cv_gphoto2_includes="none"
			AC_TRY_COMPILE([#include <gphoto2.h>],[int a;],
			[
			# gphoto2.h is in the standard search path.
				ac_cv_gphoto2_includes=
			],[
			# gphoto2.h is not in the standard search path.
			# Locate it and put its directory in `gphoto2_includes'
			for dir in /usr/include /usr/local/include \
			   /usr/include/gphoto2 /usr/local/include/gphoto2; do
				if test -f "$dir/gphoto2.h"; then
					ac_cv_gphoto2_includes="$dir"
					break
				fi
			done
			])
			#
			LIBS="$ac_gphoto2_save_LIBS"
		])
		gphoto2_includes="$ac_cv_gphoto2_includes"
	fi

	#
	# Search for libgphoto2
	#
	if test "$gphoto2_libraries" = ""; then
		AC_CACHE_VAL(ac_cv_gphoto2_libraries,[
			ac_gphoto2_save_LIBS="$LIBS"
			LIBS="-lgphoto2_port -lgphoto2 $LIBS"
			ac_cv_gphoto2_libraries="none"
			AC_TRY_LINK([#include <gphoto2.h>],[gp_context_progress_start(0,0,0,0);], [
				# libgphoto2 is in the standard search path.
				ac_cv_gphoto2_libraries=
			],[
				# libgphoto2 is not in the standard search path.
				# Locate it and put its directory in `gphoto2_libraries'
				for dir in /usr/lib /usr/local/lib; do
					if test -d "$dir" && test "`ls $dir/libgphoto2.* 2> /dev/null`" != ""; then
						ac_cv_gphoto2_libraries="$dir"
						break
					fi
				done
			])
			#
			LIBS="$ac_gphoto2_save_LIBS"
		])
		#
		gphoto2_libraries="$ac_cv_gphoto2_libraries"
	fi
# Initialise compiler and linker flag variables for export
	if test "$gphoto2_includes" = "none" -o "$gphoto2_libraries" = "none" ; then
		with_kamera="no"
	else
		with_kamera="yes"

		if test "$gphoto2_libraries" = "" -o "$gphoto2_libraries" = "none"; then
			GPHOTO2_LIBS="-lgphoto2"
		else
			GPHOTO2_LIBS="-L$gphoto2_libraries -lgphoto2"
		fi
		if test "$gphoto2_includes" != "" -a "$gphoto2_includes" != "none"; then
			GPHOTO2_INCS="-I$gphoto2_includes"
		fi
	fi
fi

if test "$with_kamera" = "yes" ; then
	# Check if it works.
	ac_gphoto2_save_LIBS="$LIBS"
	ac_gphoto2_save_CFLAGS="$CFLAGS"
	LIBS="$LIBS $GPHOTO2_LIBS"
	CFLAGS="$CFLAGS $GPHOTO2_INCS"
	AC_TRY_LINK([#include <gphoto2.h>],[gp_context_progress_start(0,0,0,0);], [
		# It works.
		AC_DEFINE(HAVE_GPHOTO2,1,[Define if you have gPhoto2 installed])
	],[
		with_kamera="no"
	])
	LIBS="$ac_gphoto2_save_LIBS"
	CFLAGS="$ac_gphoto2_save_CFLAGS"
fi
dnl **** Check for va_copy ****
AC_CACHE_CHECK([for va_copy], ac_cv_c_va_copy,
 AC_TRY_LINK(
   [#include <stdarg.h>],
   [va_list ap1, ap2;
    va_copy(ap1,ap2);
   ],
   [ac_cv_c_va_copy="yes"],
   [ac_cv_c_va_copy="no"])
 )
if test "$ac_cv_c_va_copy" = "yes"
then
    AC_DEFINE(HAVE_VA_COPY, 1, [Define if we have va_copy])
fi
AC_CACHE_CHECK([for __va_copy], ac_cv_c___va_copy,
 AC_TRY_LINK(
   [#include <stdarg.h>],
   [va_list ap1, ap2;
    __va_copy(ap1,ap2);
   ],
   [ac_cv_c___va_copy="yes"],
   [ac_cv_c___va_copy="no"])
 )
if test "$ac_cv_c___va_copy" = "yes"
then
    AC_DEFINE(HAVE___VA_COPY, 1, [Define if we have __va_copy])
fi

# Export compiler and linker flags for replacement in Makefile
AC_SUBST(GPHOTO2_INCS)
AC_SUBST(GPHOTO2_LIBS)


# Display results of configuration
gphoto2_libraries_result="$gphoto2_libraries"
gphoto2_includes_result="$gphoto2_includes"

test "$gphoto2_libraries" = "" && gphoto2_libraries_result="in default path"
test "$gphoto2_includes" = "" && gphoto2_includes_result="in default path"

test "$gphoto2_libraries" = "none" && gphoto2_libraries_result="(none)"
test "$gphoto2_includes" = "none" && gphoto2_includes_result="(none)"

AC_MSG_RESULT(
  [gphoto2 libraries $gphoto2_libraries_result, gphoto2 headers $gphoto2_includes_result])

]) dnl end of KDE_FIND_GPHOTO2 definition

KDE_FIND_GPHOTO2
if test "$with_kamera" = "no"; then
dnl  AC_MSG_WARN([You need to install gphoto 2.0 (or later), e.g. http://gphoto.net/dist/gphoto2-2.0.tar.gz if your distributor doesn't have a package])
  DO_NOT_COMPILE="$DO_NOT_COMPILE kamera"
fi