# first check for qmake-qt4, because qmake may point to qmake-qt3.
AC_CHECK_PROGS([QMAKE], [qmake-qt4 qmake])

if test "x$QMAKE" != 'x'
then
  AC_PATH_PROG([KDE4_CONFIG], [kde4-config], [no])

  if test "x$KDE4_CONFIG" != 'xno'
  then
    KDEKWALLETFOUND=yes
    if ! test "$KDE_KWALLET_CFLAGS"; then
      KDE_KWALLET_CFLAGS="-I`$KDE4_CONFIG --path include` -I`$KDE4_CONFIG --path include`/KDE `${PKG_CONFIG} --cflags QtDBus QtCore`"
    fi
    if ! test "$KDE_KWALLET_LIBS"; then
      KDE_KWALLET_LIBS="-lkdeui -lkdecore -L`kde4-config --install lib` `${PKG_CONFIG} --libs QtDBus QtCore`"
    fi
    AC_LANG_PUSH(C++)
    old_CPPFLAGS="$CPPFLAGS"
    CPPFLAGS="$CPPFLAGS $KDE_KWALLET_CFLAGS"
    AC_CHECK_HEADERS(kwallet.h, [], [KDEKWALLETFOUND=no])
    CPPFLAGS="$old_CPPFLAGS"
    AC_LANG_POP(C++)
  else
    KDEKWALLETFOUND=no
  fi
else
  KDEKWALLETFOUND=no
fi

# In contrast to the GNOME KEYRING, the KWallet is
# currently considered optional. "configure" will never enable
# by default, because that is a change that might not be
# expected by traditional users.
AC_ARG_ENABLE(kwallet,
              AS_HELP_STRING([--enable-kwallet], [enable access to KWallet]),
              [use_kde_kwallet="$enableval"
              test $KDEKWALLETFOUND = "yes" || test "$use_kde_kwallet" = "no" || AC_MSG_ERROR([kwallet.pc not found. Install it to compile with the KWallet enabled.])],
              [use_kde_kwallet="no"])

if test "$use_kde_kwallet" = "yes"; then
    have_keyring=yes
    # conditional compilation in preprocessor
    AC_DEFINE(USE_KDE_KWALLET, 1, [KWallet available])
    # link into static executables, similar to a SyncSource
    SYNCSOURCES="$SYNCSOURCES src/backends/kde/platformkde.la"
    # TODO: KWallet needs Qt. Enable the Qt check in
    # configure-post.in, otherwise it fails to compiler
    # when none of the backends ask for Qt.
else
    # avoid unneeded dependencies on KWallet
    KDE_KWALLET_CFLAGS=
    KDE_KWALLET_LIBS=
fi
AC_SUBST(KDE_KWALLET_LIBS)
AC_SUBST(KDE_KWALLET_CFLAGS)

# conditional compilation in make
AM_CONDITIONAL([USE_KDE_KWALLET], [test "$use_kde_kwallet" = "yes"])
