# Required initializer
AC_INIT

# Automake initialization
AM_INIT_AUTOMAKE(mod_revocator, 1.0)

# Add a test for a compiler.
AC_PROG_CXX
AC_PROG_CC
AM_PROG_LIBTOOL

if test "$GCC" = yes ; then
    SHARED_LINK=-shared
else
    SHARED_LINK=-G
fi

# Check for header files
AC_HEADER_STDC
AC_CHECK_HEADERS( \
unistd.h
)

# Check for typedefs, structures, and compiler characteristics.
AC_C_CONST

AC_CHECKING(for apr-config)
# check for --with-apr-config
AC_MSG_CHECKING(for --with-apr-config)
AC_ARG_WITH(apr-config, [  --with-apr-config       Use apr-config to determine the APR directory],
[
  if test -x "$withval"
  then
    AC_MSG_RESULT([using $withval])
    APR_CONFIG=$withval
  fi
],
AC_MSG_RESULT(no))

if test -z "$APR_CONFIG" ; then
  AC_MSG_CHECKING(for apr-1-config and apr-config)
  AC_PATH_PROGS(APR_CONFIG, apr-1-config apr-config, NO_APR_CONFIG, [$PATH:/usr/local/apache/sbin:/usr/sbin])
fi

if test -n "$APR_CONFIG"; then
  AC_MSG_RESULT([using $APR_CONFIG. Use --with-apr-config to specify another.])
  apr_inc=`$APR_CONFIG --includes`
else
  AC_MSG_ERROR([neither apr-config nor apr-1-config were not found. use --with-apr-config to specify it.])
fi

AC_CHECKING(for apxs)

# check for --with-apxs
AC_MSG_CHECKING(for --with-apxs)
AC_ARG_WITH(apxs, [  --with-apxs=PATH        Path to apxs],
[
  if test -x "$withval"
  then
    AC_MSG_RESULT([using $withval])
    APXS=$withval
  else
    echo
    AC_MSG_ERROR([$withval not found or not executable])
  fi
],
AC_MSG_RESULT(no))

# if no apxs found yet, check /usr/local/apache/sbin
# since it's the default Apache location
if test -z "$APXS"; then
  AC_MSG_CHECKING(for apxs in /usr/local/apache/sbin)
  AC_PATH_PROG(APXS, apxs, NO_APXS, [/usr/local/apache/sbin:/usr/sbin:$PATH])
  if test -x $APXS; then
    AC_MSG_RESULT([found $APXS. Use --with-apxs to specify another.])
  else
    AC_MSG_RESULT(no)
  fi
fi

# and finally
if test -z "$APXS"; then
  AC_MSG_ERROR([apxs was not found. use --with-apxs to specify it.])
fi

# Get some variables we need for Makefile.in
apache_inc=`$APXS -q INCLUDEDIR`
apache_conf=`$APXS -q SYSCONFDIR`
apache_prefix=`$APXS -q PREFIX`
apache_bin=`$APXS -q SBINDIR`
extra_cppflags=`$APXS -q EXTRA_CPPFLAGS`

if test "$GCC" = yes ; then
    extra_cppflags="$extra_cppflags -fpermissive"
fi

if ! test -f "$apache_inc/apr.h"; then
  if test -z "$apr_inc"; then
    AC_MSG_ERROR([apr.h is not in your Apache include dir as reported by apxs. Use --with-apr-config to have apr-config tell us where to find it.])
  fi
fi

AC_CHECKING(for NSPR)

# check for --with-nspr
AC_MSG_CHECKING(for --with-nspr)
AC_ARG_WITH(nspr, [  --with-nspr=PATH        Netscape Portable Runtime (NSPR) directory],
[
  if test -e "$withval"/include/nspr.h
  then
    AC_MSG_RESULT([using $withval])
    NSPRDIR=$withval
    nspr_inc="-I$NSPRDIR/include"
    nspr_lib="-L$NSPRDIR/lib"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# check for --with-nspr-inc
AC_MSG_CHECKING(for --with-nspr-inc)
AC_ARG_WITH(nspr-inc, [  --with-nspr-inc=PATH    Netscape Portable Runtime (NSPR) include file directory],
[
  if test -e "$withval"/nspr.h
  then
    AC_MSG_RESULT([using $withval])
    nspr_inc="-I$withval"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# check for --with-nspr-lib
AC_MSG_CHECKING(for --with-nspr-lib)
AC_ARG_WITH(nspr-lib, [  --with-nspr-lib=PATH    Netscape Portable Runtime (NSPR) library directory],
[
  if test -d "$withval"
  then
    AC_MSG_RESULT([using $withval])
    nspr_lib="-L$withval"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# if NSPR is not found yet, try pkg-config

# last resort
if test -z "$nspr_inc" -o -z "$nspr_lib"; then
  AC_MSG_CHECKING(for nspr with pkg-config)
  AC_PATH_PROG(PKG_CONFIG, pkg-config)
  if test -n "$PKG_CONFIG"; then
    if $PKG_CONFIG --exists nspr; then
      nspr_inc=`$PKG_CONFIG --cflags-only-I nspr`
      nspr_lib=`$PKG_CONFIG --libs-only-L nspr`
    else
      AC_MSG_ERROR([NSPR not found, specify with --with-nspr.])
    fi
  fi
fi 

AC_CHECKING(for NSS)

# check for --with-nss
AC_MSG_CHECKING(for --with-nss)
AC_ARG_WITH(nss, [  --with-nss=PATH         Network Security Services (NSS) directory],
[
  if test -e "$withval"/include/nss.h
  then
    AC_MSG_RESULT([using $withval])
    NSSDIR=$withval
    nss_inc="-I$NSSDIR/include"
    nss_lib="-L$NSSDIR/lib"
    nss_lib_dir="$NSSDIR/lib"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# check for --with-nss-inc
AC_MSG_CHECKING(for --with-nss-inc)
AC_ARG_WITH(nss-inc, [  --with-nss-inc=PATH     Network Security Services (NSS) include directory],
[
  if test -e "$withval"/nss.h
  then
    AC_MSG_RESULT([using $withval])
    nss_inc="-I$withval"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# check for --with-nss-lib
AC_MSG_CHECKING(for --with-nss-lib)
AC_ARG_WITH(nss-lib, [  --with-nss-lib=PATH     Network Security Services (NSS) library directory],
[
  if test -e "$withval"/libnss3.so
  then
    AC_MSG_RESULT([using $withval])
    nss_lib="-L$withval"
    nss_lib_dir="$withval"
  else
    echo
    AC_MSG_ERROR([$withval/libnss3.so not found])
  fi
],
AC_MSG_RESULT(no))

# if NSS is not found yet, try pkg-config

# last resort
if test -z "$nss_inc" -o -z "$nss_lib"; then
  AC_MSG_CHECKING(for nss with pkg-config)
  AC_PATH_PROG(PKG_CONFIG, pkg-config)
  if test -n "$PKG_CONFIG"; then
    if $PKG_CONFIG --exists nss; then
      allow_libs=$PKG_CONFIG_ALLOW_SYSTEM_LIBS
      PKG_CONFIG_ALLOW_SYSTEM_LIBS=1
      export PKG_CONFIG_ALLOW_SYSTEM_LIBS
      nss_inc=`$PKG_CONFIG --cflags-only-I nss`
      nss_lib=`$PKG_CONFIG --libs-only-L nss`
      nss_lib_dir=`echo $nss_lib | sed 's/-L//' | sed s'/ //g'`
      PKG_CONFIG_ALLOW_SYSTEM_LIBS=$allow_libs
      export PKG_CONFIG_ALLOW_SYSTEM_LIBS
      if test "$nss_lib" = " "; then
          nss_lib=-L"/usr/lib"
          nss_lib_dir="/usr/lib"
      fi
    else
      AC_MSG_ERROR([NSS not found, specify with --with-nss.])
    fi
  fi
fi

AC_CHECKING(for LDAPSDK)

# check to see if openldap is specifically requested
AC_MSG_CHECKING(for --with-openldap)
AC_ARG_ENABLE(openldap, [  --enable-openldap        Use OpenLDAP as the LDAP library (default=no)],
   openldap=$enableval, openldap=no)
if test $openldap = yes; then
   AC_MSG_RESULT(yes)
   ldapsdk_inc=""
   ldapsdk_lib=""
else
   echo
   AC_MSG_RESULT(no)
fi

# check for --with-ldapsdk
AC_MSG_CHECKING(for --with-ldapsdk)
AC_ARG_WITH(ldapsdk, [  --with-ldapsdk=PATH     LDAP SDK directory],
[
  if test -e "$withval"/include/ldap.h
  then
    AC_MSG_RESULT([using $withval])
    LDAPSDKDIR=$withval
    ldapsdk_inc="-I$LDAPSDKDIR/include"
    ldapsdk_lib="-L$LDAPSDKDIR/lib"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# check for --with-ldapsdk-inc
AC_MSG_CHECKING(for --with-ldapsdk-inc)
AC_ARG_WITH(ldapsdk-inc, [  --with-ldapsdk-inc=PATH Mozilla LDAP SDK include directory],
[
  if test -e "$withval"/ldap.h
  then
    AC_MSG_RESULT([using $withval])
    ldapsdk_inc="-I$withval"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# check for --with-ldapsdk-lib
AC_MSG_CHECKING(for --with-ldapsdk-lib)
AC_ARG_WITH(ldapsdk-lib, [  --with-ldapsdk-lib=PATH Mozilla LDAP SDK library directory],
[
  if test -d "$withval"
  then
    AC_MSG_RESULT([using $withval])
    ldapsdk_lib="-L$withval"
  else
    echo
    AC_MSG_ERROR([$withval not found])
  fi
],
AC_MSG_RESULT(no))

# last resort
if test \( -z "$ldapsdk_inc" -o -z "$ldapsdk_lib" \) -a "$openldap" != yes; then
  AC_MSG_CHECKING(for ldapsdk with pkg-config)
  AC_PATH_PROG(PKG_CONFIG, pkg-config)
  if test -n "$PKG_CONFIG"; then
    if $PKG_CONFIG --exists mozldap; then
      nss_inc=`$PKG_CONFIG --cflags-only-I mozldap`
      nss_lib=`$PKG_CONFIG --libs-only-L mozldap`
    else
      AC_MSG_ERROR([Mozilla LDAPSDK not found, specify with --with-ldapsdk.])
    fi
  fi
fi

# Set the LDAP library we're using
if test "$openldap" != yes; then
    extra_cppflags="$extra_cppflags -DMOZILLA_LDAP"
    ldapsdk_libs_ssl="-lssldap60"
    ldapsdk_libs="-lldap60 -lprldap60"
else
    ldapsdk_libs_ssl=""
    ldapsdk_libs="-lldap"
fi

if ! test -e "$nss_lib_dir"/libnssb.a
then
  AC_MSG_ERROR([NSS is installed but the PKCS11 development package is missing. Need libnssb.a])
fi

# Substitute values 
AC_SUBST(SHARED_LINK)
AC_SUBST(APXS)
AC_SUBST(apr_inc)
AC_SUBST(apache_inc)
AC_SUBST(apache_conf)
AC_SUBST(apache_prefix)
AC_SUBST(apache_bin)
AC_SUBST(extra_cppflags)
AC_SUBST(nspr_inc)
AC_SUBST(nspr_lib)
AC_SUBST(nss_inc)
AC_SUBST(nss_lib)
AC_SUBST(nss_lib_dir)
AC_SUBST(ldapsdk_inc)
AC_SUBST(ldapsdk_lib)
AC_SUBST(ldapsdk_libs_ssl)
AC_SUBST(ldapsdk_libs)

# Write config.status and the Makefile
AC_OUTPUT(Makefile)
