dnl -*- mode: m4 -*-
dnl follow section 5.3 of Autoconf, Automake book.
dnl
dnl 1. Boilerplate.
dnl
AC_PREREQ(2.53)
AC_INIT(src/balsa-app.h)
dnl ###########################################################################
dnl Boilerplace and versioning
dnl ###########################################################################

SCROLLKEEPER_REQUIRED=0.1.4
BALSA_MAJOR=2
BALSA_REVISION=3
BALSA_PATCHLEVEL=13
BALSA_VERSION="$BALSA_MAJOR.$BALSA_REVISION.${BALSA_PATCHLEVEL}"

AC_SUBST(BALSA_MAJOR)
AC_SUBST(BALSA_REVISION)
AC_SUBST(BALSA_PATCHLEVEL)
AC_SUBST(BALSA_VERSION)

AM_INIT_AUTOMAKE(balsa, "$BALSA_MAJOR.$BALSA_REVISION.${BALSA_PATCHLEVEL}")
AC_CONFIG_SRCDIR([src/main.c])
AM_CONFIG_HEADER([config.h])

AC_DEFINE_UNQUOTED(BALSA_MAJOR, $BALSA_MAJOR,
                   [Define to the Balsa major version])
AC_DEFINE_UNQUOTED(BALSA_REVISION, $BALSA_REVISION,
                   [Define to the Balsa revision])
AC_DEFINE_UNQUOTED(BALSA_PATCHLEVEL, $BALSA_PATCHLEVEL,
                   [Define to the Balsa patch level])
AC_DEFINE_UNQUOTED(BALSA_VERSION, "$BALSA_VERSION",
                   [Define to the Balsa version])

dnl #####################################################################
dnl 2. Options
dnl #####################################################################
AC_ARG_WITH(iconv,      
        AC_HELP_STRING([--with-iconv=DIR],
                [Where libiconv is installed]),
           [if test "$with_iconv" != yes -a "$with_iconv" != no ; then
                   CPPFLAGS="-I${with_iconv}/include $CPPFLAGS"
                   LDFLAGS="-liconv -L${with_iconv}/lib $LDFLAGS"
            fi])
AC_ARG_WITH([gnome],
        AC_HELP_STRING([--without-gnome],
                [Use GNOME if available (default=yes)]),[
	with_gnome=$withval
],[
	with_gnome=yes
])

if test x$with_gnome != xno; then
   gnome_extras="libgnome-2.0 libgnomeui-2.0 
   gnome-vfs-2.0 gnome-vfs-module-2.0 libbonobo-2.0 
   libgnomeprint-2.2 >= 2.1.4 libgnomeprintui-2.2 >= 2.1.4"
   AC_DEFINE(HAVE_GNOME,1,[Defined when gnome libs are available.])
else
   gnome_extras=""
fi
AM_CONDITIONAL([BUILD_WITH_GNOME], [test $with_gnome = "yes"])

dnl ###########################################################################
dnl Configure locking
dnl ###########################################################################

AC_ARG_ENABLE(flock,
        AC_HELP_STRING([--enable-flock],
                       [Do NOT use flock() to lock files (default=no)]),
	[if test x$enableval = xno; then balsa_cv_flock=no; fi])

if test x$balsa_cv_flock = xyes; then
	AC_DEFINE(USE_FLOCK,1,[Define if flock locking should be used.])
fi

balsa_cv_fcntl=yes
AC_ARG_ENABLE(fcntl,
        AC_HELP_STRING([--disable-fcntl],
                       [Use fcntl() to lock files (default=yes)]),
	[if test x$enableval = no; then balsa_cv_fcntl=no; fi])
if test x$balsa_cv_fcntl = xyes; then
	AC_DEFINE(USE_FCNTL,1,[Define if fcntl locking should be used.])
fi

AC_ARG_ENABLE(threads,
        AC_HELP_STRING([--enable-threads],
                       [Use threading for mail retrieval (default=yes)]),[
	use_threads=$enableval
],[
	use_threads=yes
])

if test x"$use_threads" = xyes; then 
        AC_DEFINE(BALSA_USE_THREADS, 1,[Defined when balsa is to use threads.])
fi

AC_ARG_WITH([ldap],
        AC_HELP_STRING([--with-ldap=DIR],
                [Use OpenLDAP if available (default=no)]),[
	with_ldap=$withval
],[
	with_ldap=no
])

AC_ARG_WITH([gpgme],
   AC_HELP_STRING([--with-gpgme=gpgme-config],
                  [build with gpgme/GnuPG support (default=no, path to gpgme-config optional)]),
	          [ gpgmecfg=$withval ], [ gpgmecfg=no ])
AC_ARG_ENABLE([smime],
   AC_HELP_STRING([--enable-smime],
                  [include S/MIME support (needs gpgme and gpgsm, experimental) (default=no)]),
	          [ have_smime=$enableval ], [ have_smime=no ])

AC_ARG_WITH([gtkhtml],
              AC_HELP_STRING([--with-gtkhtml],
                             [Preferred GtkHTML version, 2 or 3 (default=3)]),[
	use_gtkhtml=$withval
],[
	use_gtkhtml=3
])

AC_ARG_WITH([gtkspell],
        AC_HELP_STRING([--with-gtkspell],
                [Use GtkSpell if available (default=no)]),[
	with_gtkspell=$withval
],[
	with_gtkspell=no
])

AC_ARG_WITH([gtksourceview],
        AC_HELP_STRING([--with-gtksourceview],
                [Use GtkSourceView if available (default=no)]),[
	with_gtksourceview=$withval
],[
	with_gtksourceview=no
])

AC_ARG_WITH([sqlite],
        AC_HELP_STRING([--with-sqlite=DIR],
                [Use SQLite for GPE address books (default=no)]),[
	with_sqlite=$withval
],[
	with_sqlite=no
])

AC_ARG_ENABLE([pcre], 
   AC_HELP_STRING([--enable-pcre],
                  [Use Perl-compatible regular expressions if available (default=yes)]),
                  [ check_pcre=$enableval],[ check_pcre=yes ])


AC_ARG_WITH(esmtp,
   AC_HELP_STRING([--with-esmtp=DIR],
                  [Enable libESMTP (default=yes)]),
                  [ with_esmtp=$withval ],[ with_esmtp=yes ])

AC_ARG_WITH(ssl,
   AC_HELP_STRING([--with-ssl],
                  [Enable SSL (default=no)]),
                  [ with_ssl=$withval ],[ with_ssl=no ])

AC_ARG_WITH(gss,
   AC_HELP_STRING([--with-gss],
                  [Enable GSS (default=no)]),
                  [with_gss=$withval],[with_gss=no])

AC_ARG_WITH(compface,
   AC_HELP_STRING([--with-compface],
                  [Enable Compface (default=no)]),
                  [with_compface=$withval],[with_compface=no])

AC_ARG_ENABLE(more-warnings,
   AC_HELP_STRING([--enable-more-warnings],
                  [Enable maximum compiler warnings (default=yes)]),
                  [set_more_warnings="$enableval"], [set_more_warnings=yes])

dnl ##########################################################################
dnl Enable touchscreen optimized UI?
dnl ##########################################################################

AC_ARG_ENABLE(touch-ui,
        AC_HELP_STRING([--enable-touch-ui],
                [Enable Touchscreen UI mode (default=no)]),
    [AC_DEFINE(ENABLE_TOUCH_UI,1,[Defined when touchscreen optimized UI is to be enabled.])])

dnl #####################################################################
dnl 3. Programs: compilers and their options.
dnl #####################################################################
AC_PROG_CC
AM_PROG_LIBTOOL
AC_PROG_LN_S

# scrollkeeper.
#
if test x$with_gnome != xno; then
AC_SUBST(SCROLLKEEPER_REQUIRED)
AC_PATH_PROG(SCROLLKEEPER_CONFIG, scrollkeeper-config,no)
if test x$SCROLLKEEPER_CONFIG = xno; then
      AC_MSG_ERROR([Couldn't find scrollkeeper-config, please install the scrollkeeper package])
fi
fi

# glib-genmarshal
#
AC_SUBST(GLIB_GENMARSHAL)
GLIB_GENMARSHAL=`pkg-config --variable=glib_genmarshal glib-2.0`

# gettext
#
ALL_LINGUAS="am az bg ca cs da de el en_CA en_GB es et fi fr ga he hi hr hu id it ja ko lt lv ml ms nb ne nl nn pa pl pt pt_BR ro ru rw sk sl sq sr sr@Latn sv tr uk vi wa zh_CN zh_HK zh_TW"
GETTEXT_PACKAGE=balsa
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE",
        [The gettext package name.])

AC_PROG_INTLTOOL
AM_GLIB_GNU_GETTEXT

# sendmail
#
if test x"$with_esmtp" = xno; then
AC_PATH_PROG(SENDMAIL, sendmail, no, $PATH:/usr/sbin:/usr/lib)
AC_DEFINE_UNQUOTED(SENDMAIL, "$ac_cv_path_SENDMAIL",
		   [Path to sendmail program.])
fi

dnl #####################################################################
dnl 4. Libraries.
dnl #####################################################################

PKG_CHECK_MODULES(BALSA, [
glib-2.0
gtk+-2.0 >= 2.4
gmime-2.0 >= 2.1.9
$gnome_extras
])

PKG_CHECK_MODULES(BALSA_AB, [
   libgnome-2.0 libgnomeui-2.0 
   glib-2.0
   gtk+-2.0 >= 2.4
   gmime-2.0 >= 2.1.9
])

dnl ##########################################################################
dnl Check versions
dnl ##########################################################################

if test x$with_gnome != xno; then
  if $PKG_CONFIG --atleast-version=2.9.0 gnome-vfs-2.0 ; then
    AC_DEFINE(HAVE_GNOME_VFS29,1,
	      [Defined when gnome-vfs-2.0 version is at least 2.9.0])
  fi
fi

# CORBA interface.
#
                                                                        
ORBIT_IDL="`$PKG_CONFIG --variable=orbit_idl ORBit-2.0`"
AC_SUBST(ORBIT_IDL)

LIBBONOBO_IDL="`$PKG_CONFIG --variable=idldir libbonobo-2.0`"
AC_SUBST(LIBBONOBO_IDL)
 
BONOBO_ACTIVATION_IDL="`$PKG_CONFIG --variable=idldir bonobo-activation-2.0`"
AC_SUBST(BONOBO_ACTIVATION_IDL)

AC_SUBST(BALSA_CFLAGS)
AC_SUBST(BALSA_LIBS)
AC_SUBST(BALSA_AB_LIBS)

# GtkHtml library.
#
if test x"$use_gtkhtml" != xno ; then
    case "$use_gtkhtml" in
     2) check_pkgs="2.0 3.8 3.6 3.1 3.0";;
     *) check_pkgs="3.8 3.6 3.1 3.0 2.0";;
    esac
    for i in $check_pkgs; do
    if $PKG_CONFIG --exists libgtkhtml-$i ; then
	PKG_CHECK_MODULES(HTML, [ libgtkhtml-$i ])
        if test x"$i" = x2.0; then
            AC_DEFINE(HAVE_GTKHTML2,1,[Defined when GtkHtml-2 can be used.])
        else
            AC_DEFINE(HAVE_GTKHTML3,1,[Defined when GtkHtml-3 can be used.])
        fi
        AC_DEFINE(HAVE_GTKHTML,1,[Defined when GtkHtml can be used.])
        BALSA_CFLAGS="$BALSA_CFLAGS $HTML_CFLAGS"
        BALSA_LIBS="$BALSA_LIBS $HTML_LIBS"
        break
    fi
    done
else
    AC_MSG_CHECKING(whether to use GtkHTML)
    AC_MSG_RESULT(disabled)
fi

# GPGME detection.
# We don't use the std macro AM_PATH_GPGME here to avoid dependencies.

AC_MSG_CHECKING(whether to build with gpgme/GnuPG support)
if test x"$gpgmecfg" = xyes ; then
	AC_MSG_RESULT(yes)
	AC_PATH_PROG(gpgmecfg, gpgme-config, no)
fi
if test x"$gpgmecfg" != xno ; then
	AC_MSG_CHECKING([gpgme library version])
	gpgmever=`$gpgmecfg --version`
	AC_MSG_RESULT($gpgmever)
        gpgme_ve=`echo $gpgmever|sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'`
        gpgme_ma=`echo $gpgmever|sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'`
        gpgme_mi=`echo $gpgmever|sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'`
	if test $gpgme_ve -eq 0 ; then
		if test $gpgme_ma -lt 9 ; then
			gpgmecfg=no
			have_smime=no
		fi
	fi
	if test x"$gpgmecfg" != xno ; then
		if test x"$use_threads" = xyes; then
			BALSA_LIBS="$BALSA_LIBS `$gpgmecfg --thread=pthread --libs`"
			if test x"$?" != x0 ; then
				AC_MSG_ERROR([*** You enabled gpgme and pthread support, but your gpgme installation does not support threads.])
			fi
		else
			BALSA_LIBS="$BALSA_LIBS `$gpgmecfg --libs`"
		fi
		AC_DEFINE(HAVE_GPGME,1,[Defined when gpgme is available.])
		BALSA_CFLAGS="$BALSA_CFLAGS `$gpgmecfg --cflags`"
		gpgmecfg=yes
		AC_PATH_PROG(gpg_path, gpg)
		if test x"$gpg_path" != "x" ; then
			AC_DEFINE(HAVE_GPG,1,[Defined when gpg is available.])
			AC_DEFINE_UNQUOTED(GPG_PATH,["$gpg_path"],[Path of gpg.])
		fi
		AC_MSG_CHECKING(whether to include experimental S/MIME support)
		if test x"$have_smime" = xyes ; then
			AC_MSG_RESULT([yes])
			AC_DEFINE(HAVE_SMIME,1,[Defined when supporting S/MIME])
		else
			AC_MSG_RESULT([no])
		fi
	else
		AC_MSG_WARN([sorry, you need at least gpgme version 0.9.0])
	fi
else
	AC_MSG_RESULT(no)
	have_smime=no
fi
AM_CONDITIONAL([BUILD_WITH_GPGME], [test $gpgmecfg = "yes"])
AM_CONDITIONAL([BUILD_WITH_SMIME], [test $have_smime = "yes"])


# OpenLDAP configuration.
#
AC_MSG_CHECKING(whether to use LDAP)
if test x"$with_ldap" != xno ; then
	AC_MSG_RESULT([yes])
	AC_CHECK_LIB(ldap, ldap_search, AC_DEFINE(ENABLE_LDAP, 1,
                 [Defined when the LDAP support is to be compiled in.])
              LIBS="-lldap -llber -lresolv $LIBS", AC_MSG_ERROR([*** You enabled LDAP but ldap library is not found.]), -llber -lresolv)
        AC_CHECK_LIB(ldap, ldap_start_tls_s,
                     AC_DEFINE(HAVE_LDAP_TLS,1,
                               [Defined when LDAP lib supports TLS]))
    if test "$with_ldap" != yes; then
       LIBS="$LIBS -L$with_ldap/lib"
       CPPFLAGS="$CPPFLAGS -I$with_ldap/include"
    fi
else
	AC_MSG_RESULT([no])
fi

# SQLite configuration.
#
AC_MSG_CHECKING(whether to use SQLite)
if test x$with_sqlite != xno ; then
    AC_MSG_RESULT([yes])
    if $PKG_CONFIG --exists sqlite3 ; then
        PKG_CHECK_MODULES(SQLITE, [sqlite3])
        AC_DEFINE(HAVE_SQLITE3,1,[If defined, use sqlite3 api.])
    else
        PKG_CHECK_MODULES(SQLITE, [sqlite >= 2.8])
    fi
    AC_DEFINE(HAVE_SQLITE,1,[If defined, enable GPE address book support.])
    BALSA_CFLAGS="$BALSA_CFLAGS $SQLITE_CFLAGS"
    BALSA_LIBS="$BALSA_LIBS $SQLITE_LIBS"
    BALSA_AB_LIBS="$BALSA_AB_LIBS $SQLITE_LIBS"
else
    AC_MSG_RESULT([no])
fi

# LibESMTP configuration.
#
AC_MSG_CHECKING([whether to build ESMTP support])
if test "x$with_esmtp" != xno  ; then
    AC_MSG_RESULT([yes])
    test "$with_esmtp" != yes && LIBS="$LIBS -L$with_esmtp/lib"
    AC_CHECK_LIB(esmtp, smtp_start_session,BALSA_LIBS="$BALSA_LIBS -lesmtp",
	AC_MSG_ERROR([*** You enabled ESMTP but esmtp library is not found.]),
                 -lpthread)
    AC_CHECK_LIB(esmtp, smtp_option_require_all_recipients, 
                 AC_DEFINE(ENABLE_ESMTP, 1, 
                           [Defined when ESTMP support is to be enabled.]),
	AC_MSG_ERROR([*** You enabled ESMTP but esmtp library doesn't support
	    'require_all_recipients'. Please get a Balsa aware
	    libESMTP from http://balsa.gnome.org/main.html or compile
	    libESMTP with './configure --enable-require-all-recipients'.]),
                 -lpthread)
    AC_CHECK_LIB(esmtp, smtp_starttls_set_password_cb,
	AC_DEFINE(HAVE_SMTP_TLS_CLIENT_CERTIFICATE,1,
	         [Defined when libESMTP has TLS support.]),
		 -lpthread)
    test "$with_esmtp" != yes && CPPFLAGS="$CPPFLAGS -I$with_esmtp/include"
else
    AC_MSG_RESULT([no])
fi

# PCRE configuration.
#

AC_MSG_CHECKING(whether to use PCRE)
if test x"$check_pcre" = xyes ; then
	if pcre-config --libs-posix > /dev/null 2>&1; then
        	vers=`pcre-config --version`
	        case $vers in
                       2.*) have_pcre=no ;;
                       *)   have_pcre=yes ;;
        	esac
	else
        	have_pcre=no
	fi

	if test x"$have_pcre" = xyes ; then
	       	AC_MSG_RESULT(yes)
		BALSA_CFLAGS="$BALSA_CFLAGS `pcre-config --cflags-posix`"
                PCRE_LIBS="`pcre-config --libs-posix`"
                AC_SUBST(PCRE_LIBS)
		BALSA_LIBS="$BALSA_LIBS $PCRE_LIBS"
		AC_DEFINE(HAVE_PCRE,1,
                          [Defined when PCRE library is available.])
	else
	        AC_MSG_RESULT(not found)
	fi
else
	have_pcre=no
	AC_MSG_RESULT(no)
fi

# OpenSSL configuration.
#

AC_MSG_CHECKING([whether to build SSL support])
if test "x$with_ssl" != xno  ; then
# openssl >=0.9.7 is pkg-config compatible.
   if pkg-config openssl >/dev/null 2>&1; then
      BALSA_LIBS="$BALSA_LIBS `pkg-config --libs openssl`"
      BALSA_CFLAGS="`pkg-config --cflags-only-I openssl` $BALSA_CFLAGS"
      AC_MSG_RESULT(yes)
      AC_DEFINE(USE_SSL,1,[Defined when SSL support is to be compiled.])
      AC_DEFINE(USE_TLS,USE_SSL,[Defined when TLS is to be enabled.])
   else
      if test -d $with_ssl && test -f "$with_ssl/include/openssl/ssl.h" ; then
         ssldir=$with_ssl
      else
           AC_TRY_COMPILE([#include <openssl/ssl.h>], ,
		          ssldir=default,
			  ssldir="")
	   if test "x$ssldir" = x ; then		
              for prefixdir in /usr /usr/local /usr/lib /usr/pkg /var /opt; do
                  for dir in $prefixdir $prefixdir/openssl $prefixdir/ssl; do
                      if test -f "$dir/include/openssl/ssl.h"; then
                         ssldir=$dir
                      fi
                  done
              done
	   fi
       fi
       if test -z "$ssldir"; then
          AC_MSG_ERROR([SSL requested but no SSL headers found.])
       else
          AC_MSG_RESULT(yes)
          BALSA_LIBS="$BALSA_LIBS -lssl -lcrypto"
	  if test "x$ssldir" != xdefault ; then
		BALSA_CFLAGS="-I${ssldir}/include $BALSA_CFLAGS"
	  fi
	  ac_cv_path_openssl=$ssldir
	  AC_DEFINE(USE_SSL,1,[Defined when SSL support is to be compiled.])
          AC_DEFINE(USE_TLS,USE_SSL,[Defined when TLS is to be enabled.])
       fi
   fi
else
   AC_MSG_RESULT(no)
fi

# KRB5/GSSAPI configuration.
#
if test "x$with_gss" != xno  ; then
   AC_PATH_PROG(KRB5_CONFIG, krb5-config,no,[${PATH}:/usr/kerberos/bin:/usr/lib/mit/bin])
   if test "x$KRB5_CONFIG" != xno ; then 
      AC_DEFINE(HAVE_GSSAPI,1,[Defined when GSSAPI support is to be compiled.])
      BALSA_CFLAGS="$BALSA_CFLAGS `$KRB5_CONFIG --cflags gssapi`"
      BALSA_LIBS="$BALSA_LIBS `$KRB5_CONFIG --libs gssapi`"
      SAVE_CFLAGS="$CFLAGS"; CFLAGS="$BALSA_CFLAGS"
      AC_CHECK_HEADER([gssapi.h],
                      [AC_DEFINE([HAVE_HEIMDAL],1,[Defined for Heimdal.])],
        [AC_CHECK_HEADER([gssapi/gssapi.h],,
                         AC_MSG_ERROR([gssapi.h not found]))])
      CFLAGS="$SAVE_CFLAGS"
   else
      AC_MSG_ERROR([GSS requested but krb5-config not found. Install krb5-devel/heimdal-devel])
   fi
fi

# Compface configuration
#
AC_MSG_CHECKING([whether to build Compface support])
if test "x$with_compface" != xno  ; then
    AC_MSG_RESULT([yes])
    test "$with_compface" != yes && BALSA_LIBS="$BALSA_LIBS -L$with_compface/lib"
    AC_CHECK_LIB(compface, uncompface, BALSA_LIBS="$BALSA_LIBS -lcompface",
	AC_MSG_ERROR([*** You enabled Compface but the library is not found.]),)
    AC_DEFINE(HAVE_COMPFACE, 1, 
              [Defined when Compface support is requested.])
    test "$with_compface" != yes && CPPFLAGS="$CPPFLAGS -I$with_compface/include"
else
    AC_MSG_RESULT([no])
fi

# GtkSourceView configuration
#
if test x$with_gtksourceview != xno; then
    PKG_CHECK_MODULES(SOURCEVIEW, [ gtksourceview-1.0 ])
    AC_DEFINE(HAVE_GTKSOURCEVIEW,1,[Defined when GtkSourceView can be used.])
    BALSA_CFLAGS="$BALSA_CFLAGS $SOURCEVIEW_CFLAGS"
    BALSA_LIBS="$BALSA_LIBS $SOURCEVIEW_LIBS"
fi

dnl #####################################################################
dnl 5. Headers.
dnl #####################################################################

# Spell check detection.
#
AC_TRY_LINK( [#include <iconv.h>], 
             [iconv_open("UTF-8", "ISO-8859-2");],,
             [AC_MSG_ERROR([*** You need iconv for balsa.])])

if test x$with_gtkspell != xno; then
    PKG_CHECK_MODULES(SPELL, [ gtkspell-2.0 ])
    AC_DEFINE(HAVE_GTKSPELL,1,[Defined when GtkSpell can be used.])
    BALSA_CFLAGS="$BALSA_CFLAGS $SPELL_CFLAGS"
    BALSA_LIBS="$BALSA_LIBS $SPELL_LIBS"
else
    AC_CHECK_LIB(aspell, new_aspell_speller,,[
        AC_CHECK_LIB(pspell, new_pspell_manager,,[
            AC_MSG_ERROR([*** Neither aspell >=0.50 (recommended) nor pspell (deprecated) library was found. Please check http://balsa.gnome.org for information on the required files. Alternatively, check config.log for clues.])
        ], -lstdc++ -lpspell-modules)
    ], -lstdc++)
    AC_CHECK_HEADER(pspell/pspell.h,,
                    AC_MSG_ERROR([** aspell/pspell library found but not related header files. This probably means corresponding aspell-devel package is not installed ]))
fi
AM_CONDITIONAL([BUILD_WITH_GTKSPELL], [test $with_gtkspell = "yes"])

dnl #####################################################################
dnl 6. Typedefs, structures and compiler characteristics.
dnl #####################################################################
AC_C_CONST
AC_STDC_HEADERS


# more warnings.
#

if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
	for option in -Wsign-compare -Wpointer-arith -Wnested-externs \
            -Wchar-subscripts -Wuninitialized -Wmissing-prototypes    \
            -Wreturn-type -Wdeclaration-after-statement               \
            -D_FORTIFY_SOURCE=2 ; do
                SAVE_CFLAGS="$CFLAGS"
                CFLAGS="$CFLAGS $option"
                AC_MSG_CHECKING([whether gcc understands $option])
                AC_TRY_COMPILE([], [],
                        has_option=yes,
                        has_option=no,)
                if test $has_option = no; then
                  CFLAGS="$SAVE_CFLAGS"
                fi
                AC_MSG_RESULT($has_option)
                unset has_option
                unset SAVE_CFLAGS
        done
        unset option
fi

# extra warnings for CVS.
#

if test -d "${srcdir}/CVS" ; then
	BALSA_FROM_CVS=yes
	BALSA_CFLAGS="$BALSA_CFLAGS -Wall -Werror"
else
	BALSA_FROM_CVS=no
fi

AC_MSG_CHECKING([for CVS information])
AC_MSG_RESULT($BALSA_FROM_CVS)

dnl #####################################################################
dnl 7. extra functions.
dnl #####################################################################
AC_CHECK_FUNCS(getaddrinfo getnameinfo gai_strerror)

# isblank()
AC_MSG_CHECKING([for isblank])
AC_TRY_LINK( [#include <ctype.h>], [return isblank('a');],
	     AC_MSG_RESULT(yes),
	     [AC_MSG_RESULT(no)
	      AC_DEFINE(isblank,isspace,
                        [Redefined to isspace if isblank is not available.])] )


dnl ###########################################################################
dnl Remember our prefixes
dnl   The way prefix et al are defined makes us jump through some hoops.
dnl ###########################################################################

if test "x$prefix" = "xNONE"; then
	prefix=$ac_default_prefix
fi
dnl datadir='${prefix}/share', so we must eval it or something. This works
BALSA_STD_PREFIX=`eval "echo ${prefix}"`
BALSA_DATA_PREFIX=`eval "echo ${datadir}/balsa"`
AC_SUBST(BALSA_DATA_PREFIX)

dnl Consolidate them!
AC_PROG_AWK
BALSA_COMMON_PREFIXES=`sort<<EOF|uniq|${AWK} '{if(l!~/^$/)print l ",";l=$0}END{print l}'
"${BALSA_STD_PREFIX}"
"${BALSA_DATA_PREFIX}"
EOF
`

dnl Better way to do this?
AC_DEFINE_UNQUOTED( BALSA_STD_PREFIX, "$BALSA_STD_PREFIX",
                   [Directory for all balsa files.] )
AC_DEFINE_UNQUOTED( BALSA_DATA_PREFIX, "$BALSA_DATA_PREFIX",
	           [Directory for storing balsa data files.] )
AC_DEFINE_UNQUOTED( BALSA_COMMON_PREFIXES, `echo $BALSA_COMMON_PREFIXES`,
	           [Common directories for storing balsa icons.] ) 
dnl bleah


dnl ###########################################################################
dnl determine the base folder for installing the extra icons
dnl ###########################################################################

AC_ARG_ENABLE(extra-mimeicons,
        AC_HELP_STRING([--enable-extra-mimeicons],
                       [Install extra MIME icons (needs write access to the gtk+ icon folder, default=yes)]),[
	install_mimeicons=$enableval
],[
	install_mimeicons=yes
])
AM_CONDITIONAL([INSTALL_MIME_ICONS], [test $install_mimeicons = "yes"])
HICOLOR_ICON_DIR=`$PKG_CONFIG --variable=prefix gtk+-2.0`/share/icons/hicolor
AC_SUBST(HICOLOR_ICON_DIR)
dnl however, we do not mess with the icon caches - it messes up more
dnl things that it fixes, for eg. user installs, package builds etc.
dnl AC_PATH_PROG(GTK_UPDATE_ICON_CACHE, gtk-update-icon-cache)

dnl #####################################################################
dnl 8. Output
dnl #####################################################################

AC_OUTPUT([
Makefile
balsa.1
balsa.spec
po/Makefile.in
sounds/Makefile
images/Makefile
images/mimetypes/Makefile
images/16x16/Makefile
images/24x24/Makefile
doc/Makefile
doc/C/Makefile
libbalsa/Makefile
libbalsa/imap/Makefile
libinit_balsa/Makefile
src/Makefile
])

dnl ##########################################################################
dnl Summary
dnl ##########################################################################

echo ""
echo "================ Final configuration ==================="
echo "    Installing into prefix: $prefix"
echo "      Using multithreading: $use_threads"
echo "   Enable compile warnings: $set_more_warnings"
echo "                Use ESMTP : $with_esmtp"
echo "               Use GtkHTML: $use_gtkhtml"
echo "                 Use GPGME: $gpgmecfg"
echo "                  Use LDAP: $with_ldap"
echo "                   Use GSS: $with_gss"
echo "                Use SQLite: $with_sqlite"
echo "                   Use SSL: $with_ssl"
echo "              Use GtkSpell: $with_gtkspell"
echo "         Use GtkSourceView: $with_gtksourceview"
echo "              Use Compface: $with_compface"
echo "  Install extra MIME icons: $install_mimeicons"
dnl echo "                  Use SASL: $need_sasl"
echo ""

if test x"$BALSA_FROM_CVS" = xyes ; then
	echo "============================== NOTICE ================================"
	echo "                 You are using Balsa from CVS source."
	echo "  The program is likely to be unstable, contain incomplete features,"
	echo "or just plain not work. Use it at your own risk. You have been warned."
	echo "======================================================================"
	echo ""
fi
