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

dnl
dnl Copyright (c) 1998-2000 Carnegie Mellon University.  All rights reserved.
dnl
dnl Redistribution and use in source and binary forms, with or without
dnl modification, are permitted provided that the following conditions
dnl are met:
dnl
dnl 1. Redistributions of source code must retain the above copyright
dnl    notice, this list of conditions and the following disclaimer. 
dnl
dnl 2. Redistributions in binary form must reproduce the above copyright
dnl    notice, this list of conditions and the following disclaimer in
dnl    the documentation and/or other materials provided with the
dnl    distribution.
dnl
dnl 3. The name "Carnegie Mellon University" must not be used to
dnl    endorse or promote products derived from this software without
dnl    prior written permission. For permission or any other legal
dnl    details, please contact  
dnl      Office of Technology Transfer
dnl      Carnegie Mellon University
dnl      5000 Forbes Avenue
dnl      Pittsburgh, PA  15213-3890
dnl      (412) 268-4387, fax: (412) 268-7395
dnl      tech-transfer@andrew.cmu.edu
dnl
dnl 4. Redistributions of any form whatsoever must retain the following
dnl    acknowledgment:
dnl    \"This product includes software developed by Computing Services
dnl     at Carnegie Mellon University (http://www.cmu.edu/computing/).\"
dnl
dnl CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
dnl THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dnl AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
dnl FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
dnl WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
dnl AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
dnl OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
dnl

dnl
dnl configure.in for the Cyrus imapd
dnl $Id: configure.in,v 1.198 2001/06/23 01:01:47 ken3 Exp $
dnl


AC_INIT(imap/imapd.c)
AC_CONFIG_HEADER(config.h)
AC_CANONICAL_HOST

AC_CHECK_PROG(MAKEDEPEND,makedepend,makedepend,[`cd ${srcdir};pwd`/tools/not-mkdep])
if test "$MAKEDEPEND" != "makedepend"; then
        AC_MSG_WARN([Makedepend is not installed on this system.  You should compile and install the version from the makedepend subdirectory.])
fi

AC_ARG_WITH(login,,AC_ERROR([--with-login is no longer supported.
Configure SASL appropriately instead.]))

AC_ARG_WITH(cyrus-prefix,[  --with-cyrus-prefix=DIR    use DIR as cyrus install directory],
	cyrus_prefix="$withval",cyrus_prefix="/usr/cyrus")
AC_SUBST(cyrus_prefix)
AC_DEFINE_UNQUOTED(CYRUS_PATH,"$cyrus_prefix")
AC_ARG_WITH(cyrus-user,[  --with-cyrus-user=USERID   use USERID cyrus userid],
	cyrus_user="$withval",cyrus_user="cyrus")
AC_SUBST(cyrus_user)
AC_DEFINE_UNQUOTED(CYRUS_USER, "$cyrus_user")
AC_ARG_WITH(cyrus-group,[  --with-cyrus-group=GROUPID use GROUPID cyrus group],
	cyrus_group="$withval",cyrus_group="mail")
AC_SUBST(cyrus_group)
AC_ARG_WITH(statedir,[  --with-statedir=DIR     use DIR as state directory],
	statedir="$withval",statedir="/var")
AC_DEFINE_UNQUOTED(STATEDIR,"$statedir")

AC_PROG_CC
AC_PROG_RANLIB
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_AIX
AC_ISC_POSIX
AC_PROG_AWK
AC_C_CONST
AC_SYS_LONG_FILE_NAMES
if test $ac_cv_sys_long_file_names = no; then
	AC_MSG_ERROR(The Cyrus IMAPD requires support for long file names)
fi

dnl check for -R, etc. switch
CMU_GUESS_RUNPATH_SWITCH
dnl let's just link against local.  otherwise we never find anything useful.
CPPFLAGS="-I/usr/local/include ${CPPFLAGS}"
CMU_ADD_LIBPATH("/usr/local/lib")

AC_CHECK_HEADERS(unistd.h sys/select.h sys/param.h)
AC_REPLACE_FUNCS(memmove strcasecmp ftruncate strerror)
AC_HEADER_DIRENT

dnl this is to check for time things for sieve
AC_STRUCT_TM
AC_STRUCT_TIMEZONE

AC_SUBST(CPPFLAGS)
AC_SUBST(PRE_SUBDIRS)
AC_SUBST(EXTRA_SUBDIRS)
AC_SUBST(DEPLIBS)
AC_SUBST(LOCALDEFS)
AC_FUNC_VPRINTF

AC_ARG_WITH(dbdir,[  --with-dbdir=DIR       Berkeley DB in <DIR>],
  LDFLAGS="-L$withval/lib $LDFLAGS"
  CMU_ADD_LIBPATH($withval/lib)
  if test -d $withval/include/db3; then
    CPPFLAGS="-I$withval/include/db3 $CPPFLAGS"
  else
    CPPFLAGS="-I$withval/include $CPPFLAGS"
  fi,
  # otherwise, check for ../include/db3
  for db3loc in $prefix /usr/local /usr
    do
      if test -d $db3loc/include/db3; then
	CPPFLAGS="-I$db3loc/include/db3 $CPPFLAGS"
	break
      fi
    done)
#Try to detect the name of the DB3 library
dbfound="no"
for dbname in db-3.2 db3.2 db-3.1 db3.1 db-3 db3 db
  do
    AC_CHECK_LIB($dbname, db_create, [AC_DEFINE(HAVE_LIBDB)
				      LIBS=" -l$dbname ${LIBS}"
				      dbfound="yes"
				      break], dbfound="no")
  done
if test "$dbfound" = "no"; then
  AC_ERROR([this version requires Berkeley DB 3.x.
(Get it from http://www.sleepycat.com/.)])
fi

sievedir="sieve"
AC_ARG_ENABLE(sieve,
        [  --disable-sieve         disable Sieve support],
	if test "$enableval" = no; then
 		sievedir="no"
	fi)

if test "$sievedir" != "no"; then
	SIEVE_LIBS="../${sievedir}/libsieve.a"
	AC_SUBST(SIEVE_LIBS)
	SIEVE_CPPFLAGS="-I\$(srcdir)/../$sievedir"
	AC_SUBST(SIEVE_CPPFLAGS)
	AC_DEFINE(USE_SIEVE)

	dnl Sieve configure stuff
	AC_PROG_YACC
	AC_PROG_LEX
	AC_CHECK_LIB(fl,main)

 	AC_SEARCH_LIBS(regcomp, rx regex,
 			AC_DEFINE(ENABLE_REGEX)
		 	AC_CHECK_HEADER(rxposix.h, AC_DEFINE(HAVE_RX)))
  		

	PRE_SUBDIRS="${PRE_SUBDIRS} $sievedir"
	EXTRA_OUTPUT="${EXTRA_OUTPUT} $sievedir/Makefile"
fi

AC_ARG_WITH(perl,       [  --with-perl=PERL           use PERL for perl],
	with_perl="$withval", with_perl="perl5")
if test "${with_perl}" = yes; then
    with_perl="perl5"
fi
if test "${with_perl}" != no; then
    AC_CHECK_PROGS(PERL, ${with_perl} perl, with_perl=no)
fi
if test "$with_perl" = "no"; then
    AC_MSG_WARN(No Perl found: administrative tools won't be available)
else
dnl compile perl stuff
    EXTRA_SUBDIRS="${EXTRA_SUBDIRS} perl"
dnl and compile perl/cyradm
    PERL_SUBDIRS="imap"
fi

dnl for timsieved
if test "$sievedir" != "no"; then
	EXTRA_SUBDIRS="${EXTRA_SUBDIRS} timsieved"
	EXTRA_OUTPUT="${EXTRA_OUTPUT} timsieved/Makefile"

	PERL_SUBDIRS="${PERL_SUBDIRS} sieve"
	EXTRA_OUTPUT="${EXTRA_OUTPUT} perl/sieve/Makefile perl/sieve/lib/Makefile"
fi

dnl for et routines
AC_FUNC_CHECK(strerror,AC_DEFINE(HAS_STRERROR),AC_DEFINE(NEED_SYS_ERRLIST))

dnl for master fd limits
AC_CHECK_HEADERS(sys/resource.h)
AC_CHECK_FUNCS(setrlimit)

AC_EGREP_HEADER(socklen_t, sys/socket.h, AC_DEFINE(HAVE_SOCKLEN_T))
AC_EGREP_HEADER(rlim_t, sys/resource.h, AC_DEFINE(HAVE_RLIM_T))

dnl Bunch of setproctitle stuff
spt_type=""
AC_CHECK_FUNC(setproctitle,spt_type=SPT_BUILTIN)
if test "$spt_type" = ""; then
	dnl BSD/OS and FreeBSD put it in -lutil
	AC_CHECK_LIB(util,setproctitle,spt_type=SPT_BUILTIN
			LIBS="${LIBS} -lutil")
fi
if test "$spt_type" = ""; then
	AC_CHECK_HEADER(sys/pstat.h,spt_type=SPT_PSTAT)
fi
if test "$spt_type" = ""; then
	AC_CHECK_HEADER(sys/sysnews.h,spt_type=SPT_SYSMIPS)
fi
if test "$spt_type" = ""; then
	AC_MSG_CHECKING(for PS_STRINGS)
	AC_CACHE_VAL(cyrus_cv_sys_psstrings, AC_TRY_CPP([
#include <machine/vmparam.h>
#include <sys/exec.h>
#ifndef PS_STRINGS
#include </nonexistent>
#endif],cyrus_cv_sys_psstrings=yes,cyrus_cv_sys_psstrings=no))
	if test $cyrus_cv_sys_psstrings = yes; then
		spt_type=SPT_PSSTRINGS
	fi
	AC_MSG_RESULT($cyrus_cv_sys_psstrings)
fi
if test "$spt_type" = ""; then
	AC_MSG_CHECKING(for SCO)
	AC_CACHE_VAL(cyrus_cv_sys_sco, AC_TRY_CPP([
#ifndef _SCO_unix_
#include </nonexistent>
#endif],cyrus_cv_sys_sco=yes,cyrus_cv_sys_sco=no))
	if test $cyrus_cv_sys_sco = yes; then
		spt_type=SPT_SCO
	fi
	AC_MSG_RESULT($cyrus_cv_sys_sco)
fi
if test "$spt_type" = ""; then
	AC_MSG_CHECKING(for setproctitle usability)
	AC_CACHE_VAL(cyrus_cv_sys_setproctitle, AC_TRY_CPP([
#if defined(DGUX) || defined(_SEQUENT_) || defined(apollo)
#include </nonexistent>
#endif],cyrus_cv_sys_setproctitle=yes,cyrus_cv_sys_setproctitle=no))
	if test $cyrus_cv_sys_setproctitle = no; then
		spt_type=SPT_NONE
	fi
	AC_MSG_RESULT($cyrus_cv_sys_setproctitle)
fi
if test "$spt_type" != ""; then
	AC_DEFINE_UNQUOTED(SPT_TYPE,$spt_type)
fi

AC_MSG_CHECKING(nonblocking method)
AC_CACHE_VAL(cyrus_cv_sys_nonblock,AC_TRY_LINK([#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#ifndef	FNDELAY
#define FNDELAY		O_NDELAY
#endif],[fcntl(0, F_GETFL, 0)&FNDELAY],
cyrus_cv_sys_nonblock=fcntl,cyrus_cv_sys_nonblock=ioctl))
WITH_NONBLOCK=$cyrus_cv_sys_nonblock
AC_SUBST(WITH_NONBLOCK)
AC_MSG_RESULT($WITH_NONBLOCK)

AC_MSG_CHECKING(timezone GMT offset method)
AC_CACHE_VAL(cyrus_cv_struct_sys_gmtoff,AC_TRY_COMPILE([
#include <time.h>],[struct tm tm;
tm.tm_gmtoff = 0;
],cyrus_cv_struct_sys_gmtoff=tm,cyrus_cv_struct_sys_gmtoff=gmtime))
WITH_GMTOFF=$cyrus_cv_struct_sys_gmtoff
AC_SUBST(WITH_GMTOFF)
AC_MSG_RESULT($WITH_GMTOFF)
AC_MSG_CHECKING(for shared mmap)
AC_CACHE_VAL(cyrus_cv_func_mmap_shared,AC_TRY_RUN([
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
main() {
char *base;
int fd = open("conftestmmap", O_RDWR|O_CREAT|O_TRUNC, 0666);
if (fd == -1) exit(1);
if (write(fd, "test", 4) != 4) exit(1);
fsync(fd);
base = mmap((caddr_t)0, 100, PROT_READ, MAP_SHARED
#ifdef MAP_FILE
| MAP_FILE
#endif
#ifdef MAP_VARIABLE
| MAP_VARIABLE
#endif
, fd, 0L);
if (base == (caddr_t)-1) exit(1);
if (strncmp(base, "test", 4) != 0) exit(1);
if (write(fd, "test", 4) != 4) exit(1);
fsync(fd);
if (strncmp(base+4, "test", 4) != 0) exit(1);
exit(0);}
],cyrus_cv_func_mmap_shared=yes,cyrus_cv_func_mmap_shared=no,
cyrus_cv_func_mmap_shared=no))
AC_MSG_RESULT($cyrus_cv_func_mmap_shared)
if test $cyrus_cv_func_mmap_shared = yes; then
	WITH_MAP="shared"
else
AC_MSG_CHECKING(for stupid shared mmap)
AC_CACHE_VAL(cyrus_cv_func_mmap_stupidshared,AC_TRY_RUN([
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
main() {
char *base;
int fd = open("conftestmmap", O_RDWR|O_CREAT|O_TRUNC, 0666);
if (fd == -1) exit(1);
if (write(fd, "test", 4) != 4) exit(1);
fsync(fd);
base = mmap((caddr_t)0, 4, PROT_READ, MAP_SHARED
#ifdef MAP_FILE
| MAP_FILE
#endif
#ifdef MAP_VARIABLE
| MAP_VARIABLE
#endif
, fd, 0L);
if (base == (caddr_t)-1) exit(1);
if (strncmp(base, "test", 4) != 0) exit(1);
lseek(fd, 0L, 0);	
if (write(fd, "xyzz", 4) != 4) exit(1);
fsync(fd);
if (strncmp(base, "xyzz", 4) != 0) exit(1);
exit(0);}
],cyrus_cv_func_mmap_stupidshared=yes,cyrus_cv_func_mmap_stupidshared=no,
cyrus_cv_func_mmap_stupidshared=no))
AC_MSG_RESULT($cyrus_cv_func_mmap_stupidshared)
if test $cyrus_cv_func_mmap_stupidshared = yes; then
	WITH_MAP="stupidshared"
else
	AC_MSG_WARN(*** This system does not have a working mmap())
	AC_MSG_WARN(*** Expect a considerable performance penalty)
	WITH_MAP=nommap
fi
fi

AC_SUBST(WITH_MAP)
AC_ARG_WITH(lock,
  [  --with-lock=METHOD      force use of METHOD for locking (flock or fcntl)],
  WITH_LOCK="$withval",
  AC_CHECK_FUNC(flock,WITH_LOCK="flock",WITH_LOCK="fcntl"))
AC_SUBST(WITH_LOCK)

dnl for makedepend and AFS.
cant_find_sigvec=no
AC_CACHE_VAL(cyrus_sigveclib,[
 dnl bsd classic flavor
 AC_CHECK_FUNC(sigvec, [
	cyrus_sigveclib=""
  ], [
  dnl hp flavor
  AC_CHECK_LIB(BSD, sigvec, cyrus_sigveclib="-lBSD", [
    dnl not hp flavor
    SAVE_LDFLAGS="$LDFLAGS"
    dnl solaris flavor
    LDFLAGS="-L/usr/ucblib -R/usr/ucblib $LDFLAGS"
    AC_CHECK_LIB(ucb, sigvec, [
      dnl more solaris flavor
      cyrus_sigveclib="-lc -L/usr/ucblib -R/usr/ucblib -lucb"],
      [ cant_find_sigvec=yes ])
    LDFLAGS="$SAVE_LDFLAGS"])
  ])
])
AC_SUBST(cyrus_sigveclib)

# ok, we still look for this stuff because of checking groups, but
# all authentication goes through SASL

AC_ARG_WITH(afs,[  --with-afs=PATH         use AFS libraries from PATH],
	[with_afs="${withval}"
	CFLAGS="${CFLAGS} -I${withval}/include"],with_afs="/usr/local")

AC_ARG_WITH(auth,[  --with-auth=METHOD      use authorization module METHOD
                          METHOD is unix or krb],WITH_AUTH="$withval",
	if test -f /etc/krb.conf -o -f /etc/kerberosIV/krb.conf ; then
		WITH_AUTH="krb"
	else
		WITH_AUTH="unix"
	fi)
AC_SUBST(WITH_AUTH)
if test "$WITH_AUTH" = krb_pts; then
	EXTRA_SUBDIRS="${EXTRA_SUBDIRS} ptclient"
	EXTRA_OUTPUT="${EXTRA_OUTPUT} ptclient/Makefile"
	AFS_LIBS="${with_afs}/lib/afs/libkauth.a ${with_afs}/lib/afs/libprot.a  ${with_afs}/lib/afs/libauth.a ${with_afs}/lib/afs/libsys.a ${with_afs}/lib/librxkad.a ${with_afs}/lib/librx.a ${with_afs}/lib/afs/libsys.a ${with_afs}/lib/libubik.a  ${with_afs}/lib/liblwp.a ${with_afs}/lib/afs/util.a"
	if test -f ${with_afs}/lib/afs/libaudit.a; then
		AFS_LIBS="$AFS_LIBS ${with_afs}/lib/afs/libaudit.a"
	fi
	if test -f /usr/ucblib/libucb.a; then
		CMU_ADD_LIBPATH_TO(/usr/ucblib, AFS_LDFLAGS)
		AFS_LIBS="$AFS_LIBS -lc -lucb"
	fi

	AC_CACHE_VAL(cyrus_afs_pr_end,[
		SAVE_LIBS="$LIBS"
		LIBS="$AFS_LIBS"
		AC_MSG_CHECKING(if AFS libraries have pr_End)
		AC_TRY_LINK([pr_End();],
			[pr_End()],
			[
				dnl it linked; have it
				AC_MSG_RESULT(yes)
				cyrus_afs_pr_end="yes"
			], [
				dnl didn't link; use old code
				AC_MSG_RESULT(no)
				cyrus_afs_pr_end="no"
			])
		])
	LIBS="$SAVE_LIBS"
	if test "$cyrus_afs_pr_end" = no; then
		AC_DEFINE(HAVE_PR_END)
	fi
	AC_CACHE_VAL(cyrus_afs_sigvec,[
		SAVE_LIBS="$LIBS"
		LIBS="${with_afs}/lib/liblwp.a"
		AC_MSG_CHECKING(if AFS libraries need sigvec)
		dnl Does AFS need sigvec?  We have to link against lwp and see
		dnl if IOMGR_Initialize wants it
		AC_TRY_LINK([IOMGR_Initialize();],
			[IOMGR_Initialize()],
			[
				dnl it linked; don't need it
				AC_MSG_RESULT(no)
				cyrus_afs_sigvec="no"
			], [
				dnl didn't link; pick up sigvec
				AC_MSG_RESULT(yes)
				cyrus_afs_sigvec="yes"
			])
		])
	if test "$cyrus_afs_sigvec" = yes; then
	  if test "$cant_find_sigvec" = yes; then
            AC_MSG_WARN([Can't find a sigvec for AFS libraries which seem to])
	    AC_MSG_WARN([need one; ptloader may not build.])
          else
	    AFS_LIBS="${AFS_LIBS} $cyrus_sigveclib"
	  fi
	fi
	LIBS="$SAVE_LIBS"
	AC_SUBST(AFS_LIBS)
	AC_SUBST(AFS_LDFLAGS)
fi

SERVER_SUBDIRS="master imap"
AC_ARG_ENABLE(server,
	[  --disable-server        disable compiling servers],
	if test "$enableval" = no; then
        	SERVER_SUBDIRS=""
	fi)
AC_SUBST(SERVER_SUBDIRS)
# We always output a server makefile (just because we can)

dnl this is the new simple check for kerberos; since the person had to
dnl compile SASL, we might as well use the same checks.
AC_ARG_WITH(krb,[  --with-krb=PATH         use Kerberos from PATH],
	with_krb="$withval", with_krb="yes")

if test "$WITH_AUTH" = "krb" -o "$WITH_AUTH" = "krb_pts" ; then
  dnl In order to compile kerberos4, we need libkrb and libdes.

  dnl we might need -lresolv for kerberos
  AC_CHECK_LIB(resolv,res_search)

  AC_CHECK_LIB(des,des_ecb_encrypt, LIBS="${LIBS} -ldes",
 AC_MSG_ERROR([The Kerberos DES library is required for Kerberos support.]))

  dnl if we were ambitious, we'd look more aggressively for the
  dnl krb4 install
  if test -d ${with_krb}; then
     AC_CACHE_CHECK(for Kerberos includes, cyrus_krbinclude, [
       for krbhloc in include/kerberosIV include
       do
         if test -f ${with_krb}/${krbhloc}/krb.h ; then
           cyrus_krbinclude=${with_krb}/${krbhloc}
           break
         fi
       done
       ])

     if test -n "${cyrus_krbinclude}"; then
       CPPFLAGS="$CPPFLAGS -I${cyrus_krbinclude}"
     fi
     LDFLAGS="$LDFLAGS -L${with_krb}/lib"
  fi

  if test "$with_des" != no; then
    AC_CHECK_HEADER(krb.h,
      AC_CHECK_LIB(krb, krb_mk_priv,,
                   AC_WARN(No Kerberos V4 found); krb4=no,
		   -ldes),
      AC_WARN(No Kerberos V4 found); krb4=no)
  else
    AC_WARN(No DES library found for Kerberos V4 support)
    krb4=no
  fi

  if test "${krb4}" != no; then
    AC_DEFINE(HAVE_KRB)
  else
    AC_ERROR([Kerberos not found for authorization module])
  fi
fi

dnl
dnl Test for OpenSSL
dnl
AC_ARG_WITH(with-openssl,[  --with-openssl=PATH      use OpenSSL from PATH],
	with_openssl="${withval}")

case "$with_openssl" in
	no) with_openssl="no";;
	""|yes) 
	  dnl if openssl has been compiled with the rsaref2 libraries,
	  dnl we need to include the rsaref libraries in the crypto check
                LIB_RSAREF=""
	        AC_CHECK_LIB(rsaref, RSAPublicEncrypt,
		       LIB_RSAREF="-lRSAglue -lrsaref"; cmu_have_rsaref=yes,
		       cmu_have_rsaref=no)

		with_openssl="yes"
		AC_CHECK_LIB(crypto,BIO_accept,
                                    LIBS="-lcrypto $LIB_RSAREF ${LIBS}",
		                    with_openssl="no", $LIB_RSAREF)
		AC_CHECK_LIB(ssl, SSL_CTX_new, LIBS="-lssl ${LIBS}",
		             with_openssl="no", -lcrypto $LIB_RSAREF)
		
		;;
	*)  CPPFLAGS="${CPPFLAGS} -I${with_openssl}/include"
	    LDFLAGS="${LDFLAGS} -L${with_openssl}/lib"
	    LIBS="${LIBS} -lssl -lcrypto";;
esac

AC_MSG_CHECKING(for openssl)
AC_MSG_RESULT($with_openssl)

if test "$with_openssl" != "no"; then
	AC_DEFINE(HAVE_SSL)
fi

dnl
dnl Test for Zephyr
dnl
AC_ARG_WITH(zephyr,[  --with-zephyr=PATH      use Zephyr from PATH],
	with_zephyr="${withval}")
if test -z "$with_zephyr"; then
     if test -f /usr/local/lib/libzephyr.a; then
	with_zephyr="/usr/local"
     elif test -f /usr/lib/libzephyr.a; then
        with_zephyr="/usr"
     fi
fi
ZEPHYR_LIBS=""
ZEPHYR_CPPFLAGS=""
case "$with_zephyr" in
	no) true;;
	""|yes) AC_CHECK_LIB(zephyr,ZInitialize,ZEPHYR_LIBS="-lzephyr",
		with_zephyr="no",);;
       *)  if test -d ${with_zephyr}/include/zephyr; then
               ZEPHYR_CPPFLAGS="-I${with_zephyr}/include/zephyr"
           else
               ZEPHYR_CPPFLAGS="-I${with_zephyr}/include"
           fi
           ZEPHYR_LIBS="-lzephyr";;
esac
AC_SUBST(ZEPHYR_LIBS)
AC_SUBST(ZEPHYR_CPPFLAGS)
AC_ARG_WITH(notify,[  --with-notify=METHOD    use mail notification METHOD
                          METHOD is zephyr, unix or no],
	WITH_NOTIFY="${withval}",
	if test "$with_zephyr" = "no"; then
		WITH_NOTIFY=no
	else
		WITH_NOTIFY=zephyr
	fi)
AC_SUBST(WITH_NOTIFY)
if test "$with_zephyr" = "no"; then
     if test "$WITH_NOTIFY" = "zephyr"; then
	AC_MSG_ERROR(Unable to find Zephyr libraries)
     fi
fi     



dnl
dnl Select a method for IMAP IDLE
dnl
AC_ARG_WITH(idle,[  --with-idle=METHOD      use METHOD for IMAP IDLE
                          METHOD is [poll], idled or no],
	WITH_IDLE="${withval}",WITH_IDLE="poll")
AC_SUBST(WITH_IDLE)
IMAP_PROGS=""
if test "$WITH_IDLE" = "idled"; then
  IMAP_PROGS="$IMAP_PROGS idled"
fi

dnl
dnl see if we're compiling the Murder support programs
dnl
AC_ARG_ENABLE(murder,
        [  --enable-murder         enable IMAP Murder support],
        if test "$enableval" != no; then
	  IMAP_PROGS="$IMAP_PROGS proxyd pop3proxyd target-acap mupdate"
        fi) 

AC_SUBST(IMAP_PROGS)

dnl
dnl Try and find a system version of com_err.
dnl If we see something that looks a little wacky, ignore it (there are many
dnl deficient installs of com_err, unfortunately, which leave out compile_et)
dnl
AC_ARG_WITH(com_err,
[  --with-com_err=PATH     use com_err from path -- includes in PATH/include,
                          libs in PATH/lib, and compile_et in PATH/bin])
if test -z "$with_com_err"; then
	# no value supplied
	AC_CHECK_LIB(com_err, com_err, [
		# com_err is already in library path
		# guess we're okay
		# can use system com_err
		with_com_err=""
		AC_PATH_PROG(COMPILE_ET, compile_et, [no compile et])
	], [
	if test -f /usr/local/include/com_err.h; then
		with_com_err="/usr/local"
		AC_PATH_PROG(COMPILE_ET, /usr/local/bin/compile_et, [no compile et])
	else
		# use ours
		with_com_err=yes
	fi
	])

	if test "${with_com_err}" = "no"; then
	  AC_MSG_WARN([com_err is required; included version will be used.])
	  with_com_err="yes"
	fi
	if test "${COMPILE_ET}" = "no compile et" -o "${COMPILE_ET}" = ""; then
	  AC_MSG_WARN([Parts of com_err distribuion were found, but not compile_et.])
	  AC_MSG_WARN([Will build com_err from included sources.])
	  with_com_err="yes" # build it ourselves
	fi
fi

case "$with_com_err" in
  # built-in et
  yes) # use the com_err we're gonna build
	  COM_ERR_LIBS="../et/libcom_err.a"
	  COMPILE_ET="../et/compile_et"
	  COM_ERR_LDFLAGS=""
	  COM_ERR_CPPFLAGS=[-I${srcdir}/../et]
	  PRE_SUBDIRS="et ${PRE_SUBDIRS}"
	  EXTRA_OUTPUT="${EXTRA_OUTPUT} et/Makefile"
	  ;;
  "")   # no problem, we already have it in the paths
	# we do nothing to pick it up
	COM_ERR_LIBS="-lcom_err" # hope it's not shared
	# we already set COMPILE_ET, or we didn't get here
	COM_ERR_LDFLAGS=""
	COM_ERR_CPPFLAGS=""
      ;;
  *) # use whatever they told us, or whatever we found
     COMPILE_ET="${with_com_err}/bin/compile_et"
     COM_ERR_LIBS="${with_com_err}/lib/libcom_err.a"
     COM_ERR_CPPFLAGS="-I${with_com_err}/include"
     # Ever get the feeling people hide this stuff on purpose?
     if test -d "${with_com_err}/include/et" ; then
       COM_ERR_CPPFLAGS="-I${with_com_err}/include"
     fi
dnl     CMU_ADD_LIBPATH_TO(${with_com_err}/lib, COM_ERR_LDFLAGS)
     COMPILE_ET="${with_com_err}/bin/compile_et"
esac
AC_SUBST(COMPILE_ET)
AC_SUBST(COM_ERR_LIBS)
AC_SUBST(COM_ERR_LDFLAGS)
AC_SUBST(COM_ERR_CPPFLAGS)

AC_MSG_CHECKING(for modern syslog)
AC_CACHE_VAL(cyrus_cv_lib_syslog, AC_TRY_CPP([#include <syslog.h>
#ifndef LOG_LOCAL6
#include </nonexistent>
#endif],cyrus_cv_lib_syslog=yes,cyrus_cv_lib_syslog=no))
if test $cyrus_cv_lib_syslog = no; then
	PRE_SUBDIRS="${PRE_SUBDIRS} syslog"
	EXTRA_OUTPUT="${EXTRA_OUTPUT} syslog/Makefile"
	DEPLIBS="${DEPLIBS} ../syslog/libsyslog.a"
	CPPFLAGS="$CPPFLAGS -I\$(srcdir)/../syslog"
fi
AC_MSG_RESULT($cyrus_cv_lib_syslog)

dnl Have to check getdtabalesize after adding ossup, as some ossups define it
AC_REPLACE_FUNCS(getdtablesize)
AC_ARG_ENABLE(cmulocal,
	[  --enable-cmulocal       enable CMU-specific local support],
	if test "$enableval" = yes; then
		EXTRA_SUBDIRS="${EXTRA_SUBDIRS} netnews depot"
		EXTRA_OUTPUT="${EXTRA_OUTPUT} depot/Makefile"
	fi)

AC_ARG_ENABLE(netscapehack,
	[  --enable-netscapehack   enable Netscape hack for the menu option
                          in Communicator to Administrate Mail],
	if test "$enableval" = yes; then
		AC_DEFINE(ENABLE_X_NETSCAPE_HACK)
	fi)

AC_CHECK_FUNC(dlopen,,AC_CHECK_LIB(dl, dlopen))
CMU_SASL_REQUIRED

dnl
dnl Check for sasl_checkapop()
dnl
AC_CHECK_LIB(sasl, sasl_checkapop, AC_DEFINE(HAVE_APOP))

dnl --- cmu agentx library deprecated in favor of ucd snmp
dnl CMU_AGENTX
dnl 
dnl if test "$found_agentx" = yes; then
dnl   CMU_PTHREADS
dnl   SNMP_SUBDIRS="snmp"
dnl fi
dnl
SNMP_SUBDIRS=""
AC_SUBST(SNMP_SUBDIRS)

CMU_SOCKETS
CMU_LIBWRAP
CMU_UCDSNMP

LIBS="$LIBS ${LIB_SOCKET}"

# Figure out what directories we're linking against.
# Lots of fun for the whole family.
# This probably chokes on anything with spaces in it.
# All we want is the list of -L directories, and -L may or may not be
# followed by a space.
isdir=no
libpath=""
#echo "debug ldflags: << ${ldflags} >>"
#echo "debug default_ldflags: << ${default_ldflags} >>"
for flag in ${ldflags} ${default_ldflags}; do
  case $flag in
    -L)
      # it's a split -L option, we'll mark the next option as a dir.
      isdir=yes
      ;;

    -L*) 
      # attached -L option: split off the directory
      larg=`echo $flag | sed -e 's:-L\(..*\):\1:'`
      libpath="${libpath} ${larg}"
      ;;

    *)
      if test $isdir = yes ; then
	libpath="${libpath} ${flag}"
	isdir=no
      fi
  esac
done

IMAP_COM_ERR_LIBS="${COM_ERR_LIBS}"
IMAP_LIBS="${LIB_SASL} ${LIBS}"

AC_SUBST(IMAP_COM_ERR_LIBS)
AC_SUBST(IMAP_LIBS)

dnl AC_OUTPUT_COMMANDS([
dnl   if test "$with_perl" != "no"; then
dnl     (cd perl/sieve/acap; $perl Makefile.PL PREFIX=$prefix)
dnl     (cd perl/sieve/managesieve; $perl Makefile.PL PREFIX=$prefix)
dnl     (cd perl/imap; $perl Makefile.PL PREFIX=$prefix)
dnl   fi
dnl ], perl=$PERL; with_perl=$with_perl; prefix=$prefix; SASL_LIB="$LIB_SASL"; SASL_INC="$SASLFLAGS"; export SASL_LIB SASL_INC)
AC_SUBST(PERL_SUBDIRS)

dnl make sure that Makefile is the last thing output
AC_OUTPUT(man/Makefile master/Makefile lib/Makefile acap/Makefile imap/Makefile imap/feedcyrus imtest/Makefile netnews/Makefile snmp/Makefile perl/Makefile $EXTRA_OUTPUT Makefile)
