dnl BEGIN inline of gpgme.m4 from gpgme 0.4.4
dnl  remaned from AM_PATH_GPGME to TDEPIM_PATH_GPGME and inlined here
dnl  to not require aclocal fiddling...

dnl Autoconf macros for libgpgme
dnl Id: gpgme.m4,v 1.6 2003/09/03 01:15:56 marcus Exp 

AC_DEFUN([_TDEPIM_PATH_GPGME_CONFIG],
[ AC_ARG_WITH(gpgme-prefix,
            AC_HELP_STRING([--with-gpgme-prefix=PFX],
                           [prefix where GPGME is installed (optional)]),
     gpgme_config_prefix="$withval", gpgme_config_prefix="")
  if test "x$gpgme_config_prefix" != x ; then
     gpgme_config_path="$gpgme_config_prefix/bin"
  else
     gpgme_config_path="$PATH:/usr/local/bin"
  fi
  AC_PATH_PROG(GPGME_CONFIG, gpgme-config, no, $gpgme_config_path)

  if test "x$GPGME_CONFIG" != "xno" ; then
      gpgme_version=`$GPGME_CONFIG --version`
      gpgme_version_major=`echo $gpgme_version | \
               sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'`
      gpgme_version_minor=`echo $gpgme_version | \
               sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'`
      gpgme_version_micro=`echo $gpgme_version | \
               sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\3/'`
  fi
])

dnl AM_PATH_GPGME([MINIMUM-VERSION,
dnl               [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]])
dnl Test for libgpgme and define GPGME_CFLAGS and GPGME_LIBS.
dnl
AC_DEFUN([TDEPIM_PATH_GPGME],
[ AC_REQUIRE([_TDEPIM_PATH_GPGME_CONFIG])dnl
  min_gpgme_version=ifelse([$1], ,0.4.2,$1)
  AC_MSG_CHECKING(for GPGME - version >= $min_gpgme_version)
  ok=no
  if test "$GPGME_CONFIG" != "no" ; then
    req_major=`echo $min_gpgme_version | \
               sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\1/'`
    req_minor=`echo $min_gpgme_version | \
               sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\2/'`
    req_micro=`echo $min_gpgme_version | \
               sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\3/'`
    if test "$gpgme_version_major" -gt "$req_major"; then
        ok=yes
    else 
        if test "$gpgme_version_major" -eq "$req_major"; then
            if test "$gpgme_version_minor" -gt "$req_minor"; then
               ok=yes
            else
               if test "$gpgme_version_minor" -eq "$req_minor"; then
                   if test "$gpgme_version_micro" -ge "$req_micro"; then
                     ok=yes
                   fi
               fi
            fi
        fi
    fi
  fi
  if test $ok = yes; then
    GPGME_CFLAGS=`$GPGME_CONFIG --cflags`
    GPGME_LIBS=`$GPGME_CONFIG --libs`
    if test "x$GPGME_LIBS" != x ; then
      if test "x`echo $GPGME_LIBS | grep lgpg-error`" = x ; then
        GPGME_LIBS="$GPGME_LIBS -lgpg-error"
      fi
    fi
    AC_MSG_RESULT([yes])
    ifelse([$2], , :, [$2])
  else
    GPGME_CFLAGS=""
    GPGME_LIBS=""
    AC_MSG_RESULT([no, will use local libgpgme-copy])
    ifelse([$3], , :, [$3])
  fi
  AC_SUBST(GPGME_CFLAGS)
  AC_SUBST(GPGME_LIBS)
])

dnl
dnl snip AM_PATH_GPGME_{PTH,PTHREAD}
dnl

dnl END inline of gpgme.m4

# The minimum required gpgme version is 0.4.5.
# It was the first useable one for C++ (class keyword) and KDE (largefile support).
# If you change this minimum version here, update also configure.in.bot

tdepim_needs_gpgme_copy=""
TDEPIM_PATH_GPGME(0.4.5,[
	AC_LANG_SAVE
	AC_LANG_C
	tdepim_gpgmepp_save_cflags="$CFLAGS"
	tdepim_gpgmepp_save_libs="$LIBS"
	CFLAGS="$GPGME_CFLAGS"
	LIBS="$GPGME_LIBS"

	AC_MSG_CHECKING([if gpgme has GPGME_KEYLIST_MODE_VALIDATE])
	AC_TRY_COMPILE([#include <gpgme.h>], [
		gpgme_keylist_mode_t mode = GPGME_KEYLIST_MODE_VALIDATE;
	], [
		AC_DEFINE(HAVE_GPGME_KEYLIST_MODE_VALIDATE, 1, [Define to 1 if your gpgme supports GPGME_KEYLIST_MODE_VALIDATE])
		AC_MSG_RESULT([yes])
	], [
		AC_MSG_RESULT([no])
	])

	AC_MSG_CHECKING([if gpgme has gpgme_cancel])
	AC_TRY_LINK([#include <gpgme.h>], [
		gpgme_ctx_t ctx = 0;
		gpgme_error_t e = gpgme_cancel( ctx );
	], [
		AC_DEFINE(HAVE_GPGME_CANCEL, 1, [Define to 1 if your gpgme supports gpgme_cancel()])
		AC_MSG_RESULT([yes])
	], [
		AC_MSG_RESULT([no])
	])

	AC_MSG_CHECKING([if gpgme has gpgme_key_t->keylist_mode])
	AC_TRY_COMPILE([#include <gpgme.h>], [
		gpgme_key_t key = 0;
		key->keylist_mode = 0;
	], [
		AC_DEFINE(HAVE_GPGME_KEY_T_KEYLIST_MODE, 1, [Define to 1 if your gpgme's gpgme_key_t has the keylist_mode member])
		AC_MSG_RESULT([yes])
	], [
		AC_MSG_RESULT([no])
	])

	AC_MSG_CHECKING([if gpgme has gpgme_decrypt_result_t->wrong_key_usage])
	AC_TRY_COMPILE([#include <gpgme.h>], [
		gpgme_decrypt_result_t res;
		unsigned int wku = res->wrong_key_usage;
	], [
		AC_DEFINE(HAVE_GPGME_WRONG_KEY_USAGE, 1, [Define to 1 if your gpgme's gpgme_decrypt_result_t has the wrong_key_usage member])
		AC_MSG_RESULT([yes])
	], [
		AC_MSG_RESULT([no])
	])

	AC_MSG_CHECKING([if gpgme has GPGME_INCLUDE_CERTS_DEFAULT])
	AC_TRY_COMPILE([#include <gpgme.h>], [
                int i = GPGME_INCLUDE_CERTS_DEFAULT;
	], [
		AC_DEFINE(HAVE_GPGME_INCLUDE_CERTS_DEFAULT, 1, [Define to 1 if your gpgme has the GPGME_INCLUDE_CERTS_DEFAULT macro])
		AC_MSG_RESULT([yes])
	], [
		AC_MSG_RESULT([no])
	])

	AC_MSG_CHECKING([if gpgme has gpgme_op_getauditlog])
	AC_TRY_LINK([#include <gpgme.h>], [
                gpgme_ctx_t ctx = 0;
                gpgme_data_t data = 0;
                unsigned int flags = 0;
                gpgme_error_t e = gpgme_op_getauditlog( ctx, data, flags );
	], [
		AC_DEFINE(HAVE_GPGME_OP_GETAUDITLOG, 1, [Define to 1 if your gpgme supports gpgme_op_getauditlog])
		AC_MSG_RESULT([yes])
	], [
		AC_MSG_RESULT([no])
	])

	CFLAGS="$tdepim_gpgmepp_save_cflags"
	LIBS="$tdepim_gpgmepp_save_libs"
	AC_LANG_RESTORE
	
],[
	tdepim_needs_gpgme_copy="true"
	GPGME_CFLAGS='-I$(top_srcdir)/libtdenetwork/libgpgme-copy/gpgme -I$(top_srcdir)/libtdenetwork/libgpg-error-copy -I$(top_builddir)/libtdenetwork/libgpg-error-copy'
	GPGME_LIBS='$(top_builddir)/libtdenetwork/libgpgme-copy/gpgme/libgpgme.la'
	GPGME_LIBS_DEP='$(GPGME_LIBS)'
	# All of this is in gpgme-copy
	AC_DEFINE(HAVE_GPGME_KEYLIST_MODE_VALIDATE, 1, [Define to 1 if your gpgme supports GPGME_KEYLIST_MODE_VALIDATE])
	AC_DEFINE(HAVE_GPGME_KEY_T_KEYLIST_MODE, 1, [Define to 1 if your gpgme's gpgme_key_t has the keylist_mode member])
	AC_DEFINE(HAVE_GPGME_CANCEL, 1, [Define to 1 if your gpgme supports gpgme_cancel()])
])

AC_SUBST(GPGME_LIBS_DEP)

AM_CONDITIONAL(needs_gpgme_copy, test -n "$tdepim_needs_gpgme_copy")

dnl Always true - either from the local copy or from the system lib.
dnl Still used in gpgmepp/gpgmefw.h for some reason.
AC_DEFINE(HAVE_GPGME_0_4_BRANCH, 1, [Always set since we use gpgme-copy if gpgme isn't available])

dnl Used by Makefile.am
AC_PROG_AWK

