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

AC_DEFUN(KDE_FIND_GPHOTO2,
[



# Clear working variables
gphoto2_includes=
gphoto2_libraries=
gpio_includes=
gpio_libraries=



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

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

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

AC_ARG_WITH(gpio-includes,
[  --with-gpio-includes=DIR    gpio include files are in DIR],
gpio_includes="$withval")

AC_ARG_WITH(gpio-libraries,
[  --with-gpio-libraries=DIR   gpio libraries are in DIR],
gpio_libraries="$withval")

AC_MSG_CHECKING(for gPhoto2)


#
# 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 gpio include files.
#
if test "$gpio_includes" = ""; then
AC_CACHE_VAL(ac_cv_gpio_includes,
[
ac_gpio_save_LIBS="$LIBS"
#
LIBS="-lgpio $LIBS"
#
ac_cv_gpio_includes="none"
AC_TRY_COMPILE([#include <gpio.h>
],[int a;],
[
# gpio.h is in the standard search path.
ac_cv_gpio_includes=
],
[
# gpio.h is not in the standard search path.
# Locate it and put its directory in `gpio_includes'
#
for dir in /usr/include /usr/local/include \
           /usr/include/gpio /usr/local/include/gpio; do
if test -f "$dir/gpio.h"; then
ac_cv_gpio_includes="$dir"
break
fi
done
])
#
LIBS="$ac_gpio_save_LIBS"
])
gpio_includes="$ac_cv_gpio_includes"
fi                                                                              



#
# Search for libgphoto2
#
if test "$gphoto2_libraries" = ""; then
AC_CACHE_VAL(ac_cv_gphoto2_libraries,
[
ac_gphoto2_save_LIBS="$LIBS"
#
LIBS="-lgphoto2 $LIBS"
#
ac_cv_gphoto2_libraries="none"
AC_TRY_LINK([#include <gphoto2.h>],[gp_init(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


#
# Search for libgpio
#
if test "$gpio_libraries" = ""; then
AC_CACHE_VAL(ac_cv_gpio_libraries,
[
ac_gpio_save_LIBS="$LIBS"
#
LIBS="-lgpio $LIBS"
#
ac_cv_gpio_libraries="none"
AC_TRY_LINK([#include <gpio.h>],[gpio_init(0);],
[
# libgpio is in the standard search path.
ac_cv_gpio_libraries=
],
[
# libgpio is not in the standard search path.
# Locate it and put its directory in `gpio_libraries'
 
for dir in /usr/lib /usr/local/lib; do
if test -d "$dir" && test "`ls $dir/libgpio.* 2> /dev/null`" != ""; then
ac_cv_gpio_libraries="$dir"
break
fi
done
])
#
LIBS="$ac_gpio_save_LIBS"
])
#
gpio_libraries="$ac_cv_gpio_libraries"
fi                                                                              



# Intialise compiler and linker flag variables for export
if test "$gphoto2_includes" = "none" -o "$gphoto2_libraries" = "none" -o \
        "$gpio_includes" = "none" -o "$gpio_libraries" = "none"; then
        with_kamera="no"
else
        with_kamera="yes"

        if test "$gpio_libraries" = ""; then
                GPIO_LIBS="-lgpio"
        else
                GPIO_LIBS="-L$gpio_libraries -lgpio"
        fi

        if test "$gphoto2_libraries" = ""; then
                GPHOTO2_LIBS="-lgphoto2"
        else
                GPHOTO2_LIBS="-L$gphoto2_libraries -lgphoto2"
        fi


        if test "$gphoto2_includes" != ""; then
                GPHOTO2_INCS="-I$gphoto2_includes"
        fi

        if test "$gpio_includes" != ""; then
                GPIO_INCS="-I$gpio_includes"
        fi

	AC_DEFINE(HAVE_GPHOTO2,1,[Define if you have gPhoto2 installed])
fi



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



# Display results of configuration
gphoto2_libraries_result="$gphoto2_libraries"
gphoto2_includes_result="$gphoto2_includes"
gpio_libraries_result="$gpio_libraries"
gpio_includes_result="$gpio_includes"

test "$gphoto2_libraries" = "" && gphoto2_libraries_result="in default path"
test "$gphoto2_includes" = "" && gphoto2_includes_result="in default path"
test "$gpio_libraries" = "" && gpio_libraries_result="in default path"
test "$gpio_includes" = "" && gpio_includes_result="in default path"

test "$gphoto2_libraries" = "none" && gphoto2_libraries_result="(none)"
test "$gphoto2_includes" = "none" && gphoto2_includes_result="(none)"
test "$gpio_libraries" = "none" && gpio_libraries_result="(none)"
test "$gpio_includes" = "none" && gpio_includes_result="(none)"

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

]) dnl end of KDE_FIND_GPHOTO2 definition

KDE_FIND_GPHOTO2
if test "$with_kamera" = "no"; then
  DO_NOT_COMPILE="$DO_NOT_COMPILE kamera"
fi
