dnl Process this file with autoconf to produce a configure script.

dnl !!! WHEN ADDING NEW CONFIGURE TESTS, PLEASE ADD CODE TO MAIN.C !!!
dnl !!! TO DUMP THEIR RESULTS WHEN MUTT -V IS CALLED            !!!

AC_INIT([mutt],[m4_esyscmd(tr -d \\n <VERSION)])
AC_CONFIG_SRCDIR(mutt.h)
AM_INIT_AUTOMAKE
AC_CONFIG_HEADERS([config.h])

AC_SUBST([CONFIG_STATUS_DEPENDENCIES], ['$(top_srcdir)/VERSION'])

AC_USE_SYSTEM_EXTENSIONS

AC_CANONICAL_HOST

AC_MSG_CHECKING([for prefix])
if test x$prefix = xNONE; then
  mutt_cv_prefix=$ac_default_prefix
else
  mutt_cv_prefix=$prefix
fi
AC_MSG_RESULT($mutt_cv_prefix)

AC_PROG_CC
if test "x$ac_cv_prog_cc_c99" = "xno"; then
  AC_MSG_ERROR(A C99 compliant compiler is required)
fi
AC_SEARCH_LIBS([strerror],[cposix])
AC_PROG_CPP
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PROG_MKDIR_P
AC_PROG_RANLIB
AC_CHECK_TOOL(AR, ar, ar)

AC_CACHE_CHECK([for GNU make],
        [mutt_cv_gnu_make_command],
        [mutt_cv_gnu_make_command="no"
  if ${MAKE:-make} --version 2> /dev/null | grep "GNU Make" 2>&1 > /dev/null ; then
          mutt_cv_gnu_make_command="yes"
  fi])
AM_CONDITIONAL(GNU_MAKE, test x$mutt_cv_gnu_make_command = xyes)

AC_C_INLINE
AC_C_CONST
AC_C_BIGENDIAN

AC_SYS_LARGEFILE
AC_FUNC_FSEEKO
AC_CHECK_SIZEOF(off_t)

AC_PATH_PROG(DBX, dbx, no)
AC_PATH_PROG(GDB, gdb, no)
AC_PATH_PROG(SDB, sdb, no)

if test $GDB != no ; then
        DEBUGGER=$GDB
elif test $DBX != no ; then
        DEBUGGER=$DBX
elif test $SDB != no ; then
        DEBUGGER=$SDB
else
        DEBUGGER=no
fi

AC_SUBST([DEBUGGER])

AH_TEMPLATE([HAVE_START_COLOR],
            [Define if you have start_color, as a function or macro.])
AH_TEMPLATE([HAVE_TYPEAHEAD],
            [Define if you have typeahead, as a function or macro.])
AH_TEMPLATE([HAVE_BKGDSET],
            [Define if you have bkgdset, as a function or macro.])
AH_TEMPLATE([HAVE_CURS_SET],
            [Define if you have curs_set, as a function or macro.])
AH_TEMPLATE([HAVE_META],
            [Define if you have meta, as a function or macro.])
AH_TEMPLATE([HAVE_USE_DEFAULT_COLORS],
            [Define if you have use_default_colors, as a function or macro.])
AH_TEMPLATE([HAVE_RESIZETERM],
            [Define if you have resizeterm, as a function or macro.])
AH_TEMPLATE([HAVE_SETCCHAR],
            [Define if you have setcchar, as a function or macro.])
AH_TEMPLATE([HAVE_BKGRNDSET],
            [Define if you have bkgrndset, as a function or macro.])
AH_TEMPLATE([HAVE_USE_TIOCTL],
            [Define if you have use_tioctl, as a function or macro.])
AH_TEMPLATE([SIG_ATOMIC_VOLATILE_T],
            [Some systems declare sig_atomic_t as volatile, some others -- no.
             This define will have value `sig_atomic_t' or
             `volatile sig_atomic_t' accordingly.])
AH_TEMPLATE([ICONV_NONTRANS],
            [Define as 1 if iconv() only converts exactly and we should treat
             all return values other than (size_t)(-1) as equivalent.])

AH_BOTTOM([/* fseeko portability defines */
#ifdef HAVE_FSEEKO
# define LOFF_T off_t
# if HAVE_C99_INTTYPES && HAVE_INTTYPES_H
#  if SIZEOF_OFF_T == 8
#   define OFF_T_FMT "%" PRId64
#  else
#   define OFF_T_FMT "%" PRId32
#  endif
# else
#  if (SIZEOF_OFF_T == 8) && (SIZEOF_LONG == 4)
#   define OFF_T_FMT "%lld"
#  else
#   define OFF_T_FMT "%ld"
#  endif
# endif
#else
# define LOFF_T long
# define fseeko fseek
# define ftello ftell
# define OFF_T_FMT "%ld"
#endif
])
MUTT_C99_INTTYPES
AC_TYPE_LONG_LONG_INT

ac_aux_path_sendmail=/usr/sbin:/usr/lib
AC_PATH_PROG(SENDMAIL, sendmail, /usr/sbin/sendmail, $PATH:$ac_aux_path_sendmail)
AC_DEFINE_UNQUOTED(SENDMAIL,"$ac_cv_path_SENDMAIL", [Where to find sendmail on your system.])

OPS='$(srcdir)/OPS'

AC_ARG_WITH(sqlite3,
        AS_HELP_STRING([--with-sqlite3@<:@=PFX@:>@],
                [Enable sqlite3 support. Required by autocrypt.]),
        [],
        [with_sqlite3=no])
if test x$with_sqlite3 != xno; then
  if test x$with_sqlite3 != xyes; then
    LDFLAGS="$LDFLAGS -L$with_sqlite3/lib"
    CPPFLAGS="$CPPFLAGS -I$with_sqlite3/include"
  fi
  saved_LIBS="$LIBS"

  AC_CHECK_LIB(sqlite3, sqlite3_open, [],
    AC_MSG_ERROR([Unable to find sqlite3 library]))
  AC_CHECK_FUNC(sqlite3_prepare_v3, [],
    AC_MSG_ERROR([sqlite3 version 3.20 or greater is required]))

  LIBS="$saved_LIBS"
  MUTTLIBS="$MUTTLIBS -lsqlite3"
fi

AC_ARG_ENABLE(autocrypt,
        AS_HELP_STRING([--enable-autocrypt],[Enable autocrypt support]),
        [],
        [enable_autocrypt=no])
if test x$enable_autocrypt = xyes; then
  if test x$with_sqlite3 = xno; then
    AC_MSG_ERROR([autocrypt requires --with-sqlite3])
  fi
  AC_DEFINE(USE_AUTOCRYPT,1,[ Define if you want support for autocrypt. ])
  LIBAUTOCRYPT="-Lautocrypt -lautocrypt"
  LIBAUTOCRYPTDEPS="\$(top_srcdir)/autocrypt/autocrypt.h autocrypt/libautocrypt.a"
  mutt_enable_gpgme=yes
  echo "enabling autocrypt..."
  mutt_gpgme_version="1.8.0"
  echo "Note: autocrypt requires GPGME version $mutt_gpgme_version or greater"
fi
AM_CONDITIONAL(BUILD_AUTOCRYPT, test x$enable_autocrypt = xyes)
AC_SUBST(LIBAUTOCRYPT)
AC_SUBST(LIBAUTOCRYPTDEPS)

if test x$mutt_enable_gpgme != xyes; then
  AC_ARG_ENABLE(gpgme, AS_HELP_STRING([--enable-gpgme],[Enable GPGME support]),
  [       if test x$enableval = xyes; then
            mutt_enable_gpgme=yes
            mutt_gpgme_version="1.4.0"
          fi
  ])
fi

AC_MSG_CHECKING([whether to build with GPGME support])
if test x"$mutt_enable_gpgme" = xyes; then
   AC_MSG_RESULT(yes)
   AM_PATH_GPG_ERROR(1.33)
   AM_PATH_GPGME([$mutt_gpgme_version], AC_DEFINE(CRYPT_BACKEND_GPGME, 1,
		 [Defined, if GPGME support is enabled]),
		 [gpgme_found=no])
   if test x"$gpgme_found" = xno; then
      AC_MSG_ERROR([*** GPGME not found or version is older than $mutt_gpgme_version ***])
   else
      MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS crypt-gpgme.o crypt-mod-pgp-gpgme.o crypt-mod-smime-gpgme.o"
   fi
else
   AC_MSG_RESULT([no])
fi

AC_ARG_ENABLE(pgp, AS_HELP_STRING([--disable-pgp],[Disable PGP support]),
[       if test x$enableval = xno ; then
                have_pgp=no
        fi
])

if test x$have_pgp != xno ; then
        AC_DEFINE(CRYPT_BACKEND_CLASSIC_PGP,1, [Define if you want classic PGP support.])
        PGPAUX_TARGET="mutt_pgpring\$(EXEEXT) pgpewrap\$(EXEEXT)"
        MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS pgp.o pgpinvoke.o pgpkey.o pgplib.o gnupgparse.o pgpmicalg.o pgppacket.o crypt-mod-pgp-classic.o"
fi

AC_ARG_ENABLE(smime, AS_HELP_STRING([--disable-smime],[Disable SMIME support]),
[	if test x$enableval = xno ; then
		have_smime=no
	fi
])

if test x$have_smime != xno ; then
	AC_DEFINE(CRYPT_BACKEND_CLASSIC_SMIME, 1, [Define if you want classic S/MIME support.])
	MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS smime.o crypt-mod-smime-classic.o"
	SMIMEAUX_TARGET="smime_keys"
fi

AC_ARG_ENABLE(sidebar, AS_HELP_STRING([--enable-sidebar], [Enable Sidebar support]),
[       if test x$enableval = xyes ; then
		AC_DEFINE(USE_SIDEBAR, 1, [Define if you want support for the sidebar.])
		OPS="$OPS \$(srcdir)/OPS.SIDEBAR"
                MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS sidebar.o"
        fi
])

AC_ARG_ENABLE(compressed, AS_HELP_STRING([--enable-compressed], [Enable compressed folders support]),
              enable_compressed=$enableval, enable_compressed=no
)
AS_IF([test x$enable_compressed = "xyes"], [
	   AC_DEFINE(USE_COMPRESSED, 1, [Define to enable compressed folders support.])
	   MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS compress.o"
])
AM_CONDITIONAL(BUILD_COMPRESS, test x$enable_compressed = xyes)

AC_ARG_WITH(mixmaster, AS_HELP_STRING([--with-mixmaster@<:@=PATH@:>@],[Include Mixmaster support]),
  [if test "$withval" != no
   then
     if test -x "$withval"
     then
       MIXMASTER="$withval"
     else
       MIXMASTER="mixmaster"
     fi
     OPS="$OPS \$(srcdir)/OPS.MIX"
     MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS remailer.o"
     AC_DEFINE_UNQUOTED(MIXMASTER,"$MIXMASTER", [Where to find mixmaster on your system.])
   fi])

# We now require all OPS
OPS="$OPS \$(srcdir)/OPS.PGP \$(srcdir)/OPS.SMIME \$(srcdir)/OPS.CRYPT "
AC_SUBST([OPS])

AC_SUBST(PGPAUX_TARGET)
AC_SUBST(SMIMEAUX_TARGET)

AC_PATH_PROG(ISPELL, ispell, no)
if test $ISPELL != no; then
        AC_DEFINE_UNQUOTED(ISPELL,"$ISPELL",[ Where to find ispell on your system. ])
fi

AC_ARG_WITH(slang, AS_HELP_STRING([--with-slang@<:@=DIR@:>@],[Use S-Lang instead of ncurses]),
        [AC_CACHE_CHECK([if this is a BSD system], mutt_cv_bsdish,
                [AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <sys/param.h>
#include <stdlib.h>

main ()
{
#ifdef BSD
        exit (0);
#else
        exit (1);
#endif
}]])],[mutt_cv_bsdish=yes],[mutt_cv_bsdish=no],[mutt_cv_bsdish=no])])

        AC_MSG_CHECKING(for S-Lang)
        if test $withval = yes; then
                if test -d $srcdir/../slang; then
                        mutt_cv_slang=$srcdir/../slang/src
                        CPPFLAGS="$CPPFLAGS -I${mutt_cv_slang}"
                        LDFLAGS="$LDFLAGS -L${mutt_cv_slang}/objs"
                else
                        if test -d $mutt_cv_prefix/include/slang; then
                                CPPFLAGS="$CPPFLAGS -I$mutt_cv_prefix/include/slang"
                        elif test -d /usr/include/slang; then
                                CPPFLAGS="$CPPFLAGS -I/usr/include/slang"
                        fi
                        mutt_cv_slang=yes
                fi
        else
                dnl ---Check to see if $withval is a source directory
                if test -f $withval/src/slang.h; then
                        mutt_cv_slang=$withval/src
                        CPPFLAGS="$CPPFLAGS -I${mutt_cv_slang}"
                        LDFLAGS="$LDFLAGS -L${mutt_cv_slang}/objs"
                else
                        dnl ---Must be installed somewhere
                        mutt_cv_slang=$withval
                        if test -d $withval/include/slang; then
                                CPPFLAGS="$CPPFLAGS -I${withval}/include/slang"
                        elif test -d $withval/include; then
                                CPPFLAGS="$CPPFLAGS -I${withval}/include"
                        fi
                        LDFLAGS="$LDFLAGS -L${withval}/lib"
                fi
        fi
        AC_MSG_RESULT($mutt_cv_slang)
        if test $mutt_cv_bsdish = yes; then
                AC_CHECK_LIB(termlib, main)
        fi
        AC_DEFINE(USE_SLANG_CURSES,1,
                [ Define if you compile with SLang instead of curses/ncurses. ])
        AC_DEFINE(HAVE_COLOR,1,[ Define if your curses library supports color. ])
        MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS resize.o"

        dnl --- now that we've found it, check the link

        AC_CHECK_LIB(slang, SLtt_get_terminfo,
                [MUTTLIBS="$MUTTLIBS -lslang -lm"],
                [AC_MSG_ERROR(unable to compile.  check config.log)], -lm)

        ],

        [mutt_cv_curses=/usr
        AC_ARG_WITH(curses, AS_HELP_STRING([--with-curses=DIR],[Where ncurses is installed]),
                [if test x$withval != xyes; then
                        mutt_cv_curses=$withval
                fi
                if test x$mutt_cv_curses != x/usr; then
                        LDFLAGS="$LDFLAGS -L${mutt_cv_curses}/lib"
                        CPPFLAGS="$CPPFLAGS -I${mutt_cv_curses}/include"
                fi])

	AC_CHECK_FUNC(initscr,,[
	cf_ncurses="ncurses"
	for lib in ncurses ncursesw
	do
		AC_CHECK_LIB($lib, waddnwstr, [cf_ncurses="$lib"; break])
	done
        AC_CHECK_LIB($cf_ncurses, initscr,
                [MUTTLIBS="$MUTTLIBS -l$cf_ncurses"

                if test "$cf_ncurses" = ncursesw; then
                  AC_CHECK_LIB(tinfow, tgetent, [MUTTLIBS="$MUTTLIBS -ltinfow"],
                               AC_CHECK_LIB(tinfo, tgetent, [MUTTLIBS="$MUTTLIBS -ltinfo"]))

                  AC_CHECK_HEADERS(ncursesw/ncurses.h,[cf_cv_ncurses_header="ncursesw/ncurses.h"],
                    [AC_CHECK_HEADERS(ncurses.h,[cf_cv_ncurses_header="ncurses.h"])])
                else
                  AC_CHECK_LIB(tinfo, tgetent, [MUTTLIBS="$MUTTLIBS -ltinfo"])

                  AC_CHECK_HEADERS(ncurses/ncurses.h,[cf_cv_ncurses_header="ncurses/ncurses.h"],
                    [AC_CHECK_HEADERS(ncurses.h,[cf_cv_ncurses_header="ncurses.h"])])
		fi],

                [CF_CURSES_LIBS])
		])

        AC_CHECK_HEADERS(term.h)

        old_LIBS="$LIBS"
        LIBS="$LIBS $MUTTLIBS"
        CF_CHECK_FUNCDECLS([#include <${cf_cv_ncurses_header-curses.h}>],
                           [start_color typeahead bkgdset curs_set meta use_default_colors resizeterm setcchar bkgrndset use_tioctl])
        if test "$ac_cv_func_decl_start_color" = yes; then
                AC_DEFINE(HAVE_COLOR,1,[ Define if your curses library supports color. ])
        fi
        if test "$ac_cv_func_decl_resizeterm" = yes; then
                MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS resize.o"
        fi
        AC_CHECK_FUNCS([use_extended_names])
        LIBS="$old_LIBS"
        ])

AC_CHECK_HEADERS(stdarg.h sys/ioctl.h ioctl.h sysexits.h)
AC_CHECK_HEADERS(sys/time.h sys/resource.h)
AC_CHECK_HEADERS(unix.h)

AC_CHECK_FUNCS(setrlimit getsid)
AC_CHECK_FUNCS(fgets_unlocked fgetc_unlocked)

AC_MSG_CHECKING(for sig_atomic_t in signal.h)
AC_EGREP_HEADER(sig_atomic_t,signal.h,
  [
    ac_cv_type_sig_atomic_t=yes;
    AC_EGREP_HEADER(volatile.*sig_atomic_t,
                    signal.h,
                    [
                        is_sig_atomic_t_volatile=yes;
                        AC_MSG_RESULT([yes, volatile])
                    ],
                    [
                        is_sig_atomic_t_volatile=no;
                        AC_MSG_RESULT([yes, non volatile])
                    ])
  ],
  [
    AC_MSG_RESULT(no)
    AC_CHECK_TYPE(sig_atomic_t, [],
                  [AC_DEFINE([sig_atomic_t], [int],
                             [Define to `int' if <signal.h> does not define.])])
    is_sig_atomic_t_volatile=no
  ])
if test $is_sig_atomic_t_volatile = 'yes'
then
    AC_DEFINE(SIG_ATOMIC_VOLATILE_T, sig_atomic_t)
else
    AC_DEFINE(SIG_ATOMIC_VOLATILE_T, [volatile sig_atomic_t])
fi

AC_CHECK_DECLS([sys_siglist],[],[],[#include <signal.h>
/* NetBSD declares sys_siglist in unistd.h.  */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
])


AC_TYPE_PID_T
AC_CHECK_TYPE(ssize_t, [],
              [AC_DEFINE([ssize_t], [int],
                         [Define to `int' if <sys/types.h> does not define.])])

AC_CHECK_FUNCS(fgetpos memmove memccpy setegid srand48 strerror)

AC_REPLACE_FUNCS([setenv strcasecmp strdup strsep strtok_r wcscasecmp])
AC_REPLACE_FUNCS([strcasestr mkdtemp])

AC_CHECK_FUNC(getopt)
if test $ac_cv_func_getopt = yes; then
        AC_CHECK_HEADERS(getopt.h)
fi

dnl SCO uses chsize() instead of ftruncate()
AC_CHECK_FUNCS(ftruncate, , [AC_CHECK_LIB(x, chsize)])

dnl SCO has strftime() in libintl
AC_CHECK_FUNCS(strftime, , [AC_CHECK_LIB(intl, strftime)])

dnl Set the atime of files
AC_CHECK_FUNCS(futimens)

dnl Check for struct timespec
AC_CHECK_TYPES([struct timespec],,,[[#include <time.h>]])

dnl Check for stat nanosecond resolutions
AC_CHECK_MEMBERS([struct stat.st_atim.tv_nsec,
                  struct stat.st_mtim.tv_nsec,
                  struct stat.st_ctim.tv_nsec],,,[[#include <sys/stat.h>]])

dnl Check for utimesnsat
AC_CHECK_FUNCS(utimensat)

dnl Check for clock_gettime
AC_CHECK_FUNCS(clock_gettime)

dnl AIX may not have fchdir()
AC_CHECK_FUNCS(fchdir, , [mutt_cv_fchdir=no])

AC_ARG_WITH(bundled-regex,
        AS_HELP_STRING([--with-bundled-regex],[Use the bundled GNU regex library]),
        [mutt_cv_regex=yes],
        [AC_CHECK_FUNCS(regcomp, mutt_cv_regex=no, mutt_cv_regex=yes)])

if test $mutt_cv_regex = no ; then
AC_CACHE_CHECK([whether your system's regexp library is completely broken],
        [mutt_cv_regex_broken],
        AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <unistd.h>
#include <regex.h>
int main() {
  regex_t blah;
  regmatch_t p;
  p.rm_eo = p.rm_eo;
  if (regcomp(&blah, "foo.*bar", REG_NOSUB) ||
      regexec(&blah, "foobar", 0, NULL, 0))
    return(1);
  regfree(&blah);
  return(0);
}]])],
                [mutt_cv_regex_broken=no],
                [mutt_cv_regex_broken=yes],
                [mutt_cv_regex_broken=yes]))

        if test $mutt_cv_regex_broken = yes ; then
                echo "Using the included GNU regex instead." >&AS_MESSAGE_FD
                mutt_cv_regex=yes
        fi
fi

if test $mutt_cv_regex = yes; then
        AC_DEFINE(USE_GNU_REGEX,1,[ Define if you want to use the included regex.c. ])
        AC_LIBOBJ(regex)
fi


AC_ARG_WITH(homespool,
  AS_HELP_STRING([--with-homespool@<:@=FILE@:>@],[File in user's directory where new mail is spooled]), with_homespool=${withval})
if test x$with_homespool != x; then
        if test $with_homespool = yes; then
                with_homespool=mailbox
        fi
        AC_DEFINE_UNQUOTED(MAILPATH,"$with_homespool",[ Where new mail is spooled. ])
        AC_DEFINE(HOMESPOOL,1,
          [Is mail spooled to the user's home directory?  If defined,
           MAILPATH should be set to the filename of the spool mailbox
           relative the the home directory.
           use: configure --with-homespool=FILE])
        AC_DEFINE(USE_DOTLOCK,1,[ Define to use dotlocking for mailboxes. ])
        mutt_cv_setgid=no
else
        AC_ARG_WITH(mailpath, AS_HELP_STRING([--with-mailpath=DIR],[Directory where spool mailboxes are located]),
                [mutt_cv_mailpath=$withval],
                [ AC_CACHE_CHECK(where new mail is stored, mutt_cv_mailpath,
                        [mutt_cv_mailpath=no
                        if test -d /var/mail; then
                                mutt_cv_mailpath=/var/mail
                        elif test -d /var/spool/mail; then
                                mutt_cv_mailpath=/var/spool/mail
                        elif test -d /usr/spool/mail; then
                                mutt_cv_mailpath=/usr/spool/mail
                        elif test -d /usr/mail; then
                                mutt_cv_mailpath=/usr/mail
                        fi])
                ])
        if test "$mutt_cv_mailpath" = no; then
                AC_MSG_ERROR("Could not determine where new mail is stored.")
        fi
        AC_DEFINE_UNQUOTED(MAILPATH,"$mutt_cv_mailpath",[ Where new mail is spooled. ])

        AC_CACHE_CHECK(if $mutt_cv_mailpath is world writable, mutt_cv_worldwrite, [AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
        struct stat s;

        if (stat ("$mutt_cv_mailpath", &s)) exit (1);
        if (s.st_mode & S_IWOTH) exit (0);
        exit (1);
}]])],[mutt_cv_worldwrite=yes],[mutt_cv_worldwrite=no],[mutt_cv_worldwrite=no])])

        mutt_cv_setgid=no
        if test $mutt_cv_worldwrite = yes; then
                AC_DEFINE(USE_DOTLOCK,1,[ Define to use dotlocking for mailboxes. ])
        else

                AC_CACHE_CHECK(if $mutt_cv_mailpath is group writable, mutt_cv_groupwrite, [AC_RUN_IFELSE([AC_LANG_SOURCE([[#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
        struct stat s;

        if (stat ("$mutt_cv_mailpath", &s)) exit (1);
        if (s.st_mode & S_IWGRP) exit (0);
        exit (1);
}]])],[mutt_cv_groupwrite=yes],[mutt_cv_groupwrite=no],[mutt_cv_groupwrite=no])])

                if test $mutt_cv_groupwrite = yes; then
                        AC_DEFINE(USE_DOTLOCK,1,[ Define to use dotlocking for mailboxes. ])
                        AC_DEFINE(USE_SETGID,1,[ Define if mutt should run setgid "mail". ])
                        mutt_cv_setgid=yes
                fi
        fi
fi

AC_ARG_ENABLE(external_dotlock, AS_HELP_STRING([--enable-external-dotlock],[Force use of an external dotlock program]),
        [mutt_cv_external_dotlock="$enableval"])

if test "x$mutt_cv_setgid" = "xyes" || test "x$mutt_cv_fchdir" = "xno" \
        || test "x$mutt_cv_external_dotlock" = "xyes"
then
        AC_DEFINE(DL_STANDALONE,1,[ Define if you want to use an external dotlocking program. ])
        DOTLOCK_TARGET="mutt_dotlock\$(EXEEXT)"
else
        MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS dotlock.o"
fi

AC_SUBST(DOTLOCK_TARGET)

dnl autoconf <2.60 compatibility
if test -z "$datarootdir"; then
  datarootdir='${prefix}/share'
fi
AC_SUBST([datarootdir])

AC_MSG_CHECKING(where to put the documentation)
AC_ARG_WITH(docdir, AS_HELP_STRING([--with-docdir=PATH],[Specify where to put the documentation]),
        [mutt_cv_docdir=$withval],
        [mutt_cv_docdir='${datarootdir}/doc/mutt'])
AC_MSG_RESULT($mutt_cv_docdir)
if test -z "$docdir" -o -n "$with_docdir"
then
  docdir=$mutt_cv_docdir
fi
AC_SUBST(docdir)

if test x$mutt_cv_setgid = xyes; then
        DOTLOCK_GROUP='mail'
        DOTLOCK_PERMISSION=2755
else
        DOTLOCK_GROUP=''
        DOTLOCK_PERMISSION=755
fi
AC_SUBST(DOTLOCK_GROUP)
AC_SUBST(DOTLOCK_PERMISSION)

AC_ARG_WITH(domain, AS_HELP_STRING([--with-domain=DOMAIN],[Specify your DNS domain name]),
        [if test $withval != yes; then
            if test $withval != no; then
                AC_DEFINE_UNQUOTED(DOMAIN,"$withval",[ Define your domain name. ])
            fi
        fi])

need_socket="no"

dnl -- socket dependencies --

dnl getaddrinfo is used by getdomain.c, and requires libnsl and
dnl libsocket on some platforms
AC_CHECK_FUNC(gethostent, , AC_CHECK_LIB(nsl, gethostent))
AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt))
AC_CHECK_FUNCS(getaddrinfo)

AC_ARG_ENABLE(pop,  AS_HELP_STRING([--enable-pop],[Enable POP3 support]),
[       if test x$enableval = xyes ; then
                AC_DEFINE(USE_POP,1,[ Define if you want support for the POP3 protocol. ])
                MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS pop.o pop_lib.o pop_auth.o"
                need_pop="yes"
                need_socket="yes"
                need_md5="yes"
        fi
])

AC_ARG_ENABLE(imap, AS_HELP_STRING([--enable-imap],[Enable IMAP support]),
[       if test x$enableval = xyes ; then
                AC_DEFINE(USE_IMAP,1,[ Define if you want support for the IMAP protocol. ])
                LIBIMAP="-Limap -limap"
                LIBIMAPDEPS="\$(top_srcdir)/imap/imap.h imap/libimap.a"
                need_imap="yes"
                need_socket="yes"
                need_md5="yes"
        fi
])
AM_CONDITIONAL(BUILD_IMAP, test x$need_imap = xyes)

AC_ARG_ENABLE(smtp, AS_HELP_STRING([--enable-smtp],[include internal SMTP relay support]),
	[if test $enableval = yes; then
		AC_DEFINE(USE_SMTP, 1, [Include internal SMTP relay support])
            	MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS smtp.o"
		need_socket="yes"
	fi])

if test x"$need_imap" = xyes -o x"$need_pop" = xyes ; then
  MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS bcache.o"
fi

dnl -- end socket dependencies --

if test "$need_socket" = "yes"
then
        AC_CHECK_HEADERS([sys/select.h])

        AC_MSG_CHECKING([for socklen_t])
        AC_EGREP_HEADER(socklen_t, sys/socket.h, AC_MSG_RESULT([yes]),
                AC_MSG_RESULT([no])
                AC_DEFINE(socklen_t,int,
                        [ Define to 'int' if <sys/socket.h> doesn't have it. ]))
        AC_DEFINE(USE_SOCKET,1,
                [ Include code for socket support. Set automatically if you enable POP3 or IMAP ])
        MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS account.o mutt_socket.o mutt_tunnel.o"
fi

dnl -- imap dependencies --

AC_ARG_WITH(gss, AS_HELP_STRING([--with-gss@<:@=PFX@:>@],[Compile in GSSAPI authentication for IMAP]),
    gss_prefix="$withval", gss_prefix="no")
if test "$gss_prefix" != "no"
then
  if test "$need_imap" = "yes"
  then
    MUTT_AM_PATH_GSSAPI(gss_prefix)
    AC_MSG_CHECKING(GSSAPI implementation)
    AC_MSG_RESULT($GSSAPI_IMPL)
    if test "$GSSAPI_IMPL" = "none"
    then
      AC_CACHE_SAVE
      AC_MSG_ERROR([GSSAPI libraries not found])
    fi
    if test "$GSSAPI_IMPL" = "Heimdal"
    then
      AC_DEFINE(HAVE_HEIMDAL,1,[ Define if your GSSAPI implementation is Heimdal ])
    fi
    CPPFLAGS="$CPPFLAGS $GSSAPI_CFLAGS"
    MUTTLIBS="$MUTTLIBS $GSSAPI_LIBS"
    AC_DEFINE(USE_GSS,1,[ Define if you have GSSAPI libraries available ])
    need_gss="yes"
  else
    AC_MSG_WARN([GSS was requested but IMAP is not enabled])
  fi
fi
AM_CONDITIONAL(USE_GSS, test x$need_gss = xyes)

# if zlib
AC_ARG_WITH(zlib, AS_HELP_STRING([--with-zlib@<:@=PFX@:>@],[Enable DEFLATE support for IMAP using libz]),
    zlib_prefix="$withval", zlib_prefix="auto")
if test "$zlib_prefix" != "no"
then
  if test "$need_imap" = "yes"
  then
	have_zlib=
	if test "$zlib_prefix" != "yes" -a "$zlib_prefix" != "auto"
	then
	  LDFLAGS="$LDFLAGS -L$zlib_prefix/lib"
          CPPFLAGS="$CPPFLAGS -I$zlib_prefix/include"
	fi
        saved_LIBS="$LIBS"
	AC_CHECK_HEADERS([zlib.h], [AC_CHECK_LIB([z], [deflate],
					 [have_zlib=yes])])
	if test "x$have_zlib" = "x"
	then
	  if test "x$zlib_prefix" != "xauto"
	  then
		AC_MSG_ERROR([ZLIB requested, but library or headers not found])
	  fi
	else
	  MUTTLIBS="$MUTTLIBS -lz"
	  AC_DEFINE(USE_ZLIB, 1, [Define if you have libz available])
	  MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS mutt_zstrm.o"
	fi
        LIBS="$saved_LIBS"
  else
    AC_MSG_WARN([ZLIB was requested but IMAP is not enabled])
  fi
fi

dnl -- end imap dependencies --

AC_ARG_WITH(ssl, AS_HELP_STRING([--with-ssl@<:@=PFX@:>@],[Enable TLS support using OpenSSL]),
[       if test "$with_ssl" != "no"
        then
          if test "$need_socket" != "yes"; then
           AC_MSG_WARN([SSL support is only useful with POP, IMAP or SMTP support])
	  else
            if test "$with_ssl" != "yes"
            then
             LDFLAGS="$LDFLAGS -L$withval/lib"
             CPPFLAGS="$CPPFLAGS -I$withval/include"
            fi
            saved_LIBS="$LIBS"

            crypto_libs=""
            AC_CHECK_LIB(z, deflate, [crypto_libs=-lz])
            AC_CHECK_LIB(crypto, X509_STORE_CTX_new,
              [crypto_libs="-lcrypto $crypto_libs"],
              AC_MSG_ERROR([Unable to find SSL library]), [$crypto_libs])
            AC_CHECK_LIB(ssl, SSL_new,,
              AC_MSG_ERROR([Unable to find SSL library]), [$crypto_libs])

            LIBS="$LIBS $crypto_libs"
            AC_CHECK_FUNCS(RAND_egd)
            AC_CHECK_DECLS([SSL_set_mode, SSL_MODE_AUTO_RETRY],,
              AC_MSG_ERROR([Unable to find decent SSL header]), [[#include <openssl/ssl.h>]])

            AC_CHECK_DECL([X509_V_FLAG_PARTIAL_CHAIN],
              AC_DEFINE(HAVE_SSL_PARTIAL_CHAIN,1,[ Define if OpenSSL supports partial chains. ]),
              ,
              [[#include <openssl/x509_vfy.h>]])

            AC_DEFINE(USE_SSL,1,[ Define if you want support for SSL. ])
            AC_DEFINE(USE_SSL_OPENSSL,1,[ Define if you want support for SSL via OpenSSL. ])
            LIBS="$saved_LIBS"
            MUTTLIBS="$MUTTLIBS -lssl $crypto_libs"
            MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS mutt_ssl.o"
            need_ssl=yes
          fi
        fi
])

AC_ARG_WITH([gnutls], AS_HELP_STRING([--with-gnutls@<:@=PFX@:>@],[enable TLS support using gnutls]),
    [gnutls_prefix="$withval"], [gnutls_prefix="no"])
if test "$gnutls_prefix" != "no" && test x"$need_ssl" != xyes
then
  if test "$need_socket" != "yes"
  then
    AC_MSG_WARN([SSL support is only useful with POP, IMAP or SMTP support])
  else
    if test "$gnutls_prefix" != "yes"
    then
      LDFLAGS="$LDFLAGS -L$gnutls_prefix/lib"
      CPPFLAGS="$CPPFLAGS -I$gnutls_prefix/include"
    fi
    saved_LIBS="$LIBS"

    AC_CHECK_LIB(gnutls, gnutls_check_version,
      [dnl GNUTLS found
      AC_CHECK_DECLS([GNUTLS_VERIFY_DISABLE_TIME_CHECKS], [], [],
                    [[#include <gnutls/x509.h>]])

      LIBS="$LIBS -lgnutls"
      AC_CHECK_FUNCS(gnutls_priority_set_direct)
      AC_CHECK_TYPES([gnutls_certificate_credentials_t,
                      gnutls_certificate_status_t,
                      gnutls_datum_t,
                      gnutls_digest_algorithm_t,
                      gnutls_session_t,
                      gnutls_transport_ptr_t,
                      gnutls_x509_crt_t], [], [], [[#include <gnutls/gnutls.h>]])

      LIBS="$saved_LIBS"
      MUTTLIBS="$MUTTLIBS -lgnutls"

      AC_DEFINE(USE_SSL, 1, [ Define if you want support for SSL. ])
      AC_DEFINE(USE_SSL_GNUTLS, 1, [ Define if you want support for SSL via GNUTLS. ])

      MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS mutt_ssl_gnutls.o"
      need_ssl=yes],
      [AC_MSG_ERROR([could not find libgnutls])])
  fi
fi

AM_CONDITIONAL(USE_SSL, test x$need_ssl = xyes)

AC_ARG_WITH(sasl, AS_HELP_STRING([--with-sasl@<:@=PFX@:>@],[Use SASL network security library]),
        [
        if test "$with_sasl" != "no"
        then
          if test "$need_socket" != "yes"
          then
            AC_MSG_ERROR([SASL support is only useful with POP or IMAP support])
          fi

          if test "$with_sasl" != "yes"
          then
            CPPFLAGS="$CPPFLAGS -I$with_sasl/include"
            LDFLAGS="$LDFLAGS -L$with_sasl/lib"
          fi

          saved_LIBS="$LIBS"
          LIBS=

          # OpenSolaris provides a SASL2 interface in libsasl
          sasl_libs="sasl2 sasl"
          AC_SEARCH_LIBS(sasl_encode64, [$sasl_libs],,
                  AC_MSG_ERROR([could not find sasl lib]),)

          MUTTLIBS="$MUTTLIBS $LIBS"
          MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS mutt_sasl.o"
          LIBS="$saved_LIBS"

          AC_DEFINE(USE_SASL,1,
                  [ Define if want support for SASL. ])
          AC_DEFINE(USE_SASL_CYRUS,1,
                  [ Define if want to use the Cyrus SASL library for POP/IMAP authentication. ])
          need_sasl=yes
          need_sasl_cyrus=yes
        fi
        ])
AC_ARG_WITH(gsasl, AS_HELP_STRING([--with-gsasl@<:@=PFX@:>@],[Use GNU SASL network security library]),
        [
        if test "$with_gsasl" != "no"
        then
          if test "$need_socket" != "yes"
          then
            AC_MSG_ERROR([GNU SASL support is only useful with POP or IMAP support])
          fi

          if test x"$need_sasl" = "xyes"
          then
            AC_MSG_ERROR([Both --with-gsasl and --with-sasl can not be enabled at the same time])
          fi

          if test "$with_gsasl" != "yes"
          then
            CPPFLAGS="$CPPFLAGS -I$with_gsasl/include"
            LDFLAGS="$LDFLAGS -L$with_gsasl/lib"
          fi

          saved_LIBS="$LIBS"
          LIBS=

          AC_CHECK_HEADER(gsasl.h,
              AC_CHECK_LIB(gsasl, gsasl_check_version,,
                           AC_MSG_ERROR([GNU SASL library not found])),
              AC_MSG_ERROR([GNU SASL headers not found]))

          MUTTLIBS="$MUTTLIBS -lgsasl"
          MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS mutt_sasl_gnu.o"
          LIBS="$saved_LIBS"

          AC_DEFINE(USE_SASL,1,
                  [ Define if want support for SASL. ])
          AC_DEFINE(USE_SASL_GNU,1,
                  [ Define if want to use the GNU SASL library for POP/IMAP authentication. ])
          need_sasl=yes
          need_sasl_gnu=yes
        fi
        ])
AM_CONDITIONAL(USE_SASL_CYRUS, test x$need_sasl_cyrus = xyes)
AM_CONDITIONAL(USE_SASL_GNU, test x$need_sasl_gnu = xyes)

dnl -- end socket --

AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug],[Enable debugging support]),
        [ if test x$enableval = xyes ; then
                AC_DEFINE(DEBUG,1,[ Define to enable debugging info. ])
          fi
         ])

AC_ARG_ENABLE(flock, AS_HELP_STRING([--enable-flock],[Use flock() to lock files]),
        [if test $enableval = yes; then
                AC_DEFINE(USE_FLOCK,1, [ Define to use flock() to lock mailboxes. ])
        fi])

mutt_cv_fcntl=yes
AC_ARG_ENABLE(fcntl, AS_HELP_STRING([--disable-fcntl],[Do NOT use fcntl() to lock files]),
        [if test $enableval = no; then mutt_cv_fcntl=no; fi])

if test $mutt_cv_fcntl = yes; then
        AC_DEFINE(USE_FCNTL,1, [ Define to use fcntl() to lock folders. ])
fi

AC_ARG_ENABLE(filemonitor, AS_HELP_STRING([--disable-filemonitor],[Disable file monitoring support (Linux only)]),
[       if test x$enableval = xno ; then
                have_filemonitor=no
        fi
])

if test x$have_filemonitor != xno ; then
        AC_CHECK_FUNCS(inotify_init inotify_add_watch inotify_rm_watch,
                       [], [have_filemonitor=no])
        if test x$have_filemonitor != xno ; then
                AC_DEFINE(USE_INOTIFY,1,[ Define if want to use inotify for filesystem monitoring (available in Linux only). ])
                AC_CHECK_FUNCS_ONCE(inotify_init1)
                AC_CHECK_HEADERS(sys/inotify.h)
                MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS monitor.o"
        fi
fi

AC_MSG_CHECKING(whether struct dirent defines d_ino)
ac_cv_dirent_d_ino=no
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <dirent.h>]], [[struct dirent dp; (void)dp.d_ino]])],[ac_cv_dirent_d_ino=yes],[])
if test x$ac_cv_dirent_d_ino = xyes ; then
  AC_DEFINE(HAVE_DIRENT_D_INO,1,
            [Define to 1 if your system has the dirent::d_ino member])
fi
AC_MSG_RESULT($ac_cv_dirent_d_ino)

mutt_cv_warnings=yes
AC_ARG_ENABLE(warnings, AS_HELP_STRING([--disable-warnings],[Turn off compiler warnings (not recommended)]),
[if test $enableval = no; then
        mutt_cv_warnings=no
fi])

if test x$GCC = xyes && test $mutt_cv_warnings = yes; then
  CFLAGS="-Wall -pedantic -Wno-long-long $CFLAGS"
fi

AC_ARG_ENABLE(nfs-fix, AS_HELP_STRING([--enable-nfs-fix],[Work around an NFS with broken attributes caching]),
        [if test x$enableval = xyes; then
                AC_DEFINE(NFS_ATTRIBUTE_HACK,1,
                  [Define if you have problems with mutt not detecting
                   new/old mailboxes over NFS.  Some NFS implementations
                   incorrectly cache the attributes of small files.])
        fi])

AC_ARG_ENABLE(mailtool, AS_HELP_STRING([--enable-mailtool],[Enable Sun mailtool attachments support]),
        [if test x$enableval = xyes; then
                AC_DEFINE(SUN_ATTACHMENT,1,[ Define to enable Sun mailtool attachments support. ])
        fi])

AC_ARG_ENABLE(locales-fix, AS_HELP_STRING([--enable-locales-fix],[The result of isprint() is unreliable]),
        [if test x$enableval = xyes; then
                AC_DEFINE(LOCALES_HACK,1,[ Define if the result of isprint() is unreliable. ])
        fi])

AC_ARG_WITH(exec-shell, AS_HELP_STRING([--with-exec-shell=SHELL],[Specify alternate shell (ONLY if /bin/sh is broken)]),
        [if test $withval != yes; then
                AC_DEFINE_UNQUOTED(EXECSHELL, "$withval",
                 [program to use for shell commands])
         else
                AC_DEFINE_UNQUOTED(EXECSHELL, "/bin/sh")
        fi],
        [AC_DEFINE_UNQUOTED(EXECSHELL, "/bin/sh")])

AC_ARG_ENABLE(exact-address, AS_HELP_STRING([--enable-exact-address],[Enable regeneration of email addresses]),
        [if test $enableval = yes; then
                AC_DEFINE(EXACT_ADDRESS,1,
                  [Enable exact regeneration of email addresses as parsed?
                   NOTE: this requires significant more memory when defined.])

        fi])

dnl -- start cache --
db_found=no
db_requested=auto
AC_ARG_ENABLE(hcache, AS_HELP_STRING([--enable-hcache],[Enable header caching]))
AC_ARG_WITH(kyotocabinet, AS_HELP_STRING([--with-kyotocabinet@<:@=DIR@:>@],[Use kyotocabinet hcache backend]))
AC_ARG_WITH(tokyocabinet, AS_HELP_STRING([--with-tokyocabinet@<:@=DIR@:>@],[Use tokyocabinet hcache backend]))
AC_ARG_WITH(lmdb, AS_HELP_STRING([--with-lmdb@<:@=DIR@:>@],[Use lmdb hcache backend]))
AC_ARG_WITH(qdbm, AS_HELP_STRING([--with-qdbm@<:@=DIR@:>@],[Use qdbm hcache backend]))
AC_ARG_WITH(gdbm, AS_HELP_STRING([--with-gdbm@<:@=DIR@:>@],[Use gdbm hcache backend]))
AC_ARG_WITH(bdb, AS_HELP_STRING([--with-bdb@<:@=DIR@:>@],[Use bdb hcache backend]))

if test x$enable_hcache = xyes
then
    AC_DEFINE(USE_HCACHE, 1, [Enable header caching])
    MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS hcache.o"

    OLDCPPFLAGS="$CPPFLAGS"
    OLDLDFLAGS="$LDFLAGS"
    OLDLIBS="$LIBS"

    need_md5="yes"

    if test -n "$with_kyotocabinet" && test "$with_kyotocabinet" != "no"
    then
      if test "$db_requested" != "auto"
      then
        AC_MSG_ERROR([more than one header cache engine requested.])
      else
        db_requested=kc
      fi
    fi
    if test -n "$with_tokyocabinet" && test "$with_tokyocabinet" != "no"
    then
      if test "$db_requested" != "auto"
      then
        AC_MSG_ERROR([more than one header cache engine requested.])
      else
        db_requested=tc
      fi
    fi
    if test -n "$with_lmdb" && test "$with_lmdb" != "no"
    then
      if test "$db_requested" != "auto"
      then
        AC_MSG_ERROR([more than one header cache engine requested.])
      else
        db_requested=lmdb
      fi
    fi
    if test -n "$with_qdbm" && test "$with_qdbm" != "no"
    then
      if test "$db_requested" != "auto"
      then
        AC_MSG_ERROR([more than one header cache engine requested.])
      else
        db_requested=qdbm
      fi
    fi
    if test -n "$with_gdbm" && test "$with_gdbm" != "no"
    then
      if test "$db_requested" != "auto"
      then
        AC_MSG_ERROR([more than one header cache engine requested.])
      else
        db_requested=gdbm
      fi
    fi
    if test -n "$with_bdb" && test "$with_bdb" != "no"
    then
      if test "$db_requested" != "auto"
      then
        AC_MSG_ERROR([more than one header cache engine requested.])
      else
        db_requested=bdb
      fi
    fi

    dnl -- Kyoto Cabinet --
    if test x$with_kyotocabinet != xno && test $db_found = no \
	    && test "$db_requested" = auto -o "$db_requested" = kc
    then
      if test -n "$with_kyotocabinet" && test "$with_kyotocabinet" != "yes"
      then
        CPPFLAGS="$CPPFLAGS -I$with_kyotocabinet/include"
        LDFLAGS="$LDFLAGS -L$with_kyotocabinet/lib"
      fi

      AC_CHECK_HEADER(kclangc.h,
      AC_CHECK_LIB(kyotocabinet, kcdbopen,
        [MUTTLIBS="$MUTTLIBS -lkyotocabinet"
         AC_DEFINE(HAVE_KC, 1, [Kyoto Cabinet Support])
         db_found=kc],
        [CPPFLAGS="$OLDCPPFLAGS"
         LDFLAGS="$OLDLDFLAGS"]))
      if test "$db_requested" != auto && test "$db_found" != "$db_requested"
      then
        AC_MSG_ERROR([Kyoto Cabinet could not be used. Check config.log for details.])
      fi
    fi

    dnl -- Tokyo Cabinet --
    if test "x$with_tokyocabinet" != "xno" && test $db_found = no \
	    && test "$db_requested" = auto -o "$db_requested" = tc
    then
      if test -n "$with_tokyocabinet" && test "$with_tokyocabinet" != "yes"
      then
        CPPFLAGS="$CPPFLAGS -I$with_tokyocabinet/include"
        LDFLAGS="$LDFLAGS -L$with_tokyocabinet/lib"
      fi

      AC_CHECK_HEADER(tcbdb.h,
      AC_CHECK_LIB(tokyocabinet, tcbdbopen,
        [MUTTLIBS="$MUTTLIBS -ltokyocabinet"
         AC_DEFINE(HAVE_TC, 1, [Tokyo Cabinet Support])
         db_found=tc],
        [CPPFLAGS="$OLDCPPFLAGS"
         LDFLAGS="$OLDLDFLAGS"]))
      if test "$db_requested" != auto && test "$db_found" != "$db_requested"
      then
        AC_MSG_ERROR([Tokyo Cabinet could not be used. Check config.log for details.])
      fi
    fi

    dnl -- LMDB --
    if test x$with_lmdb != xno && test $db_found = no \
	    && test "$db_requested" = auto -o "$db_requested" = lmdb
    then
        if test -n "$with_lmdb" && test "$with_lmdb" != "yes"
        then
          CPPFLAGS="$CPPFLAGS -I$with_lmdb/include"
          LDFLAGS="$LDFLAGS -L$with_lmdb/lib"
        fi
        saved_LIBS="$LIBS"
        LIBS="$LIBS -llmdb"
        AC_CACHE_CHECK(for mdb_env_create, ac_cv_mdbenvcreate,[
            ac_cv_mdbenvcreate=no
            AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <lmdb.h>]], [[mdb_env_create(0);]])],[ac_cv_mdbenvcreate=yes],[])
        ])
        LIBS="$saved_LIBS"
        if test "$ac_cv_mdbenvcreate" = yes
        then
          AC_DEFINE(HAVE_LMDB, 1, [LMDB Support])
          MUTTLIBS="$MUTTLIBS -llmdb"
          db_found=lmdb
        fi
        if test "$db_requested" != auto && test "$db_found" != "$db_requested"
        then
          AC_MSG_ERROR([LMDB could not be used. Check config.log for details.])
        fi
    fi

    dnl -- QDBM --
    if test "$with_qdbm" != "no" && test $db_found = no \
	    && test "$db_requested" = auto -o "$db_requested" = qdbm
    then
      if test -n "$with_qdbm" && test "$with_qdbm" != "yes"
      then
        if test -d $with_qdbm/include/qdbm; then
          CPPFLAGS="$CPPFLAGS -I$with_qdbm/include/qdbm"
	else
          CPPFLAGS="$CPPFLAGS -I$with_qdbm/include"
	fi
        LDFLAGS="$LDFLAGS -L$with_qdbm/lib"
      else
         if test -d /usr/include/qdbm; then
           CPPFLAGS="$CPPFLAGS -I/usr/include/qdbm"
	 fi
      fi

      saved_LIBS="$LIBS"
      AC_CHECK_HEADERS(villa.h)
      AC_CHECK_LIB(qdbm, vlopen,
        [MUTTLIBS="$MUTTLIBS -lqdbm"
         AC_DEFINE(HAVE_QDBM, 1, [QDBM Support])
         db_found=qdbm],
        [CPPFLAGS="$OLDCPPFLAGS"
         LDFLAGS="$OLDLDFLAGS"])
      LIBS="$saved_LIBS"
      if test "$db_requested" != auto && test "$db_found" != "$db_requested"
      then
        AC_MSG_ERROR([QDBM could not be used. Check config.log for details.])
      fi
    fi

    dnl -- GDBM --
    if test x$with_gdbm != xno && test $db_found = no \
	    && test "$db_requested" = auto -o "$db_requested" = gdbm
    then
        if test -n "$with_gdbm" && test "$with_gdbm" != "yes"
        then
          CPPFLAGS="$CPPFLAGS -I$with_gdbm/include"
          LDFLAGS="$LDFLAGS -L$with_gdbm/lib"
        fi
        saved_LIBS="$LIBS"
        LIBS="$LIBS -lgdbm"
        AC_CACHE_CHECK(for gdbm_open, ac_cv_gdbmopen,[
            ac_cv_gdbmopen=no
            AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <gdbm.h>]], [[gdbm_open(0,0,0,0,0);]])],[ac_cv_gdbmopen=yes],[])
        ])
        LIBS="$saved_LIBS"
        if test "$ac_cv_gdbmopen" = yes
        then
          AC_DEFINE(HAVE_GDBM, 1, [GDBM Support])
          MUTTLIBS="$MUTTLIBS -lgdbm"
          db_found=gdbm
        fi
        if test "$db_requested" != auto && test "$db_found" != "$db_requested"
        then
          AC_MSG_ERROR([GDBM could not be used. Check config.log for details.])
        fi
    fi

    dnl -- BDB --
    if test x$with_bdb != xno && test $db_found = no \
	    && test "$db_requested" = auto -o "$db_requested" = bdb
    then
        if test -n "$with_bdb" && test "$with_bdb" != "yes"
        then
          CPPFLAGS="$CPPFLAGS -I$with_bdb/include"
          LDFLAGS="$LDFLAGS -L$with_bdb/lib"
        fi
        saved_LIBS="$LIBS"
        LIBS="$LIBS -ldb"
        AC_CACHE_CHECK([for BDB > 4.0], ac_cv_dbcreate, [
          ac_cv_dbcreate=no
          AC_LINK_IFELSE([AC_LANG_PROGRAM([[
                          #include <stdlib.h>
                          #include <db.h>
                          ]], [[
                          DB *db = NULL;
                          db->open(db,NULL,NULL,NULL,0,0,0);
                          ]])],[ac_cv_dbcreate=yes],[])

          ])
        LIBS="$saved_LIBS"
        if test "x$ac_cv_dbcreate" = "xyes"
        then
            AC_DEFINE(HAVE_DB4, 1, [Berkeley DB4 Support])
            MUTTLIBS="$MUTTLIBS -ldb"
            db_found=bdb
        fi
        if test "$db_requested" != auto && test "$db_found" != "$db_requested"
        then
          AC_MSG_ERROR([BDB could not be used. Check config.log for details.])
        fi
    fi

    if test $db_found = no
    then
        AC_MSG_ERROR([You need Kyoto Cabinet, Tokyo Cabinet, LMDB, QDBM, GDBM, or BDB for hcache])
    fi
fi
dnl -- end cache --

AM_CONDITIONAL(BUILD_HCACHE, test x$db_found != xno)

if test "$need_md5" = "yes"
then
  MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS md5.o"
fi

AC_SUBST(MUTTLIBS)
AC_SUBST(MUTT_LIB_OBJECTS)
AC_SUBST(LIBIMAP)
AC_SUBST(LIBIMAPDEPS)

dnl -- iconv/gettext --

AC_ARG_ENABLE(iconv, AS_HELP_STRING([--disable-iconv],[Disable iconv support]),
        [if test x$enableval = xno ; then
                am_cv_func_iconv=no
        fi
])

AM_GNU_GETTEXT([external])
AM_ICONV

if test "$am_func_iconv" != "yes"
then
  AC_MSG_WARN([Configuring without iconv support. See INSTALL for details])
else

AC_CHECK_HEADERS(iconv.h,
        [AC_MSG_CHECKING(whether iconv.h defines iconv_t)
         AC_EGREP_HEADER([typedef.*iconv_t],iconv.h,
                [AC_MSG_RESULT(yes)
                 AC_DEFINE(HAVE_ICONV_T_DEF, 1,
                        [Define if <iconv.h> defines iconv_t.])],
                 AC_MSG_RESULT(no))])

dnl (1) Some implementations of iconv won't convert from UTF-8 to UTF-8.
dnl (2) In glibc-2.1.2 and earlier there is a bug that messes up ob and
dnl     obl when args 2 and 3 are 0 (fixed in glibc-2.1.3).
AC_CACHE_CHECK([whether this iconv is good enough], mutt_cv_iconv_good,
        mutt_save_LIBS="$LIBS"
        LIBS="$LIBS $LIBICONV"
        AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <iconv.h>
int main()
{
  iconv_t cd;
changequote(, )dnl
  char buf[4];
changequote([, ])dnl
  char *ob;
  size_t obl;
  ob = buf, obl = sizeof(buf);
  return ((cd = iconv_open("UTF-8", "UTF-8")) != (iconv_t)(-1) &&
          (iconv(cd, 0, 0, &ob, &obl) ||
           !(ob == buf && obl == sizeof(buf)) ||
           iconv_close(cd)));
}
                ]])],[mutt_cv_iconv_good=yes],[mutt_cv_iconv_good=no],[mutt_cv_iconv_good=yes])
        LIBS="$mutt_save_LIBS")
if test "$mutt_cv_iconv_good" = no; then
  AC_MSG_ERROR(Try using libiconv instead)
fi

dnl This is to detect implementations such as the one in glibc-2.1,
dnl which always convert exactly but return the number of characters
dnl converted instead of the number converted inexactly.
AC_CACHE_CHECK([whether iconv is non-transcribing], mutt_cv_iconv_nontrans,
        mutt_save_LIBS="$LIBS"
        LIBS="$LIBS $LIBICONV"
        AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <iconv.h>
#include <string.h>
int main()
{
  iconv_t cd;
  char *ib;
  char *ob;
  size_t ibl, obl;
  char *s = (char *) "\304\211";
changequote(, )dnl
  char t[3];
changequote([, ])dnl
  ib = s, ibl = 2, ob = t, obl = 3;
  return ((cd = iconv_open("UTF-8", "UTF-8")) == (iconv_t)(-1) ||
          iconv(cd, &ib, &ibl, &ob, &obl));
}
                ]])],[mutt_cv_iconv_nontrans=no],[mutt_cv_iconv_nontrans=yes],[mutt_cv_iconv_nontrans=no])
        LIBS="$mutt_save_LIBS")
if test "$mutt_cv_iconv_nontrans" = yes; then
  AC_DEFINE(ICONV_NONTRANS, 1)
else
  AC_DEFINE(ICONV_NONTRANS, 0)
fi

mutt_save_LIBS="$LIBS"
LIBS="$LIBS $LIBINTL"
AC_CHECK_FUNCS(bind_textdomain_codeset)
LIBS="$mutt_save_LIBS"

fi # libiconv

dnl -- IDN depends on iconv

dnl mutt_idna.c will perform charset transformations (for smtputf8
dnl support) as long as at least iconv is installed.  If there is no
dnl iconv, then it doesn't need to be included in the build.
if test "$am_func_iconv" = yes; then
  MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS mutt_idna.o"
fi

AC_ARG_WITH(idn, AS_HELP_STRING([--with-idn=@<:@PFX@:>@],[Use GNU libidn for internationalized domain names]),
		 [
		  if test "$with_idn" != "no" ; then
		  	if test "$with_idn" != "yes" ; then
			   CPPFLAGS="$CPPFLAGS -I$with_idn/include"
			   LDFLAGS="$LDFLAGS -L$with_idn/lib"
			fi
		  fi
		 ],
		 [with_idn=auto])

AC_ARG_WITH(idn2, AS_HELP_STRING([--with-idn2=@<:@PFX@:>@],[Use GNU libidn2 for internationalized domain names]),
        [
          if test "$with_idn2" != "no" ; then
            if test "$with_idn" = "auto"; then
              with_idn="no"
            fi

            if test "$with_idn" != "no"; then
              AC_MSG_ERROR([Cannot enable IDN and IDN2 support at the same time])
            fi

            if test "$with_idn2" != "yes" ; then
              CPPFLAGS="$CPPFLAGS -I$with_idn2/include"
              LDFLAGS="$LDFLAGS -L$with_idn2/lib"
            fi
          fi
        ],
        [with_idn2=no])

if test "x$with_idn" != "xno"; then
  if test "$am_func_iconv" != "yes"; then
    if test "$with_idn" != "auto"; then
      AC_MSG_ERROR([IDN requested but iconv is disabled or unavailable])
    fi
  else
    dnl Solaris 11 has /usr/include/idn
    have_stringprep_h=no
    AC_CHECK_HEADERS([stringprep.h idn/stringprep.h], [
		      have_stringprep_h=yes
		      break])
    have_idna_h=no
    AC_CHECK_HEADERS([idna.h idn/idna.h], [
		      have_idna_h=yes
		      break])

    mutt_save_LIBS="$LIBS"
    LIBS=

    AC_SEARCH_LIBS([stringprep_check_version], [idn], [
      AC_DEFINE([HAVE_LIBIDN], 1, [Define to 1 if you have the GNU idn library])
      MUTTLIBS="$MUTTLIBS $LIBS"

      LIBS="$LIBS $LIBICONV"
      AC_CHECK_FUNCS(idna_to_unicode_utf8_from_utf8 idna_to_unicode_8z8z)
      AC_CHECK_FUNCS(idna_to_ascii_from_utf8 idna_to_ascii_8z)
      AC_CHECK_FUNCS(idna_to_ascii_lz idna_to_ascii_from_locale)
    ])

    LIBS="$mutt_save_LIBS"

    if test "$with_idn" != auto; then
      if test $have_stringprep_h = no || test $have_idna_h = no || test $ac_cv_search_stringprep_check_version = no; then
	AC_MSG_ERROR([IDN was requested, but libidn was not usable on this system])
      fi
    fi
  fi
fi

dnl idna2
if test "x$with_idn2" != "xno"; then
  if test "$am_func_iconv" != "yes"; then
    AC_MSG_ERROR([IDN2 requested but iconv is disabled or unavailable])
  else
    dnl Solaris 11 has /usr/include/idn
    have_idn2_h=no
    AC_CHECK_HEADERS([idn2.h idn/idn2.h], [
              have_idn2_h=yes
              break])

    mutt_save_LIBS="$LIBS"
    LIBS=

    AC_SEARCH_LIBS([idn2_check_version], [idn2], [
      AC_DEFINE([HAVE_LIBIDN2], 1, [Define to 1 if you have the GNU idn2 library])
      MUTTLIBS="$MUTTLIBS $LIBS"

      dnl -lunistring is needed for static linking, and has to come
      dnl after the -lidn2
      AC_SEARCH_LIBS([u8_strconv_from_locale], [unistring], [
            if test "$ac_cv_search_u8_strconv_from_locale" != "none required"; then
              MUTTLIBS="$MUTTLIBS -lunistring"
            fi
      ])

      dnl libidn2 >= 2.0.0 declares compatibility macros in idn2.h
      LIBS="$LIBS $LIBICONV"
      AC_CHECK_DECL([idna_to_unicode_8z8z],
                    [AC_DEFINE([HAVE_IDNA_TO_UNICODE_8Z8Z])], [],
                    [[
#if defined(HAVE_IDN2_H)
#include <idn2.h>
#elif defined(HAVE_IDN_IDN2_H)
#include <idn/idn2.h>
#endif
        ]])
      AC_CHECK_DECL([idna_to_ascii_8z],
                    [AC_DEFINE([HAVE_IDNA_TO_ASCII_8Z])], [],
                    [[
#if defined(HAVE_IDN2_H)
#include <idn2.h>
#elif defined(HAVE_IDN_IDN2_H)
#include <idn/idn2.h>
#endif
        ]])
      AC_CHECK_DECL([idna_to_ascii_lz],
                    [AC_DEFINE([HAVE_IDNA_TO_ASCII_LZ])], [],
                    [[
#if defined(HAVE_IDN2_H)
#include <idn2.h>
#elif defined(HAVE_IDN_IDN2_H)
#include <idn/idn2.h>
#endif
        ]])
    ])

    LIBS="$mutt_save_LIBS"

    if test "$have_idn2_h" = "no" || \
       test "$ac_cv_search_idn2_check_version" = "no" || \
       test "x$ac_cv_have_decl_idna_to_unicode_8z8z" != "xyes" || \
       test "x$ac_cv_have_decl_idna_to_ascii_8z" != "xyes" || \
       test "x$ac_cv_have_decl_idna_to_ascii_lz" != "xyes"
    then
      AC_MSG_ERROR([IDN2 was requested, but libidn2 was not usable on this system])
    fi
  fi
fi

dnl -- locales --

AC_CHECK_HEADERS(wchar.h)

AC_CACHE_CHECK([for wchar_t], mutt_cv_wchar_t,
        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <stddef.h>
#include <stdlib.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
                ]], [[ wchar_t wc; return 0; ]])],[mutt_cv_wchar_t=yes],[mutt_cv_wchar_t=no]))

if test "$mutt_cv_wchar_t" = no; then
        AC_DEFINE(wchar_t,int,[ Define to 'int' if system headers don't define. ])
fi

AC_CACHE_CHECK([for wint_t], mutt_cv_wint_t,
        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <stddef.h>
#include <stdlib.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
                ]], [[ wint_t wc; return 0; ]])],[mutt_cv_wint_t=yes],[mutt_cv_wint_t=no]))

if test "$mutt_cv_wint_t" = no; then
        AC_DEFINE(wint_t,int,[ Define to 'int' if system headers don't define. ])
fi

AC_CHECK_HEADERS(wctype.h)
AC_CHECK_FUNCS(iswalnum iswalpha iswblank iswcntrl iswdigit)
AC_CHECK_FUNCS(iswgraph iswlower iswprint iswpunct iswspace iswupper)
AC_CHECK_FUNCS(iswxdigit towupper towlower)

AC_CACHE_CHECK([for mbstate_t], mutt_cv_mbstate_t,
        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <stddef.h>
#include <stdlib.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
                ]], [[ mbstate_t s; return 0; ]])],[mutt_cv_mbstate_t=yes],[mutt_cv_mbstate_t=no]))

if test "$mutt_cv_mbstate_t" = no; then
        AC_DEFINE(mbstate_t,int,[ Define to 'int' if system headers don't define. ])
fi

wc_funcs=maybe
AC_ARG_WITH(wc-funcs, AS_HELP_STRING([--without-wc-funcs],[Do not use the system's wchar_t functions]),
        wc_funcs=$withval)

if test "$wc_funcs" != yes && test "$wc_funcs" != no; then
        AC_CACHE_CHECK([for wchar_t functions], mutt_cv_wc_funcs,
                mutt_cv_wc_funcs=no
                AC_LINK_IFELSE([AC_LANG_PROGRAM([[
#define _XOPEN_SOURCE 600
#include <stddef.h>
#include <stdlib.h>
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
#ifdef HAVE_WCTYPE_H
#include <wctype.h>
#endif]], [[mbrtowc(0, 0, 0, 0); wctomb(0, 0); wcwidth(0); iswblank(0);
        iswprint(0); iswspace(0); towlower(0); towupper(0); iswalnum(0)]])],[mutt_cv_wc_funcs=yes],[]))
        wc_funcs=$mutt_cv_wc_funcs
fi

if test $wc_funcs = yes; then
        AC_DEFINE(HAVE_WC_FUNCS,1,[ Define if you are using the system's wchar_t functions. ])
else
	MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS utf8.o wcwidth.o"
fi

AC_CACHE_CHECK([for nl_langinfo and CODESET], mutt_cv_langinfo_codeset,
  [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <langinfo.h>]], [[char* cs = nl_langinfo(CODESET);]])],[mutt_cv_langinfo_codeset=yes],[mutt_cv_langinfo_codeset=no])])
if test $mutt_cv_langinfo_codeset = yes; then
  AC_DEFINE(HAVE_LANGINFO_CODESET,1,[ Define if you have <langinfo.h> and nl_langinfo(CODESET). ])
fi

AC_CACHE_CHECK([for nl_langinfo and YESEXPR], mutt_cv_langinfo_yesexpr,
  [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <langinfo.h>]], [[char* cs = nl_langinfo(YESEXPR);]])],[mutt_cv_langinfo_yesexpr=yes],[mutt_cv_langinfo_yesexpr=no])])
if test $mutt_cv_langinfo_yesexpr = yes; then
  AC_DEFINE(HAVE_LANGINFO_YESEXPR,1,[ Define if you have <langinfo.h> and nl_langinfo(YESEXPR). ])
fi

dnl Documentation tools
have_openjade="no"
AC_PATH_PROG([OSPCAT], [ospcat], [none])
if test "$OSPCAT" != "none"
then
  AC_MSG_CHECKING([for openjade docbook stylesheets])
  dslosfile=`ospcat --public-id="-//Norman Walsh//DOCUMENT DocBook Print Stylesheet//EN"`
  DSLROOT=`echo $dslosfile | sed -n -e "s/.*SOIBASE='\(@<:@^'@:>@*\)\/catalog'.*/\1/p"`
  # ospcat may spit out an absolute path without an SOIBASE
  if test -z "$DSLROOT"
  then
    DSLROOT=`echo $dslosfile | sed -e 's|<OSFILE>\(.*\)/print/docbook.dsl|\1|'`
  fi
  if test -f $DSLROOT/print/docbook.dsl
  then
    AC_MSG_RESULT([in $DSLROOT])
    have_openjade="yes"
  else
    AC_MSG_RESULT([not found: PDF documentation will not be built.])
  fi
fi
AC_SUBST(DSLROOT)

AC_PATH_PROGS([DB2XTEXI], [docbook2x-texi db2x_docbook2texi docbook2texi], [none])
if test "$DB2XTEXI" != "none"; then
   AC_PATH_PROG([MAKEINFO], [makeinfo], [none])
   if test "$MAKEINFO" != "none"; then
      do_build_info=yes
   fi
fi
AM_CONDITIONAL(BUILD_INFO, test x$do_build_info = xyes)
AC_SUBST(DB2XTEXI)
AC_SUBST(MAKEINFO)

AC_ARG_ENABLE(doc, AS_HELP_STRING([--disable-doc],[Do not build the documentation]),
[       if test x$enableval = xno ; then
                do_build_doc=no
        fi
])
AM_CONDITIONAL(BUILD_DOC, test x$do_build_doc != xno)

AC_ARG_ENABLE(full_doc,
	      AS_HELP_STRING([--disable-full-doc],[Omit disabled variables]),
[       if test x$enableval = xno ; then
                full_doc=no
        fi
])
if test x$full_doc != xno ; then
  AC_DEFINE(MAKEDOC_FULL,1, [Define if you want complete documentation.])
fi

AC_CONFIG_FILES(Makefile contrib/Makefile doc/Makefile imap/Makefile
        m4/Makefile po/Makefile.in autocrypt/Makefile
        doc/instdoc.sh)
AC_OUTPUT
