#MIN_CONFIG(3.3.5)

AM_INIT_AUTOMAKE(klamav, 0.1)
AC_C_BIGENDIAN
AC_CHECK_KDEMAXPATHLEN

###############################################################################
# BEGIN PKG-CONFIG CHECK
###############################################################################

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

###############################################################################
# END PKG-CONFIG CHECK
###############################################################################

###############################################################################
# BEGIN SQLITE CHECK
###############################################################################

LIB_SQLITE=""

AC_ARG_WITH(included-sqlite,
    AC_HELP_STRING([--without-included-sqlite],[build KlamAV using system sqlite library]),
    [included_sqlite=$withval],
    [included_sqlite=yes]
)

if test x$included_sqlite = xno; then
    if test x$PKGCONFIGFOUND = xyes; then
        PKG_CHECK_MODULES(SQLITE, sqlite3 >= 3.0, have_sqlite=yes,have_sqlite=no)

        if test x$have_sqlite = xyes; then
            LIB_SQLITE=`pkg-config --libs sqlite3`
        else
            AC_MSG_ERROR(Can't find sqlite database library. Check your installation of sqlite or use included sqlite.)
        fi
    fi
fi

AC_SUBST(LIB_SQLITE)
AM_CONDITIONAL(with_included_sqlite, [test x$included_sqlite = xyes])

# Determine pointer size for sqlite
KDE_CHECK_TYPES
AC_DEFINE(SQLITE_PTR_SZ, SIZEOF_CHAR_P, [Determine pointer size for SQLite])

###############################################################################
# END SQLITE CHECK
###############################################################################


# ---- Taken from KOffice ----------------------
# --- Check for KDE 3.2 or 3.3 or 3.4 or 3.5 ---

AC_MSG_CHECKING([for KDE version])

AC_LANG_SAVE
AC_LANG_CPLUSPLUS
kdeversion_save_CXXFLAGS="$CXXFLAGS"
kdeversion_safe_LIBS="$LIBS"
LIBS="$LIBS $X_EXTRA_LIBS"
CXXFLAGS="$CXXFLAGS $all_includes"

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 2, 90 ) )
#error KDE 3.2
#endif
],
        need_kde32_compat="no"
,
        need_kde32_compat="yes"
)

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 3, 90 ) )
#error KDE 3.3
#endif
],
        need_kde33_compat="no"
,
        need_kde33_compat="yes"
)

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 4, 90 ) )
#error KDE 3.4
#endif
],
        need_kde34_compat="no"
,
        need_kde34_compat="yes"
)

AC_COMPILE_IFELSE([
#include <kdeversion.h>
#if ! ( KDE_IS_VERSION( 3, 5, 2 ) )
#error KDE 3.5.x (x < 2)
#endif
],
        need_kde351_compat="no"
,
        need_kde351_compat="yes"
)
CXXFLAGS="$kdeversion_save_CXXFLAGS"
LIBS="$kdeversion_safe_LIBS"
AC_LANG_RESTORE

if test "$need_kde32_compat" = "yes"; then
        AC_MSG_ERROR([You have KDE 3.2.x. KlamAV needs KDE 3.5.2 or greater.])
else
        if test "$need_kde33_compat" = "yes"; then
                AC_MSG_ERROR([You have KDE 3.3.x KlamAV needs KDE 3.5.2 or greater.])
        else 
                if test "$need_kde34_compat" = "yes"; then
                        AC_MSG_ERROR([You have KDE 3.4.x KlamAV needs KDE 3.5.2 or greater.])
                else
                        if test "$need_kde351_compat" = "yes"; then
                                AC_MSG_ERROR([You have KDE 3.5.1. KlamAV needs KDE 3.5.2 or greater.])
                        else
                                AC_MSG_RESULT([KDE 3.5.x (x >=2) or SVN trunk])
                        fi
                fi
        fi
fi


# --- End KDE 3.2 check ---




AC_CHECK_LIB(clamav, cl_scanfile, [LIB_CLAM=" -lclamav";],AC_MSG_ERROR(Can't find ClamAV's libraries. Please check your installation of ClamAV.))
AC_SUBST(LIB_CLAM)

AC_CHECK_LIB(clamav, cl_free, [ tempres="no" ],tempres="yes")
if test "$tempres" = "yes"; then
  AC_DEFINE([SUPPORT_CLAMAV_V095],[],[Support ClamAV 0.95])
fi

dnl disable updates

# Whether we should use the disableupdates memory allocation debugging library.
AC_MSG_CHECKING(whether to use disable-updates (build KlamAV with GUI updates disabled ))
AC_ARG_WITH(disableupdates,
[  --with-disableupdates          build KlamAV with GUI updates disabled . ],
[if [[ "$withval" = "yes" ]]; then
  disableupdates=1
  AC_MSG_RESULT(yes)
else
  disableupdates=1
  AC_MSG_RESULT(no)
fi], [ disableupdates=0; AC_MSG_RESULT(no) ]
)

if [[ $disableupdates -eq 1 ]]; then
  AC_DEFINE(DISABLE_UPDATES, 1, "Define to disable gui updates")
fi
