AC_INIT(mechanisms.h)
AC_CONFIG_AUX_DIR(config)
AM_CONFIG_HEADER(saslauthd.h)
AC_CANONICAL_HOST

dnl Should we enable SASLAUTHd at all?
AC_ARG_WITH(saslauthd, [  --with-saslauthd=DIR    enable use of the saslauth daemon using state dir DIR ],
		with_saslauthd=$withval,
		with_saslauthd=yes)
  if test "$with_saslauthd" = yes; then
    with_saslauthd="/var/state/saslauthd"
  fi
  AC_DEFINE(HAVE_SASLAUTHD)
  AC_DEFINE_UNQUOTED(PATH_SASLAUTHD_RUNDIR, "$with_saslauthd")
AM_CONDITIONAL(SASLAUTHD, test "$with_saslauthd" != no)

AM_INIT_AUTOMAKE(saslauthd,2.1.3-BETA)
CMU_INIT_AUTOMAKE

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_AWK
AC_PROG_MAKE_SET
AC_PROG_LN_S
AC_PROG_INSTALL
CMU_C___ATTRIBUTE__

dnl Checks for libraries.
CMU_SOCKETS

dnl mechanism-related checking
SASL_KERBEROS_V4_CHK
SASL_GSSAPI_CHK

if test "$gssapi" != no; then
	AC_DEFINE(AUTH_KRB5)
	if test "$gss_impl" = "heimdal"; then
	    AC_DEFINE(KRB5_HEIMDAL)
	fi
fi

SASL_PLAIN_CHK

AC_ARG_ENABLE(sia, [  --enable-sia            enable SIA authentication [no] ],
  sia=$enableval,
  sia=no)
LIB_SIA=""
if test "$sia" != no; then
  if test -f /etc/sia/matrix.conf; then
    AC_DEFINE(HAVE_SIA)
    LIB_SIA="-lsecurity -ldb -lm -laud"
  else
    AC_ERROR([No support for SIA found])
  fi
fi
AC_SUBST(LIB_SIA)

AC_ARG_ENABLE(auth-sasldb, [  --enable-auth-sasldb    enable experimental SASLdb authentication module [no] ],
  authsasldb=$enableval,
  authsasldb=no)
if test "$authsasldb" != no; then
  if test ! -d "../sasldb"; then
     echo "ERROR: Cannot build sasldb module outside of the full SASL source tree."
     exit 0;
  fi
  AC_DEFINE(AUTH_SASLDB)
  SASL_DB_PATH_CHECK()
  SASL_DB_CHECK()
  SASL_DB_LIB="$SASL_DB_LIB ../sasldb/libsasldb.a"
fi

AC_ARG_WITH(pam, [  --with-pam=DIR          use PAM (rooted in DIR) [yes] ],
	with_pam=$withval,
	with_pam=yes)
if test "$with_pam" != no; then
  if test -d $with_pam; then
    CPPFLAGS="$CPPFLAGS -I${with_pam}/include"
    LDFLAGS="$LDFLAGS -L${with_pam}/lib"
  fi
  cmu_save_LIBS="$LIBS"
  AC_CHECK_LIB(pam, pam_start,
	  AC_CHECK_HEADER(security/pam_appl.h,,
			  with_pam=no),
		with_pam=no, $SASL_DL_LIB)
  LIBS="$cmu_save_LIBS"
fi

AC_ARG_WITH(ipctype, [  --with-ipctype={unix,doors}    use ipctype [unix] ],
	with_ipctype=$withval,
	with_ipctype="unix")
IPCTYPE=$with_ipctype
AC_SUBST(IPCTYPE)
if test "$with_ipctype" = "doors"; then
  LIBS="$LIBS -ldoor -lpthread"
fi

AC_MSG_CHECKING(for PAM support)
AC_MSG_RESULT($with_pam)
LIB_PAM=""
if test "$with_pam" != no; then
  AC_DEFINE(HAVE_PAM)
  LIB_PAM="-lpam"
fi
AC_SUBST(LIB_PAM)

AC_CHECK_LIB(resolv, inet_aton)

AC_MSG_CHECKING(to include experimental LDAP support)
AC_ARG_WITH(ldap, [  --with-ldap             use LDAP (experimental) [no] ],
	with_ldap=$withval,
	with_ldap=no)
AC_MSG_RESULT($with_ldap)

LDAP_LIBS=""
if test "$with_ldap" != no; then
  AC_CHECK_LIB(ldap, ldap_initialize, [ AC_DEFINE(HAVE_LDAP)
                                        LDAP_LIBS="-lldap -llber" ],,-llber)
fi
AC_SUBST(LDAP_LIBS)


dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h strings.h syslog.h unistd.h sys/uio.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST  
AC_TYPE_PID_T

dnl Checks for library functions.
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(gethostname mkdir socket strdup)
AC_CHECK_FUNCS(getspnam getuserpw, break)

dnl Check for getaddrinfo
GETADDRINFOOBJS=""
sasl_cv_getaddrinfo=no
IPv6_CHECK_FUNC(getaddrinfo,
		[AC_DEFINE(HAVE_GETADDRINFO)], [sasl_cv_getaddrinfo=yes])
if test $sasl_cv_getaddrinfo = yes; then
    COMPAT_SRC="$COMPAT_SRC getaddrinfo.c"
    COMPAT_OBJ="$COMPAT_OBJ getaddrinfo.o"
fi

dnl Check for getnameinfo
GETNAMEINFOOBJS=""
sasl_cv_getnameinfo=no
IPv6_CHECK_FUNC(getnameinfo,
		[AC_DEFINE(HAVE_GETNAMEINFO)], [sasl_cv_getnameinfo=yes])
if test $sasl_cv_getnameinfo = yes; then
	COMPAT_SRC="$COMPAT_SRC getnameinfo.c"
	COMPAT_OBJ="$COMPAT_OBJ getnameinfo.o"
fi

AC_SUBST(COMPAT_SRC)
AC_SUBST(COMPAT_OBJ)

IPv6_CHECK_SS_FAMILY()
IPv6_CHECK_SA_LEN()
IPv6_CHECK_SOCKLEN_T()

AC_EGREP_HEADER(sockaddr_storage, sys/socket.h,
                AC_DEFINE(HAVE_STRUCT_SOCKADDR_STORAGE))

AC_OUTPUT(Makefile)
