dnl ProFTPD - FTP server daemon
dnl Copyright (c) 1997, 1998 Public Flood Software
dnl Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
dnl Copyright (c) 2001-2021 The ProFTPD Project team
dnl
dnl This program is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 2 of the License, or
dnl (at your option) any later version.
dnl
dnl This program is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl GNU General Public License for more details.
dnl
dnl You should have received a copy of the GNU General Public License
dnl along with this program; if not, write to the Free Software
dnl Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
dnl
dnl Process this file with autoconf to produce a configure script.

AC_INIT(include/conf.h)

ac_core_modules="mod_core.o mod_xfer.o mod_rlimit.o mod_auth_unix.o mod_auth_file.o mod_auth.o mod_ls.o mod_log.o mod_site.o mod_delay.o mod_facts.o"
ac_build_core_modules="modules/mod_core.o modules/mod_xfer.o modules/mod_rlimit.o modules/mod_auth_unix.o modules/mod_auth_file.o modules/mod_auth.o modules/mod_ls.o modules/mod_log.o modules/mod_site.o modules/mod_delay.o modules/mod_facts.o"

dnl Get the OS type
AC_CONFIG_AUX_DIR(./)
AC_CANONICAL_SYSTEM

ostype=`echo $build_os | sed 's/\..*$//g' | sed 's/-.*//g' | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
osrel=`echo $build_os | sed 's/-.*//g' | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ | tr '.' '_'`

OSTYPE="-D$ostype"
OSREL="-D$osrel"
platform="\"$ostype ($osrel)\""

if test "$OSTYPE" = "$OSREL" ; then
  OSTYPE=""
  platform="\"$ostype\""
fi

BUILD_OPTS=`echo "$ac_configure_args"`
AC_DEFINE_UNQUOTED(PR_BUILD_OPTS, "$BUILD_OPTS", [Define the build options])
AC_DEFINE_UNQUOTED(PR_PLATFORM, $platform, [Define the build platform])
AC_SUBST(BUILD_OPTS)
AC_SUBST(OSREL)
AC_SUBST(OSTYPE)

dnl MacOSX requires -traditional-cpp; autodetecting it isn't impossible
dnl AFAIK, since assuming the need for -traditional-cpp breaks the build
dnl on other OSes. -jwm, 30 Jan 2001
if test "$OSTYPE" = "-DRHAPSODY5"; then
  CFLAGS="$CFLAGS -traditional-cpp -D__OT__"
fi

dnl This needs to happen here, prior to the creation of the libtool script,
dnl so that that script uses the correct shell-isms
AC_SUBST(CONFIG_SHELL)

dnl AC_PROG_LIBTOOL relies on the top_builddir variable
top_builddir=.
LT_INIT([dlopen])

dnl If LT_INIT provides an empty value for CONFIG_SHELL for some reason (it
dnl happens on my MacOSX 10.5 machine, for example), have a fallback.
if test x"$CONFIG_SHELL" = x; then
  CONFIG_SHELL="$SHELL"
fi

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PROG_LIBTOOL

dnl This test must come as early as possible after the compiler
dnl configuration tests, because the choice of the file model can (in
dnl principle) affect whether functions and headers are available,
dnl whether they work, etc.
AC_SYS_LARGEFILE  

# The native HP-UX 10 compiler needs -Ae to enable Extended ANSI compliance
# (-Ae is the default in the native compiler under HP-UX 11).
if test $ac_cv_prog_gcc = no -a "$OSTYPE" = "-DHPUX10"; then
  CFLAGS="$CFLAGS -Ae"
fi

LDFLAGS="-L\$(top_srcdir)/lib -L\$(top_builddir)/lib $LDFLAGS"

# AIX has issues with the -rdynamic linker flag.  How many different AIX
# versions should we support here?
if test $ac_cv_prog_gcc = yes && test "$OSTYPE" != "-DAIX7"; then
  dnl Make sure to use the -rdynamic linker flag, for stacktraces
  LDFLAGS="$LDFLAGS -rdynamic"
fi

# Record the current CPPFLAGS, LDFLAGS, and LIBS here
ac_orig_cppflags="$CPPFLAGS"
ac_orig_ldflags="$LDFLAGS"
ac_orig_libs="$LIBS"

if test $ac_cv_prog_gcc = yes; then
  if test x"$CFLAGS" = "x-g -O2"; then
    fullCFLAGS="-O2"
  else
    fullCFLAGS="$CFLAGS"
  fi

  dnl Compilation tests

  dnl test for -malign-jumps=2 -malign-loops=2 -malign-functions=2
  if echo $ac_cv_build_cpu | egrep "^i[[34567]]86$" > /dev/null 2>&1; then
    AC_MSG_CHECKING([whether the C compiler accepts -malign-jumps -malign-loops -malign-functions])
    CFLAGS="-malign-jumps=2 -malign-loops=2 -malign-functions=2"
    AC_TRY_COMPILE(,,
      AC_MSG_RESULT(yes); fullCFLAGS="$fullCFLAGS $CFLAGS",
      AC_MSG_RESULT(no))
  fi

  dnl test for -Wall
  AC_MSG_CHECKING([whether the C compiler accepts -Wall])
  CFLAGS="-Wall"
  AC_TRY_COMPILE(,,
    AC_MSG_RESULT(yes); fullCFLAGS="$fullCFLAGS $CFLAGS",
    AC_MSG_RESULT(no))

  dnl test for -fno-omit-frame-pointer
  AC_MSG_CHECKING([whether the C compiler accepts -fno-omit-frame-pointer])
  CFLAGS="-fno-omit-frame-pointer"
  AC_TRY_COMPILE(,,
    AC_MSG_RESULT(yes); fullCFLAGS="$fullCFLAGS $CFLAGS",
    AC_MSG_RESULT(no))

  dnl test for -fno-strict-aliasing
  AC_MSG_CHECKING([whether the C compiler accepts -fno-strict-aliasing])
  CFLAGS="-fno-strict-aliasing"
  AC_TRY_COMPILE(,,
    AC_MSG_RESULT(yes); fullCFLAGS="$fullCFLAGS $CFLAGS",
    AC_MSG_RESULT(no))

  CFLAGS="-g2 $fullCFLAGS"
fi

dnl We substitute these in the man page templates.
if test x$exec_prefix = xNONE ; then
  exec_prefix=$prefix
fi

if test x$prefix = xNONE ; then
  prefix=/usr/local
  bindir=/usr/local/bin
  datadir=/usr/local/share
  libexecdir=/usr/local/libexec
  sbindir=/usr/local/sbin
fi

BINDIR=`eval echo $bindir`
AC_SUBST(BINDIR)
DATADIR=`eval echo $datadir`
AC_SUBST(DATADIR)
INCLUDEDIR=`eval echo $includedir`
AC_SUBST(INCLUDEDIR)
LIBEXECDIR=`eval echo $libexecdir`
AC_SUBST(LIBEXECDIR)
LOCALSTATEDIR=`eval echo $localstatedir`
AC_SUBST(LOCALSTATEDIR)
RUNSTATEDIR=`eval echo $runstatedir`
AC_SUBST(RUNSTATEDIR)
PREFIX=`eval echo $prefix`
AC_SUBST(PREFIX)
SBINDIR=`eval echo $sbindir`
AC_SUBST(SBINDIR)
SYSCONFDIR=`eval echo $sysconfdir`
AC_SUBST(SYSCONFDIR)

LIB_DEPS="\"\""

AC_ARG_VAR(LIBS, [linker flags, e.g. -l<lib>.{a,so} if you have nonstandard libraries to link])

dnl configure command line options...

dnl --with options.

dnl ProFTPD comes bundled with GNU's implementation of getopt, to be used
dnl in case the host system doesn't have getopt.  However, this causes
dnl problems for certain builds of proftpd, e.g. ProFTPD and MySQL (which
dnl itself similarly bundles a getopt implementation).  Thus, we need to
dnl support/handle --without-getopt, to disable use of ProFTPD's getopt.
dnl I don't know if MySQL supports any similar option.
LIB_OBJS="pr_fnmatch.o sstrncpy.o strsep.o vsnprintf.o glibc-glob.o glibc-hstrerror.o glibc-mkstemp.o pr-syslog.o pwgrent.o hanson-tpl.o ccan-json.o openbsd-blowfish.o openbsd-bcrypt.o"

AC_ARG_WITH(getopt,
  [AC_HELP_STRING(
      [--without-getopt],
      [prevent proftpd from using its bundled getopt implementation.  This is done automatically if the host supports getopt, but may need to be explicitly used when combining proftpd with other applications, such as MySQL])
  ],
  [
    if test "$withval" != "no" ; then
      AC_CHECK_FUNCS(getopt,
        [AC_CHECK_HEADERS(getopt.h)
         AC_CHECK_FUNCS(getopt_long)
         AC_DEFINE(PR_USE_SYSTEM_GETOPT, 1,
           [Define if using system getopt support])],
        [LIB_OBJS="$LIB_OBJS getopt.o getopt1.o"]
      )
    fi
  ],
  [
    AC_CHECK_FUNCS(getopt,
      [AC_CHECK_HEADERS(getopt.h)
       AC_CHECK_FUNCS(getopt_long)
       AC_DEFINE(PR_USE_SYSTEM_GETOPT, 1,
         [Define if using system getopt support])],
      [LIB_OBJS="$LIB_OBJS getopt.o getopt1.o"]
    )
  ])

dnl Lastlog support
AC_ARG_WITH(lastlog,
  [AC_HELP_STRING(
    [--with-lastlog=PATH],
    [specify lastlog location (Default: /var/adm/lastlog)],
  )],
  [
    if test "x$withval" = "xno" ; then
      # nothing to do
      foo=bar

    elif test -n "$withval" ; then
      AC_DEFINE(PR_USE_LASTLOG, 1, [Define if enabling lastlog support.])
      AC_CHECK_HEADERS(lastlog.h paths.h)

      if test "x${withval}" != "xyes" ; then
        pr_lastlog_path="$withval"

      else
        dnl Try to determine the correct lastlog location (be it file or
        dnl directory) automagically.
        AC_MSG_CHECKING([for LASTLOG_FILE])
        AC_TRY_COMPILE([
            #include <sys/types.h>
            #include <utmp.h>
            #ifdef HAVE_LASTLOG_H
            #  include <lastlog.h>
            #endif
            #ifdef HAVE_PATHS_H
            #  include <paths.h>
            #endif
            #ifdef HAVE_LOGIN_H
            # include <login.h>
            #endif
          ],
          [
            char *lastlog = LASTLOG_FILE;
          ],
          [
            AC_MSG_RESULT(yes)
          ],
          [
            AC_MSG_RESULT(no)
            AC_MSG_CHECKING([for _PATH_LASTLOG])
            AC_TRY_COMPILE([
                #include <sys/types.h>
                #include <utmp.h>
                #ifdef HAVE_LASTLOG_H
                #  include <lastlog.h>
                #endif
                #ifdef HAVE_PATHS_H
                #  include <paths.h>
                #endif
              ],
              [
                char *lastlog = _PATH_LASTLOG;
              ],
              [
                AC_MSG_RESULT(yes)
              ],
              [
                AC_MSG_RESULT(no)
                found_lastlog_path=no
              ])
          ])

         if test -z "$pr_lastlog_path" ; then
           if test x"$found_lastlog_path" = x"no" ; then
             for f in /var/log/lastlog /var/adm/lastlog /usr/adm/lastlog /etc/security/lastlog ; do
               AC_MSG_CHECKING([for $f])
               if (test -d "$f" || test -f "$f") ; then
                 pr_lastlog_path=$f
                 AC_MSG_RESULT(yes)

               else
                 AC_MSG_RESULT(no)
               fi
             done

             if test -z "$pr_lastlog_path" ; then
               AC_MSG_WARN([** Cannot find lastlog **])
             fi
          fi
        fi
      fi

      if test -n "$pr_lastlog_path" ; then
        AC_DEFINE_UNQUOTED(PR_LASTLOG_PATH, "`eval echo "$pr_lastlog_path"`", [Define the lastlog path])
      fi
    fi
  ])

dnl Modules...'nuff said.
AC_ARG_WITH(includes,
  [AC_HELP_STRING(
    [--with-includes=LIST],
    [add additional include paths to proftpd. LIST is a colon-separated list of include paths to add e.g. --with-includes=/some/mysql/include:/my/include])
  ],
  [
    if test x"$withval" != x; then
      if test x"$withval" = xyes; then
        AC_MSG_ERROR([--with-includes parameter missing required colon-separated list of include paths])
      fi

      if test x"$withval" != xno; then
        ac_addl_includes=`echo "$withval" | sed -e 's/:/ /g'` ;
        for ainclude in $ac_addl_includes; do
          if test x"$ac_build_addl_includes" = x ; then
            ac_build_addl_includes="-I$ainclude"
          else
            ac_build_addl_includes="-I$ainclude $ac_build_addl_includes"
          fi
        done
        CPPFLAGS="$CPPFLAGS $ac_build_addl_includes"
      fi
    fi
  ])

AC_ARG_WITH(libraries,
  [AC_HELP_STRING(
    [--with-libraries=LIST],
    [add additional library paths to proftpd. LIST is a colon-separated list of library paths to add e.g. --with-libraries=/some/mysql/libdir:/my/libs])
  ],
  [
    if test x"$withval" != x; then
      if test x"$withval" = xyes; then
        AC_MSG_ERROR([--with-libraries parameter missing required colon-separated list of library paths])
      fi

      if test x"$withval" != xno; then
        ac_addl_libdirs=`echo "$withval" | sed -e 's/:/ /g'` ;
        for alibdir in $ac_addl_libdirs; do
          if test x"$ac_build_addl_libdirs" = x ; then
            ac_build_addl_libdirs="-L$alibdir"
          else
            ac_build_addl_libdirs="-L$alibdir $ac_build_addl_libdirs"
          fi
        done 
        LDFLAGS="$LDFLAGS $ac_build_addl_libdirs"
      fi
    fi
  ])

dnl This variable is used to impose a certain ordering between mod_ifsession
dnl and mod_cap, per Bug#3576
ifsession_requested="false"
AC_ARG_WITH(modules,
  [AC_HELP_STRING(
    [--with-modules=LIST],
    [add additional modules to proftpd. LIST is a colon-separated list of modules to add e.g. --with-modules=mod_readme:mod_ifsession])
  ],
  [
    if test x"$withval" != x; then
      if test x"$withval" = xyes; then
        AC_MSG_ERROR([--with-modules parameter missing required colon-separated list of modules])
      fi

      if test x"$withval" != xno; then
        modules_list=`echo "$withval" | sed -e 's/:/ /g'`;

        for amodule in $modules_list; do
          if test x"$amodule" = xmod_dso ; then
            AC_MSG_ERROR([use --enable-dso instead of --with-modules=mod_dso for DSO support])
          fi

          if test x"$amodule" = xmod_ifsession ; then
            ifsession_requested="true"
          fi

          if test x"$amodule" = xmod_lang ; then
            AC_MSG_ERROR([use --enable-nls instead of --with-modules=mod_lang for NLS/UTF8 support])
          fi

          if test x"$amodule" = xmod_memcache ; then
            AC_MSG_ERROR([use --enable-memcache instead of --with-modules=mod_memcache for Memcache support])
          fi

          if test x"$amodule" = xmod_redis ; then
            AC_MSG_ERROR([use --enable-redis instead of --with-modules=mod_redis for Redis support])
          fi
        done

        # Trim off any leading/trailing colons, and collapse double-colons
        # into single colons; these are common typos.
        ac_static_modules=`echo "$withval" | sed 's/::/:/g' | sed 's/^://' | sed 's/:$//' | sed -e 's/:/.o /g'`.o ;

        for amodule in $ac_static_modules; do
          ac_build_static_modules="modules/$amodule $ac_build_static_modules"
        done

        dnl Make sure that mod_ifsession, if present in the list, appears at
        dnl the end.
        if test x"$ifsession_requested" = xtrue; then
          ac_static_modules=`echo "$ac_static_modules" | sed -e 's/mod_ifsession\.o//g'`
          ac_static_modules="$ac_static_modules mod_ifsession.o"

          ac_build_static_modules=`echo "$ac_build_static_modules" | sed -e 's/modules\/mod_ifsession\.o//g'`
          ac_build_static_modules="$ac_build_static_modules modules/mod_ifsession.o";
        fi
      fi
    fi
  ])

dnl Memcache
if test x"$enable_memcache" = xyes; then
  AC_CHECK_LIB(memcached, memcached_create)
  AC_CHECK_LIB(memcachedutil, libmemcached_util_ping)

  # Yes, we DO want mod_memcache AFTER the other modules in the static
  # module list. Otherwise, the module load ordering will be such that
  # memcache support will not work as expected
  ac_static_modules="$ac_static_modules mod_memcache.o"
  ac_build_static_modules="$ac_build_static_modules modules/mod_memcache.o"
fi

dnl Redis
if test x"$enable_redis" = xyes; then
  AC_CHECK_LIB(hiredis, redisConnect)

  # Yes, we DO want mod_redis AFTER the other modules in the static
  # module list. Otherwise, the module load ordering will be such that
  # Redis support will not work as expected.
  ac_static_modules="$ac_static_modules mod_redis.o"
  ac_build_static_modules="$ac_build_static_modules modules/mod_redis.o"
fi

dnl List of modules which are not allowed to be built as DSOs
ac_unshareable_modules="mod_auth mod_rlimit mod_auth_unix mod_core mod_dso mod_ls mod_xfer mod_log mod_site mod_cap mod_ctrls mod_memcache mod_redis"

AC_ARG_WITH(shared,
  [AC_HELP_STRING(
    [--with-shared=LIST],
    [build DSO modules for proftpd. LIST is a colon-separated list of modules to build as DSOs e.g. --with-shared=mod_rewrite:mod_ifsession])
  ],
  [
    if test x"$withval" != x; then
      if test x"$withval" = xyes; then
        AC_MSG_ERROR([--with-shared parameter missing required colon-separated list of modules])
      fi

      if test x"$withval" != xno; then
        # Trim off any leading/trailing colons, and collapse double-colons
        # into single colons; these are common typos.
        shared_modules=`echo "$withval" | sed 's/::/:/g'| sed 's/^://' | sed 's/:$//' | sed -e 's/:/ /g'`;
        ac_shared_modules=`echo "$withval" | sed 's/::/:/g'| sed 's/^://' | sed 's/:$//' | sed -e 's/:/.la /g'`.la;

        # First double-check that the given list does not contain any
        # unshareable modules
        for amodule in $pr_shared_modules; do
          for smodule in $ac_unshareable_modules; do
            if test x"$amodule" = x"$smodule"; then
              AC_MSG_ERROR([cannot build $amodule as a shared module])
            fi
          done
        done

        for amodule in $ac_shared_modules; do
          ac_build_shared_modules="modules/$amodule $ac_build_shared_modules"
        done

        PR_CHECK_CC_OPT(Werror=implicit-function-declaration)
      fi
    fi
  ])

dnl Configuration location of mysql_config
my_config="mysql_config"
AC_ARG_WITH(mysql-config,
  [AC_HELP_STRING(
    [--with-mysql-config=PATH],
    [configure location of the MySQL mysql_config script (default=mysql_config)])
  ],
  [
    if test x"$withval" != x; then
      my_config=`echo "$withval"`

      if test -z "$my_config"; then
        my_config="no"

      elif test x"$my_config" = xno; then
        # do nothing
        foo=bar

      elif test x"$my_config" = xyes; then
        # Use the default
        my_config="mysql_config"

      elif test -x "$my_config"; then
        # do nothing
        foo=bar

      else
        AC_MSG_ERROR([mysql_config path $my_config is not executable])
      fi

    else
      my_config="no"
    fi
  ])

dnl Configuration location of openssl cmdline
openssl_cmdline="openssl"
AC_ARG_WITH(openssl-cmdline,
  [AC_HELP_STRING(
    [--with-openssl-cmdline=PATH],
    [configure location of the openssl(1) command-line tool (default=openssl)])
  ],
  [
    if test x"$withval" != x; then
      openssl_cmdline=`echo "$withval"`

      if test -z "$openssl_cmdline"; then
        openssl_cmdline="no"

      elif test x"$openssl_cmdline" = xno; then
        # do nothing
        foo=bar

      elif test x"$openssl_cmdline" = xyes; then
        # Use the default
        openssl_cmdline="openssl"

      elif test -x "$openssl_cmdline"; then
        # do nothing
        foo=bar

      else
        AC_MSG_ERROR([openssl path $openssl_cmdline is not executable])
      fi

    else
      openssl_cmdline="no"
    fi
  ])


dnl Configuration location of pg_config
pg_config="pg_config"
AC_ARG_WITH(postgres-config,
  [AC_HELP_STRING(
    [--with-postgres-config=PATH],
    [configure location of the Postgres pg_config script (default=pg_config)])
  ],
  [
    if test x"$withval" != x; then
      pg_config=`echo "$withval"`

      if test -z "$pg_config"; then
        pg_config="no"

      elif test x"$pg_config" = xno; then
        # do nothing
        foo=bar

      elif test x"$pg_config" = xyes; then
        # Use the default
        pg_config="pg_config"

      elif test -x "$pg_config"; then
        # do nothing
        foo=bar

      else
        AC_MSG_ERROR([pg_config path $pg_config is not executable])
      fi

    else
      pg_config="no"
    fi
  ])

dnl Configurable location of the pkgconfig file
pkgconfigdir=NONE
AC_ARG_WITH(pkgconfig,
  [AC_HELP_STRING(
    [--with-pkgconfig=PATH],
    [configure directory that will contain the proftpd.pc pkgconfig file (default=lib/pkgconfig)])
  ],
  [
    if test x"$withval" != x; then
      if test x"$withval" = xyes; then
        AC_MSG_ERROR([--with-pkgconfig parameter missing required directory path])
      fi

      if test x"$withval" != xno; then
        pkgconfigdir=`echo "$withval"`
      fi
    fi
  ])

dnl --enable/--disable options.

AC_ARG_ENABLE(auth-file,
  [AC_HELP_STRING(
    [--disable-auth-file],
    [omit mod_auth_file from core modules])
  ],
  [
    if test "$enableval" = "no"; then
      ac_core_modules=`echo "$ac_core_modules" | sed -e 's/mod_auth_file\.o//'`
      ac_build_core_modules=`echo "$ac_build_core_modules" | sed -e 's/modules\/mod_auth_file\.o//'`
    fi
  ])
 
dnl Auto-detection of shadow passwords.
AC_ARG_ENABLE(autoshadow,
  [AC_HELP_STRING(
    [--enable-autoshadow],
    [enable run-time auto-detection of shadowed passwords (requires shadow)])
  ],
  [
    dnl AIX does not have shadow file/library support.  Try to prevent a
    dnl bad build if the admin, configuring on an AIX box, uses this option.
    if test "$enableval" = "yes" && test "$OSTYPE" = "-DAIX4"; then
      AC_MSG_WARN(AIX does not support traditional shadowed passwords)
      enableval="no"
    fi

    if test "$enableval" != "no" ; then
      AC_DEFINE(PR_USE_SHADOW, 1, [Define if using /etc/shadow files.])
      AC_DEFINE(PR_USE_AUTO_SHADOW, 1,
      [Define if auto-detection of shadow passwords is wanted.])
      force_shadow="yes"
    fi ])

dnl PAM support.
AC_ARG_ENABLE(auth-pam,
  [AC_HELP_STRING(
    [--enable-auth-pam],
    [enable PAM support (default=auto)])
  ],
  [
    if test "$enableval" = "no"; then
      if test `echo $ac_static_modules | grep -c mod_auth_pam` != "0"; then
        AC_MSG_ERROR([You cannot run configure with --disable-auth-pam and include mod_auth_pam in --with-modules at the same time])
      fi

      if test `echo $ac_shared_modules | grep -c mod_auth_pam` != "0"; then
        AC_MSG_ERROR([You cannot run configure with --disable-auth-pam and include mod_auth_pam in --with-shared at the same time])
      fi
    fi
  ])

dnl Optional workaround for broken getaddrinfo on some systems (e.g. HP-UX 11.x)
AC_ARG_ENABLE(builtin-getaddrinfo,
  [AC_HELP_STRING(
    [--enable-builtin-getaddrinfo],
    [force use of builtin getaddrinfo (default=no)])
  ],
  [
    if test x"$enableval" = xyes; then
      AC_DEFINE(PR_USE_GETADDRINFO, 1, [Define if using builtin getaddrinfo()])
    fi
  ])

dnl Since getaddrinfo() is broken on HP-UX 11.x, we automatically use the
dnl builtin version.
if test "$OSTYPE" = "-DHPUX11"; then
  AC_MSG_WARN([HP-UX 11 has broken getaddrinfo(), using builtin version])
  AC_DEFINE(PR_USE_GETADDRINFO, 1, [Define if using builtin getaddrinfo()])
fi

dnl Workaround for broken getnameinfo on some systems
AC_ARG_ENABLE(builtin-getnameinfo,
  [AC_HELP_STRING(
    [--enable-builtin-getnameinfo],
    [force use of builtin getnameinfo (default=no)])
  ],
  [
    if test x"$enableval" = xyes; then
      AC_DEFINE(PR_USE_GETNAMEINFO, 1, [Define if using builtin getnameinfo()])
    fi
  ])

dnl Capabilities support
AC_ARG_ENABLE(cap,
  [AC_HELP_STRING(
    [--enable-cap],
    [enable POSIX.1e capabilities (default=yes on Linux)])
  ])

dnl Controls support
AC_ARG_ENABLE(ctrls,
  [AC_HELP_STRING(
    [--enable-ctrls],
    [enable proftpd controls via ftpdctl (default=no)])
  ],
  [ if test x"$enableval" = xyes ; then
      AC_DEFINE(PR_USE_CTRLS, 1, [Define if using controls support.])
    fi
  ])

dnl POSIX ACL support
AC_ARG_ENABLE(facl,
  [AC_HELP_STRING(
    [--enable-facl],
    [enable support for POSIX ACLs])
  ])

dnl Curses/ncurses support
AC_ARG_ENABLE(curses,
  [AC_HELP_STRING(
    [--disable-curses],
    [disable use of curses (default=no)])
  ])

dnl DSO support
AC_ARG_ENABLE(dso,
  [AC_HELP_STRING(
    [--enable-dso],
    [add mod_dso to core modules])
  ],
  [
    if test x"$enableval" = x"yes"; then
      ac_core_modules="$ac_core_modules mod_dso.o"
      ac_build_core_modules="$ac_build_core_modules modules/mod_dso.o"

      MAIN_LDFLAGS="-L\$(top_srcdir)/lib/libltdl -dlopen self -export-dynamic"
      MAIN_LIBS="$MAIN_LIBS \$(LIBLTDL)"

      MODULE_LDFLAGS="-avoid-version -export-dynamic -module"

      INSTALL_DEPS="install-libltdl"
      LIB_DEPS="libltdl"
      MODULE_DEPS="libltdl"
      AC_DEFINE(PR_USE_DSO, 1, [Define if using DSO support.])

      dnl Run configure scripts in subdirectories
      LT_CONFIG_LTDL_DIR([lib/libltdl])
      LTDL_INIT([convenience])

      dnl Even though this macro is deprecated, we need to use it to
      dnl specifically tell ltdl that it is NOT to use any system directories,
      dnl and ONLY to use this bundled location.
      LTDL_CONVENIENCE([lib/libltdl])
    fi
  ])

dnl ident (RFC1413) support
AC_ARG_ENABLE(ident,
  [AC_HELP_STRING(
    [--enable-ident],
    [enable use of ident (RFC1413) lookups (default=no)])
  ])

dnl Memcache support
AC_ARG_ENABLE(memcache,
  [AC_HELP_STRING(
    [--enable-memcache],
    [enable support for memcache (default=no)])
  ],
  [ if test x"$enableval" = xyes ; then
      AC_DEFINE(PR_USE_MEMCACHE, 1, [Define if using Memcache support.])
    fi
  ])

dnl NLS support
ENABLE_NLS="\"\""
AC_ARG_ENABLE(nls,
  [AC_HELP_STRING(
    [--enable-nls],
    [enable Native Language Support (NLS) (default=no)])
  ],
  [ if test x"$enableval" = x"yes" ; then

      dnl The libintl library is only needed on non-GNU systems.  If it
      dnl is not installed, and the admin enables NLS support, then we
      dnl should abort.

      AC_CHECK_LIB(intl, bindtextdomain,
        [ac_build_addl_libs="-lintl $ac_build_addl_libs" UTILS_LIBS="-lintl"
         AC_DEFINE(HAVE_LIBINTL, 1, [Define if libintl is present.])
         AC_DEFINE(PR_USE_NLS, 1, [Define if using NLS support.])
         ENABLE_NLS="1"
        ],
        [AC_CHECK_LIB(c, bindtextdomain,
          [AC_DEFINE(PR_USE_NLS, 1, [Define if using NLS support.])
           ENABLE_NLS="1"
          ],
          [AC_MSG_ERROR([libintl support, required for NLS, not present -- aborting])]
        ])
      )

      dnl Similarly, the libiconv library is only needed on some non-GNU
      dnl systems.  Note that some systems redefine the iconv_open function
      dnl to something else (Bug#3682), e.g. libiconv_open.
      AC_CHECK_LIB(iconv, iconv_open,
        [LIBS="$LIBS -liconv"
         AC_DEFINE(HAVE_LIBICONV, 1, [Define if libiconv is present.])
         AC_DEFINE(PR_USE_NLS, 1, [Define if using NLS support.])
         ENABLE_NLS="1"
        ],
        [AC_CHECK_LIB(iconv, libiconv_open,
          [LIBS="$LIBS -liconv"
           AC_DEFINE(HAVE_LIBICONV, 1, [Define if libiconv is present.])
           AC_DEFINE(PR_USE_NLS, 1, [Define if using NLS support.])
           ENABLE_NLS="1"
          ],
          [AC_CHECK_LIB(c, iconv_open,
            [AC_DEFINE(PR_USE_NLS, 1, [Define if using NLS support.])
             ENABLE_NLS="1"
            ],
            [AC_MSG_ERROR([libiconv support, required for NLS, not present -- aborting])]
          ])
        ])
      )
    fi
  ])

AC_ARG_ENABLE(nonblocking-log-open,
  [AC_HELP_STRING(
    [--disable-nonblocking-log-open],
    [disable use of nonblocking open of log files (default=no)])
  ])

AC_ARG_ENABLE(ncurses,
  [AC_HELP_STRING(
    [--disable-ncurses],
    [disable use of ncurses (default=no)])
  ])

AC_ARG_ENABLE(pcre,
  [AC_HELP_STRING(
    [--enable-pcre],
    [enable use of PCRE for POSIX regular expressions rather than the system library (default=no)],
  )],
  [
    if test x"$enableval" = xyes ; then
      AC_DEFINE(PR_USE_PCRE, 1, [Define if using PCRE support.])
      ac_build_addl_libs="$ac_build_addl_libs -lpcreposix -lpcre"

      # Check for other PCRE-specific functionality here
      saved_ldflags="$LDFLAGS"
      saved_libs="$LIBS"
      saved_cppflags="$CPPFLAGS"

      # fiddle with CPPFLAGS, LDFLAGS
      CPPFLAGS="$CPPFLAGS $ac_build_addl_includes"
      LDFLAGS="$LDFLAGS $ac_build_addl_libdirs"

      dnl Splice out -lsupp, since that library hasn't been built yet
      LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
      LIBS="$LIBS -lpcre -lpcreposix"

      AC_MSG_CHECKING([for PCRE's pcre_free_study])
      AC_TRY_LINK(
        [ #ifdef HAVE_STDDEF_H
          # include <stddef.h>
          #endif
          #ifdef HAVE_STDLIB_H
          # include <stdlib.h>
          #endif
          #ifdef HAVE_SYS_TYPES_H
          # include <sys/types.h>
          #endif
          #include <pcre.h>
        ],
        [
          pcre_extra *extra = NULL;
          pcre_free_study(extra);
        ],
        [
          AC_MSG_RESULT(yes)
          AC_DEFINE(HAVE_PCRE_PCRE_FREE_STUDY, 1, [Define if you have PCRE's pcre_free_study])
        ],
        [
          AC_MSG_RESULT(no)
        ]
      )

      # restore CPPFLAGS, LDFLAGS
      CPPFLAGS="$saved_cppflags"
      LDFLAGS="$saved_ldflags"
      LIBS="$saved_libs"
    fi
  ])

dnl Redis support
AC_ARG_ENABLE(redis,
  [AC_HELP_STRING(
    [--enable-redis],
    [enable support for Redis (default=no)])
  ],
  [ if test x"$enableval" = xyes ; then
      AC_CHECK_HEADER(hiredis/hiredis.h,
        [AC_DEFINE(HAVE_HIREDIS_HIREDIS_H, 1, [Define if hiredis.h is present.])
         AC_DEFINE(PR_USE_REDIS, 1, [Define if using Redis support.])
      ])

      # Check for the redisReconnect() function in Hiredis; earlier versions
      # did not have this.
      saved_ldflags="$LDFLAGS"
      saved_libs="$LIBS"
      saved_cppflags="$CPPFLAGS"

      # fiddle with CPPFLAGS, LDFLAGS
      CPPFLAGS="$CPPFLAGS $ac_build_addl_includes"
      LDFLAGS="$LDFLAGS $ac_build_addl_libdirs"

      dnl Splice out -lsupp, since that library hasn't been built yet
      LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
      LIBS="$LIBS -lhiredis"

      AC_MSG_CHECKING([for Hiredis' redisReconnect])
      AC_TRY_LINK(
        [
          #ifdef HAVE_STDLIB_H
          # include <stdlib.h>
          #endif
          #ifdef HAVE_SYS_TYPES_H
          # include <sys/types.h>
          #endif
          #include <hiredis/hiredis.h>
        ],
        [
          (void) redisReconnect(NULL);
        ],
        [
          AC_MSG_RESULT(yes)
          AC_DEFINE(HAVE_HIREDIS_REDISRECONNECT, 1, [Define if you have Hiredis' redisReconnect])
        ],
        [
          AC_MSG_RESULT(no)
        ]
      )

      # restore CPPFLAGS, LDFLAGS
      CPPFLAGS="$saved_cppflags"
      LDFLAGS="$saved_ldflags"
      LIBS="$saved_libs"
    fi
  ])

dnl Workaround for broken setpassent on FreeBSD.
AC_ARG_ENABLE(force-setpassent,
  [AC_HELP_STRING(
    [--enable-force-setpassent],
    [force use of setpassent (default=no on FreeBSD)])
  ])

dnl IPv6 support.
AC_ARG_ENABLE(ipv6,
  [AC_HELP_STRING(
    [--disable-ipv6],
    [disable IPv6 support (default=no)])
  ])

dnl OpenSSL support
pr_use_openssl=""
AC_ARG_ENABLE(openssl,
  [AC_HELP_STRING(
    [--enable-openssl],
    [enable OpenSSL support (default=no)])
  ],
  [ if test x"$enableval" = xno ; then
      pr_use_openssl="no"
    fi
  ])

dnl Sodium support
pr_use_sodium=""
AC_ARG_ENABLE(sodium,
  [AC_HELP_STRING(
    [--enable-sodium],
    [enable Sodium support (default=auto)])
  ],
  [ if test x"$enableval" = xno ; then
      pr_use_sodium="no"
    fi
  ])

dnl Sendfile support.
AC_ARG_ENABLE(sendfile,
  [AC_HELP_STRING(
    [--disable-sendfile],
    [disable sendfile support (default=no)])
  ])

dnl Check for enabled shadow password support.
AC_ARG_ENABLE(shadow,
  [AC_HELP_STRING(
    [--enable-shadow],
    [force compilation of shadowed password support])
  ],
  [
    dnl AIX does not have shadow file/library support.  Try to prevent a
    dnl bad build if the admin, configuring on an AIX box, uses this option.
    if test "$enableval" = "yes" && test "$OSTYPE" = "-DAIX4"; then
      AC_MSG_WARN([AIX does not support traditional shadowed passwords])
      enableval="no" 
    fi

    if test "$enableval" = "no" ; then
      use_shadow=""
      force_shadow="no"
    else
      AC_DEFINE(PR_USE_SHADOW, 1, [Define if using shadow password support.])
      force_shadow="yes"
    fi ])

dnl Enable support for Tru64's C2 SIA authentication
AC_ARG_ENABLE(sia,
  [AC_HELP_STRING(
    [--enable-sia],
    [enable SIA authentication support (Tru64)])
  ],
  [
    if test x"$enableval" = xyes ; then

      dnl Tru64's C2/SIA authentication requires these headers
      AC_CHECK_HEADERS(sia.h siad.h sys/security.h)

      AC_CHECK_LIB(security, set_auth_parameters)
      AC_CHECK_FUNCS(set_auth_parameters)

      AC_CHECK_LIB(sec, getprpwent)
      AC_CHECK_FUNCS(getprpwent)

      AC_DEFINE(PR_USE_SIA, 1, [Define if using Tru64 SIA support.])
    fi
  ])

dnl Test support
ENABLE_TESTS="\"\""
AC_ARG_ENABLE(tests,
  [AC_HELP_STRING(
    [--enable-tests],
    [enable unit tests (default=no)])
  ],
  [
    if test x"$enableval" != xno ; then
      AC_CHECK_HEADERS(check.h)

      AC_CHECK_LIB(check, tcase_create,
        [AC_DEFINE(HAVE_LIBCHECK, 1, [Define if libcheck is present.])
         ENABLE_TESTS="1"
         AC_DEFINE(PR_USE_TESTS, 1, [Define if testsuite support is enabled.])
        ],
        [
         AC_MSG_ERROR([libcheck support, required for tests, not present -- aborting])
        ]
      )

      if test x"$enableval" != x"nonetwork" ; then
        AC_DEFINE(PR_USE_NETWORK_TESTS, 1, [Define if non-local network tests are enabled.])
      fi
    fi
  ])

dnl Trace support.
AC_ARG_ENABLE(trace,
  [AC_HELP_STRING(
    [--disable-trace],
    [disable trace support (default=no)])
  ])

dnl Extended attribute (xattr) support
AC_ARG_ENABLE(xattr,
  [AC_HELP_STRING(
    [--disable-xattr],
    [disable extended attribute support (default=auto)])
  ])

dnl Enable developer code
pr_devel_cflags="-g3 -O0 -Wcast-align -Wchar-subscripts -Winline -Wstrict-prototypes -Wmissing-declarations -Wnested-externs -Wpointer-arith -Wshadow -Wundef"
pr_devel_libs=""

AC_ARG_ENABLE(devel,
  [AC_HELP_STRING(
    [--enable-devel],
    [enable developer-only code (default=no)])
  ],
  [
    if test x"$enableval" != xno ; then
      devel="yes"

      # Additional warnings but only for developer mode.  Note that
      # -Wconversion is a bit noisy at the moment, thus why we
      # selectively choose which warnings to enable.
      PR_CHECK_CC_OPT(Wdangling-else)
      PR_CHECK_CC_OPT(Wextra)
      PR_CHECK_CC_OPT(Werror=implicit-function-declaration)
      PR_CHECK_CC_OPT(Winit-self)
      PR_CHECK_CC_OPT(Wno-missing-field-initializers)
      PR_CHECK_CC_OPT(Wno-unused-parameter)
      PR_CHECK_CC_OPT(Wnull-dereference)
      PR_CHECK_CC_OPT(Wstrict-prototypes)
      PR_CHECK_CC_OPT(fdelete-null-pointer-checks)

      dnl Check to see if specific developer flags were requested

      if test `echo $enableval | grep -c coredump` = "1" ; then
        pr_devel_cflags="-DPR_DEVEL_COREDUMP $pr_devel_cflags"
      fi

      if test `echo $enableval | grep -c coverage` = "1" ; then
        pr_devel_cflags="-DPR_DEVEL_COVERAGE --coverage $pr_devel_cflags"
        pr_devel_libs="--coverage $pr_devel_libs"
      fi

      if test `echo $enableval | grep -c nodaemon` = "1" ; then
        pr_devel_cflags="-DPR_DEVEL_NO_DAEMON $pr_devel_cflags"
      fi

      if test `echo $enableval | grep -c nofork` = "1" ; then
        pr_devel_cflags="-DPR_DEVEL_NO_FORK $pr_devel_cflags"
      fi

      if test `echo $enableval | grep -c profile` = "1" ; then
        pr_devel_cflags="-DPR_DEVEL_PROFILE -p -pg $pr_devel_cflags"
        pr_devel_libs="-pg $pr_devel_libs"
      fi

      if test `echo $enableval | grep -c sanitize` = "1" ; then
        pr_devel_cflags="-fsanitize=address $pr_devel_cflags"
        pr_devel_libs="-fsanitize=address $pr_devel_libs"

        # Determine whether we need to link with libasan (gcc) or not (clang)
        AC_MSG_CHECKING([whether the C compiler accepts -lasan])
        saved_ldflags=$LDFLAGS
        LDFLAGS="-lasan $LDFLAGS"
        AC_TRY_LINK(
          [
          ],
          [
            int i;
            i = 7;
          ],
          [
            AC_MSG_RESULT(yes)
            pr_devel_libs="-lasan $pr_devel_libs"
          ],
          [
            AC_MSG_RESULT(no)
          ]
        )
        LDFLAGS=$saved_ldflags
      fi

      dnl Here is where we WOULD check for the stacktrace developer option.
      dnl However, as of Issue 276, stacktraces are now enabled by default.

      if test `echo $enableval | grep -c timing` = "1"; then
        pr_devel_cflags="-DPR_DEVEL_TIMING $pr_devel_cflags"
      fi

    else
      devel="no"
    fi
  ])

dnl The "tunable" options, from include/options.h
AC_ARG_ENABLE(buffer-size,
  [AC_HELP_STRING(
    [--enable-buffer-size],
    [tune the the size (in bytes) of internal buffers (default=1024)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(buffer size defaulting to 1024 bytes)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_BUFFER_SIZE, 1024,
      [Default buffer size])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_BUFFER_SIZE, $enableval,
      [Default buffer size])
    fi
  ])

AC_ARG_ENABLE(pool-size,
  [AC_HELP_STRING(
    [--enable-pool-size],
    [tune the size (in bytes) of memory pools (default=512)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(pool size defaulting to 512 bytes)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_NEW_POOL_SIZE, 512,
      [Default pool size])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_NEW_POOL_SIZE, $enableval,
      [Default pool size])
    fi
  ])

AC_ARG_ENABLE(scoreboard-buffer-size,
  [AC_HELP_STRING(
    [--enable-scoreboard-buffer-size],
    [tune the the size (in bytes) of certain scoreboard buffers (default=80)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(scoreboard buffer size defaulting to 80 bytes)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_SCOREBOARD_BUFFER_SIZE, 80,
      [Default scoreboard buffer size])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_SCOREBOARD_BUFFER_SIZE, $enableval,
      [Default scoreboard buffer size])
    fi
  ])

AC_ARG_ENABLE(scoreboard-updates,
  [AC_HELP_STRING(
    [--enable-scoreboard-updates],
    [set how often (in loops) the mod_xfer module updates the scoreboard (default=10)])
  ],
  [
    if test x"$enableval" = xyes || test x"$enableval" = xno ; then
      AC_MSG_WARN(scoreboard updates defaulting to 10)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_XFER_SCOREBOARD_UPDATES, 10, [Define the scoreboard update count])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_XFER_SCOREBOARD_UPDATES, $enableval, [Define the scoreboard update count])
    fi
  ])

keepsyms="yes"
AC_ARG_ENABLE(strip,
  [AC_HELP_STRING(
    [--enable-strip],
    [strip debugging symbols from installed code (default=no)])
  ],
  [
    if test x"$enableval" = xyes ; then
      keepsyms="no"
    fi
  ]
)

AC_ARG_ENABLE(timeout-ident,
  [AC_HELP_STRING(
    [--enable-timeout-ident],
    [set the default timeout (in secs) for RFC931 connections (default=10)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(ident timeout defaulting to 10 secs)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTIDENT, 10,
      [Default ident timeout])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTIDENT, $enableval,
      [Default ident timeout])
    fi
  ])

AC_ARG_ENABLE(timeout-idle,
  [AC_HELP_STRING(
    [--enable-timeout-idle],
    [set the default timeout (in secs) for idle connections (default=600)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(idle timeout defaulting to 600 secs)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTIDLE, 600,
      [Default idle timeout])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTIDLE, $enableval,
      [Default idle timeout])
    fi
  ])

AC_ARG_ENABLE(timeout-linger,
  [AC_HELP_STRING(
    [--enable-timeout-linger],
    [set the default timeout (in secs) for lingering closes (default=30)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(linger timeout defaulting to 30 secs)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTLINGER, 30,
      [Default linger timeout])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTLINGER, $enableval,
      [Default linger timeout])
    fi
  ])

AC_ARG_ENABLE(timeout-login,
  [AC_HELP_STRING(
    [--enable-timeout-login],
    [set the default timeout (in secs) for logging in after connecting (default=300)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(login timeout defaulting to 300 secs)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTLOGIN, 300,
      [Default login timeout])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTLOGIN, $enableval,
      [Default login timeout])
    fi
  ])

AC_ARG_ENABLE(timeout-no-transfer,
  [AC_HELP_STRING(
    [--enable-timeout-no-transfer],
    [set the default timeout (in secs) for no data transferred (default=300)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(no data transfer timeout defaulting to 300 secs)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTNOXFER, 300,
      [Default when no data transferred])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTNOXFER, $enableval,
      [Default when no data transferred])
    fi
  ])

AC_ARG_ENABLE(timeout-stalled,
  [AC_HELP_STRING(
    [--enable-timeout-stalled],
    [set the default timeout (in secs) for stalled transfers (default=3600)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(stalled timeout defaulting to 3600 secs)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTSTALLED, 3600,
      [Default stalled timeout])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_TIMEOUTSTALLED, $enableval,
      [Default stalled timeout])
    fi
  ])

AC_ARG_ENABLE(parser-buffer-size,
  [AC_HELP_STRING(
    [--enable-parser-buffer-size],
    [tune the the size (in bytes) of parser buffers (default=4096 bytes)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(parser buffer size defaulting to regular buffer size)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_PARSER_BUFFER_SIZE, 4096,
      [Default buffer size])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_PARSER_BUFFER_SIZE, $enableval,
      [Specified parser buffer size])
    fi
  ])

AC_ARG_ENABLE(transfer-buffer-size,
  [AC_HELP_STRING(
    [--enable-transfer-buffer-size],
    [tune the the size (in bytes) of data transfer buffers (default=OS dependent)])
  ],
  [
    if test "$enableval" = "yes" || test "$enableval" = "no" ; then
      AC_MSG_WARN(transfer buffer size defaulting to regular buffer size)
      AC_DEFINE_UNQUOTED(PR_TUNABLE_XFER_BUFFER_SIZE, 8192,
      [Default buffer size])
    else
      AC_DEFINE_UNQUOTED(PR_TUNABLE_XFER_BUFFER_SIZE, $enableval,
      [Specified transfer buffer size])
    fi
  ])

dnl Checks for libraries.  Yes, this is the hard way, but it's necessary.
AC_CACHE_CHECK(for standalone crypt,pr_cv_lib_standalone_crypt,
  AC_TRY_LINK(,[crypt();],
  	pr_cv_lib_standalone_crypt="yes", pr_cv_lib_standalone_crypt="no" ))

if test "$pr_cv_lib_standalone_crypt" = "no"; then
  AC_CHECK_LIB(crypt, crypt)
fi

AC_CACHE_CHECK(for standalone gethostbyname,pr_cv_lib_standalone_gethost,
  AC_TRY_LINK(,[gethostbyname();],
  	pr_cv_lib_standalone_gethost="yes",
	pr_cv_lib_standalone_gethost="no" ))

if test "$pr_cv_lib_standalone_gethost" = "no"; then
  AC_CHECK_LIB(resolv, gethostbyname)
  AC_CHECK_LIB(resolv, inet_aton)
fi

AC_CACHE_CHECK(for standalone inet_aton,pr_cv_lib_standalone_aton,
  AC_TRY_LINK(,[inet_aton();],
  	pr_cv_lib_standalone_aton="yes",
	pr_cv_lib_standalone_aton="no" ))

if test "$pr_cv_lib_standalone_aton" = "no"; then
  AC_CHECK_LIB(bind, inet_aton)
fi

AC_CACHE_CHECK(for standalone nsl functions,pr_cv_lib_standalone_nsl,[
  AC_TRY_LINK(,[gethostent();],
  pr_cv_lib_standalone_nsl="yes", pr_cv_lib_standalone_nsl="no") ])

if test "$pr_cv_lib_standalone_nsl" = "no"; then
  AC_CHECK_LIB(nsl, gethostent)
fi

AC_CACHE_CHECK(for standalone socket functions,pr_cv_lib_standalone_sockets,
  AC_TRY_LINK(,[bind();],
  pr_cv_lib_standalone_sockets="yes", pr_cv_lib_standalone_sockets="no"))

if test "$pr_cv_lib_standalone_sockets" = "no"; then
  AC_CHECK_LIB(socket, bind)
fi

AC_CACHE_CHECK(for _pw_stayopen variable,pr_cv_var__pw_stayopen,
  AC_TRY_LINK(
    [extern int _pw_stayopen; ],
    [_pw_stayopen = 1;],
    pr_cv_var__pw_stayopen="yes", pr_cv_var__pw_stayopen="no"))

if test "$pr_cv_var__pw_stayopen" = "yes"; then
  AC_DEFINE(HAVE__PW_STAYOPEN, 1, [Define if you have __pw_stayopen variable.])
fi

AC_CHECK_HEADERS(krb.h login.h prot.h usersec.h sys/audit.h)

dnl HP-UX's hpsecurity.h can multiply define MAXINT and confuse configure
AC_CHECK_HEADERS(hpsecurity.h, [
    AC_SEARCH_LIBS(getprpwnam, sec)
  ],
  [
    AC_MSG_CHECKING(for hpsecurity.h workaround)
    AC_TRY_COMPILE([
#include <values.h>
#undef MAXINT
#include <hpsecurity.h>
  ], [],
  [
    AC_DEFINE(HAVE_HPSECURITY_H, 1, [Define you have <hpsecurity.h>])
    AC_SEARCH_LIBS(getprpwnam, sec)
    AC_MSG_RESULT(yes)
  ], [
    AC_MSG_RESULT(no)
  ])
])

dnl AIX's "lastlog" support is done via specific functions, rather than a
dnl struct (Bug#4285).
AC_MSG_CHECKING(for AIX authenticate)
AC_TRY_LINK([
  #include <sys/types.h>
  #ifdef HAVE_USERSEC_H
  # include <usersec.h>
  #else
  # error "we are not on AIX"
  #endif
  ],
  [
    (void) authenticate(NULL, NULL, NULL, NULL);
  ],
  [
    AC_DEFINE(HAVE_AUTHENTICATE, 1, [Define if you have the AIX authenticate function])
    AC_MSG_RESULT(yes)
  ], [
    AC_MSG_RESULT(no)
  ]
)

AC_MSG_CHECKING(for AIX loginfailed)
AC_TRY_LINK([
  #include <sys/types.h>
  #ifdef HAVE_USERSEC_H
  # include <usersec.h>
  #else
  # error "we are not on AIX"
  #endif
  ],
  [
    (void) loginfailed(NULL, NULL, NULL, 0);
  ],
  [
    AC_DEFINE(HAVE_LOGINFAILED, 1, [Define if you have the AIX loginfailed function])
    AC_MSG_RESULT(yes)
  ], [
    AC_MSG_RESULT(no)
  ]
)

AC_MSG_CHECKING(for AIX loginsuccess)
AC_TRY_LINK([
  #include <sys/types.h>
  #ifdef HAVE_USERSEC_H
  # include <usersec.h>
  #else
  # error "we are not on AIX"
  #endif
  ],
  [
    (void) loginsuccess(NULL, NULL, NULL, NULL);
  ],
  [
    AC_DEFINE(HAVE_LOGINSUCCESS, 1, [Define if you have the AIX loginsuccess function]
)
    AC_MSG_RESULT(yes)
  ], [
    AC_MSG_RESULT(no)
  ]
)

dnl Checks for installation user/group

if test x"$install_user" = x; then
  if test "x$target_os" = "xcygwin"; then
    install_user=`id -u`
  else
    install_user=root
  fi
fi
if test x"$install_group" = x ; then
  if test "x$target_os" = "xcygwin"; then
    install_group=`id -g`
  else
    install_group=`sed -n '/.*:.*:0:/{s/^\(.*\):.*:0:.*/\1/p;q;}' /etc/group 2>/dev/null`
  fi
fi

AC_SUBST(install_user)
AC_SUBST(install_group)

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h signal.h linux/prctl.h sys/ioctl.h sys/prctl.h sys/resource.h sys/time.h junistd.h memory.h)
if test x"$force_shadow" != xno ; then
  AC_CHECK_HEADERS(shadow.h,
    [ if test "$use_shadow" = "" && test -f /etc/shadow ; then
        AC_DEFINE(PR_USE_SHADOW, 1, [Define if using shadow password support.])

        AC_CHECK_MEMBER(struct spwd.sp_warn,
          [AC_DEFINE(HAVE_SPWD_SP_WARN, 1, [Define if struct spwd has sp_warn])],, [#include <shadow.h>])
        AC_CHECK_MEMBER(struct spwd.sp_inact,
          [AC_DEFINE(HAVE_SPWD_SP_INACT, 1, [Define if struct spwd has sp_inact])],, [#include <shadow.h>])
        AC_CHECK_MEMBER(struct spwd.sp_expire,
          [AC_DEFINE(HAVE_SPWD_SP_EXPIRE, 1, [Define if struct spwd has sp_expire])],, [#include <shadow.h>])
      fi ])
fi

dnl The pam test used to disable use_shadow, and I'm not sure why, libpam
dnl really shouldn't have anything to do with force_shadow or use_shadow.
if test x"$enable_auth_pam" != xno ; then
  AC_CHECK_HEADERS(security/pam_appl.h security/pam_modules.h pam/pam_appl.h,
    [
      if test `echo $ac_static_modules | grep -c mod_auth_pam` = "0"; then
        ac_static_modules="mod_auth_pam.o $ac_static_modules"
        ac_build_static_modules="modules/mod_auth_pam.o $ac_build_static_modules"
      fi
    ], [],
    [
      #ifdef HAVE_SECURITY_PAM_APPL_H
      # include <security/pam_appl.h>
      #endif
    ])

  dnl This next check looks funky due to a linker problem with some versions
  dnl of the PAM library.  Prior to 0.72 release, the Linux PAM shared library
  dnl omitted requiring libdl linking information. PAM-0.72 or better ships
  dnl with RedHat 6.2 and Debian 2.2 or better.
  dnl
  dnl This test used to add -lpam to the libraries list, however this isn't
  dnl necessary in light of the fact that mod_auth_pam.c has the $Libraries $
  dnl tag and is picked up at configure time.  The only thing we need is to
  dnl add -ldl in the event of broken PAM.

  if test `echo $ac_static_modules | grep -c mod_auth_pam` = "1"; then

	AC_CHECK_LIB(pam, pam_start,
		[AC_DEFINE(HAVE_PAM, 1, [Define if you have PAM support.])],
		[AC_CHECK_LIB(pam, pam_end,
				[AC_DEFINE(HAVE_PAM, 1,
                                  [Define if you have PAM support.])
					LIBS="$LIBS -ldl"],,
				[-ldl])])
  fi

fi

ac_add_mod_cap="no"
ac_have_libcap="no"
if test x"$enable_cap" != xno; then
  case "$host" in
    [*-linux-*])
      rev=`uname -r | awk -F'[[.-]]' '{printf "%03d%03d%03d", $1, $2, $3}'`

      if test $rev -ge 002001097; then
        AC_CHECK_HEADERS(linux/capability.h sys/capability.h)
 
        if test x"$ac_cv_header_linux_capability_h" = "xyes" && test x"$ac_cv_header_sys_capability_h" = "xyes"; then
          ac_add_mod_cap="yes"
        fi

        AC_CHECK_LIB(cap2, cap_init,
          [ac_have_libcap="yes"
           AC_DEFINE(HAVE_LIBCAP2, 1, [Define if libcap2 is present.])

          ],[AC_CHECK_LIB(cap, cap_init,
              [ac_have_libcap="yes"
               AC_DEFINE(HAVE_LIBCAP, 1, [Define if libcap is present.])
              ])
          ])
      fi
  esac
fi

AC_MSG_CHECKING([whether to enable mod_cap])
AC_MSG_RESULT($ac_add_mod_cap)

if test x"$ac_add_mod_cap" = xyes; then

  dnl Make sure that mod_ifsession, if present in the list, appears at
  dnl the end.
  if test x"$ifsession_requested" = xtrue; then
    ac_static_modules=`echo "$ac_static_modules" | sed -e 's/mod_ifsession\.o//g'`
    ac_static_modules="$ac_static_modules mod_cap.o mod_ifsession.o"

    ac_build_static_modules=`echo "$ac_build_static_modules" | sed -e 's/modules\/mod_ifsession\.o//g'`
    ac_build_static_modules="$ac_build_static_modules modules/mod_cap.o modules/mod_ifsession.o";

  else
    ac_static_modules="$ac_static_modules mod_cap.o"
    ac_build_static_modules="$ac_build_static_modules modules/mod_cap.o"
  fi
fi

AC_CHECK_HEADERS(bstring.h crypt.h ctype.h execinfo.h iconv.h inttypes.h langinfo.h limits.h locale.h sasl/sasl.h)
AC_CHECK_HEADERS(string.h strings.h stropts.h)
AC_CHECK_HEADERS(sys/file.h sys/mman.h sys/types.h sys/ucred.h sys/uio.h sys/socket.h)
AC_MSG_CHECKING(for net/if.h)
AC_TRY_COMPILE([
  #include <time.h>
  #ifdef HAVE_SYS_TYPES_H
  # include <sys/types.h>
  #endif
  #ifdef HAVE_SYS_SOCKET_H
  # include <sys/socket.h>
  #endif
  #include <net/if.h>
  ], [
  ],
  [
    AC_DEFINE(HAVE_NET_IF_H, 1, [Define if you have the net/if.h header.])
    AC_MSG_RESULT(yes)
  ], [
    AC_MSG_RESULT(no)
  ])

AC_CHECK_HEADERS(ifaddrs.h)

dnl These particular checks are done a little differently because on some
dnl platforms, <sys/mount.h> wants <sys/param.h> to be included first.
AC_CHECK_HEADERS(sys/param.h sys/mount.h,,,
  [
    [
     #if HAVE_SYS_PARAM_H
     # include <sys/param.h>
     #endif
    ]
  ])

AC_CHECK_HEADERS(netdb.h netinet/in.h)

dnl On AIX, it seems that in order for hstrerror to be properly seen
dnl from netdb.h, we need to define _USE_IRS.  And people wonder why
dnl there's all this effort for compatibility, when "Unix is Unix,
dnl right?"  Yeah, whatever.
AC_MSG_CHECKING(whether netdb.h requires _USE_IRS)
AC_EGREP_HEADER(_USE_IRS, netdb.h,
  [
    AC_DEFINE(_USE_IRS, 1, [Define if netdb.h requires _USE_IRS])
    AC_MSG_RESULT(yes)
  ],
  [
    AC_MSG_RESULT(no)
  ])

AC_CHECK_HEADERS(netinet/in_systm.h,,,
  [
    [
     #if HAVE_SYS_TYPES_H
     # include <sys/types.h>
     #endif
    ]
  ])

AC_CHECK_HEADERS(netinet/ip.h,,,
  [
    [
     #if HAVE_SYS_TYPES_H
     # include <sys/types.h>
     #endif
     #if HAVE_NETINET_IN_H
     # include <netinet/in.h>
     #endif
     #if HAVE_NETINET_IN_SYSTM_H
     # include <netinet/in_systm.h>
     #endif
    ]
  ])

AC_CHECK_HEADERS(netinet/tcp.h arpa/inet.h idna.h libintl.h)
AC_CHECK_HEADERS(regex.h sys/stat.h errno.h sys/termios.h sys/termio.h)
AC_CHECK_HEADERS(sys/statfs.h sys/statvfs.h sys/un.h sys/vfs.h sys/select.h)
AC_CHECK_HEADERS(termios.h dirent.h ndir.h sys/ndir.h sys/dir.h vmsdir.h)
AC_CHECK_HEADERS(ucred.h ucontext.h utime.h utmpx.h)
AC_CHECK_HEADER(syslog.h, have_syslog_h="yes",)
AC_CHECK_HEADERS(curses.h ncurses.h)

dnl Check for the presence of the tzname, timezone, and daylight global
dnl variables.
AC_MSG_CHECKING(for tzname global variable)
AC_TRY_COMPILE([
  #include <time.h>
  ], [
    char *dup[2];
    dup[0] = tzname[0];
    dup[1] = tzname[1];
  ],
  [
    AC_DEFINE(HAVE_TZNAME, 1, [Define if you have the tzname variable.])
    AC_MSG_RESULT(yes)
  ], [
    AC_MSG_RESULT(no)
  ])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_UID_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_GETGROUPS

dnl Check the function signature of getgrouplist()
AC_MSG_CHECKING([whether the getgrouplist(3) function takes ints])
AC_TRY_LINK([
  #define _GNU_SOURCE
  #ifdef HAVE_GRP_H
  # include <grp.h>
  #endif
  #ifdef HAVE_UNISTD_H
  # include <unistd.h>
  #endif
], [
  int (*f)(const char *, int, int *, int *) = getgrouplist;
], [
  AC_MSG_RESULT(yes)
  AC_DEFINE(HAVE_GETGROUPLIST_TAKES_INTS, 1, [Define if getgrouplist3() takes ints as arguments])
], [
  AC_MSG_RESULT(no)
])

AC_HEADER_TIME
AC_STRUCT_TM

AC_CHECK_SIZEOF(short, 0)
AC_CHECK_SIZEOF(int, 0)
AC_CHECK_SIZEOF(long, 0)
AC_CHECK_SIZEOF(long long, 0)
AC_CHECK_SIZEOF(off_t, 0)
AC_CHECK_SIZEOF(size_t, 0)
AC_CHECK_SIZEOF(time_t, 0)
AC_CHECK_SIZEOF(char *, 0)
AC_CHECK_SIZEOF(void *, 0)
AC_CHECK_SIZEOF(uid_t, 0)
AC_CHECK_SIZEOF(gid_t, 0)

dnl Check for generic typedefs
AC_CHECK_TYPE(mode_t, mode_t)
AC_CHECK_TYPE(ino_t, ino_t)
AC_CHECK_TYPE(intptr_t, AC_DEFINE(HAVE_INTPTR_T, 1, [Define if have intpr_t type]),, [
  #if HAVE_INTTYPES_H
  # include <inttypes.h>
  #endif
])

AC_CHECK_TYPES(socklen_t, ,
  [AC_DEFINE(socklen_t, int)],
  [
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_NETDB_H
    # include <netdb.h>
    #endif
    #include <sys/socket.h>
  ])

dnl See what type of utmp exists
AC_CHECK_HEADERS(utmp.h, have_utmp=1, have_utmp=0)

if test $have_utmp; then
  AC_CACHE_CHECK(whether struct utmp has ut_user,
  		pr_cv_header_utmaxtype,
	AC_EGREP_HEADER([ *ut_user.*;],utmp.h,
			pr_cv_header_utmaxtype="yes",
			pr_cv_header_utmaxtype="no"))
  AC_CACHE_CHECK(whether struct utmp has ut_host,
  		pr_cv_header_ut_host,
	AC_EGREP_HEADER([ *ut_host.*;],utmp.h,
			pr_cv_header_ut_host="yes",
			pr_cv_header_ut_host="no"))
  AC_CACHE_CHECK(whether struct utmp has ut_exit,
		pr_cv_header_ut_exit,
	AC_EGREP_HEADER([ *ut_exit.*;],utmp.h,
			pr_cv_header_ut_exit="yes",
			pr_cv_header_ut_exit="no"))
  if test "$pr_cv_header_utmaxtype" = "yes"; then
    AC_DEFINE(HAVE_UTMAXTYPE, 1, [Define if struct utmp has ut_user.])
  fi
  if test "$pr_cv_header_ut_host" = "yes"; then
    AC_DEFINE(HAVE_UT_UT_HOST, 1, [Define if struct utmp has ut_host.])
  fi
  if test "$pr_cv_header_ut_exit" = "yes"; then
    AC_DEFINE(HAVE_UT_UT_EXIT, 1, [Define if struct utmp has ut_exit.])
  fi
fi

dnl See if various LOG_ macros are defined
if test "$have_syslog_h" = "yes"; then
  AC_DEFINE(HAVE_SYSLOG_H, 1, [Define if you have <syslog.h>])

  AC_CACHE_CHECK(whether syslog.h defines LOG_CRON,
  		pr_cv_header_syslog_log_cron,
  	AC_EGREP_CPP(yes,[
#include <syslog.h>
#ifdef LOG_CRON
  yes
#endif
	],pr_cv_header_syslog_log_cron="yes",
	pr_cv_header_syslog_log_cron="no") )

  AC_CACHE_CHECK(whether syslog.h defines LOG_FTP,
  		pr_cv_header_syslog_log_ftp,
  	AC_EGREP_CPP(yes,[
#include <syslog.h>
#ifdef LOG_FTP
  yes
#endif
	],pr_cv_header_syslog_log_ftp="yes",
	pr_cv_header_syslog_log_ftp="no") )

  if test "$pr_cv_header_syslog_log_cron" = "yes"; then
    AC_DEFINE(HAVE_LOG_CRON, 1, [Define if you have LOG_CRON])
  fi
  if test "$pr_cv_header_syslog_log_ftp" = "yes"; then
    AC_DEFINE(HAVE_LOG_FTP, 1, [Define if you have LOG_FTP])
  fi
fi

dnl Check for directory file descriptor member ("d_fd", "dd_fd" or "__dd_fd")
dnl of the DIR struct, possibly defining one of:
dnl	HAVE_STRUCT_DIR_D_FD
dnl	HAVE_STRUCT_DIR_DD_FD
dnl	HAVE_STRUCT_DIR___DD_FD
dnl Should use AC_C_STRUCT_MEMBER, when available in an official release.
dnl The autoconf-2.14.1 floating around is NOT an official release.
dnl Note: use of either the _POSIX_C_SOURCE or the _XOPEN_SOURCE preprocessor
dnl macro could result in a wrong answer unless it is also defined here.
dnl Doing so may be automatic or may take some care or reordering.
dnl NB: autoheader won't pick up these macros, requiring acconfig.h entries.
dnl
for dirfd in d_fd dd_fd __dd_fd ; do
	AC_MSG_CHECKING(for $dirfd in DIR structure)
	AC_TRY_COMPILE([
#include <stdio.h>
#include <dirent.h>
], [
DIR *dirp;
int i = dirp->$dirfd;
], eval ac_cv_struct_dir_$dirfd=yes,)
	if test "`eval echo x'$''ac_cv_struct_dir_'$dirfd`" = xyes ; then
		ucdirfd=`echo $dirfd | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`
		AC_DEFINE_UNQUOTED(HAVE_STRUCT_DIR_$ucdirfd)
		AC_MSG_RESULT(yes)
		break
	else
		AC_MSG_RESULT(no)
	fi
done


dnl Checks for library functions.

AC_FUNC_ALLOCA

dnl UnixWare's alloca is in libucb, in /usr/ucblib/. Muck about with
dnl $LDFLAGS so AC_CHECK_LIB() will find it there.
old_LDFLAGS=$LDFLAGS
LDFLAGS="$LDFLAGS -L/usr/ucblib/"
AC_CHECK_LIB(ucb, alloca)
LDFLAGS=$old_LDFLAGS

AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF

AC_CHECK_FUNCS(bcopy crypt ctime_r fdatasync fgetspent flock fpathconf freeaddrinfo fsync futimes getifaddrs getpgid getpgrp gmtime_r localtime_r mkdtemp nl_langinfo)

AC_CHECK_FUNC(gai_strerror,
  AC_DEFINE(HAVE_GAI_STRERROR, 1,
    [Define if you have the gai_strerror() function]),
  LIB_OBJS="$LIB_OBJS glibc-gai_strerror.o"
)

AC_MSG_CHECKING([for iconv])
AC_TRY_LINK(
  [ #ifdef HAVE_STDDEF_H
    # include <stddef.h>
    #endif
    #ifdef HAVE_STDLIB_H
    # include <stdlib.h>
    #endif
    #ifdef HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #ifdef HAVE_ICONV_H
    # include <iconv.h>
    #endif
  ],
  [ 
    size_t res, in_len = 0, out_len = 0;
    const char *in = NULL;
    char *out = NULL;
    res = iconv((iconv_t)-1, &in, &in_len, &out, &out_len);
  ],
  [
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_ICONV, 1, [Define if you have iconv])
  ],
  [
    AC_MSG_RESULT(no)
  ]
)

AC_MSG_CHECKING([for idna_to_ascii_8z])
AC_TRY_COMPILE(
  [
    #include <sys/types.h>
    #ifdef HAVE_IDNA_H
    #  include <idna.h>
    #endif
  ],
  [
    int flags = 0, res = 0;
    const char *input = NULL;
    char *output = NULL;
    res = idna_to_ascii_8z(input, &output, flags);
  ],
  [
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_IDNA_TO_ASCII_8Z, 1, [Define if you have idna_to_ascii_8z])
    MAIN_LIBS="$MAIN_LIBS -lidn"
  ],
  [
    AC_MSG_RESULT(no)
  ]
)

AC_MSG_CHECKING([for dirfd])
AC_TRY_LINK(
  [ #ifdef HAVE_STDDEF_H
    # include <stddef.h>
    #endif
    #ifdef HAVE_STDLIB_H
    # include <stdlib.h>
    #endif
    #ifdef HAVE_STDIO_H
    # include <stdio.h>
    #endif
    #ifdef HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_DIRENT_H
    # include <dirent.h>
    #endif
  ],
  [
    DIR *dirh = NULL;
    int fd;

    fd = dirfd(dirh);
  ],
  [
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_DIRFD, 1, [Define if you have dirfd])
  ],
  [
    AC_MSG_RESULT(no)
  ]
)

dnl getaddrinfo is a #define on Tru64 Unix.  How annoying.
AC_MSG_CHECKING([for getaddrinfo])
AC_TRY_LINK(
  [ #ifdef HAVE_STDDEF_H
    # include <stddef.h>
    #endif
    #ifdef HAVE_STDLIB_H
    # include <stdlib.h>
    #endif
    #ifdef HAVE_STDIO_H
    # include <stdio.h>
    #endif
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_SYS_SOCKET_H
    # include <sys/socket.h>
    #endif
    #if HAVE_NETDB_H
    # include <netdb.h>
    #endif
  ],
  [
    getaddrinfo(NULL, NULL, NULL, NULL);
  ],
  [
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_GETADDRINFO, 1, [Define if you have getaddrinfo])
  ],
  [
    AC_MSG_RESULT(no)
  ]
)

AC_CHECK_FUNCS(getcwd getenv getgrouplist getgroups getgrset gethostbyname2 gethostname getnameinfo)
AC_CHECK_FUNCS(gettimeofday hstrerror inet_aton inet_ntop inet_pton initgroups)
AC_CHECK_FUNCS(loginrestrictions)
AC_CHECK_FUNCS(explicit_bzero memcpy mempcpy memset_s mkdir mkstemp mlock mlockall munlock munlockall)
AC_CHECK_FUNCS(pathconf posix_fadvise pread prctl putenv pwrite random regcomp rmdir select setgroups socket srandom statfs strchr strcoll strerror timingsafe_bcmp)
AC_CHECK_FUNCS(strlcat strlcpy strsep strtod strtof strtol strtoll strtoull setprotoent setspent endprotoent)
# __snprintf and __vsnprintf are only on solaris and _really_ broken there.
AC_CHECK_FUNCS(vsnprintf snprintf)
if test x"$ac_cv_func_vsnprintf" != xyes || test x"$ac_cv_func_snprintf" != xyes
then
	AC_CHECK_FUNCS(fconvert fcvt)
	AC_CHECK_HEADERS(floatingpoint.h)
fi
AC_CHECK_FUNCS(setsid setgroupent seteuid setegid setenv setpgid siginterrupt)
AC_CHECK_FUNCS(tzset uname unsetenv)

AC_CHECK_FUNC(setpassent,
[case "$target_os:$enable_force_setpassent" in
changequote(, )dnl
  *freebsd[23]\.[0-3]*:)
changequote([, ])dnl
    AC_MSG_WARN(Disabling broken FreeBSD setpassent(), see README for details.)
        ;;
  *:no) ;;
  *) AC_DEFINE(HAVE_SETPASSENT, 1, [Define if you have setpassent()]) ;;
esac])

dnl Controls
if test x"$enable_ctrls" = xyes; then
  AC_CHECK_FUNCS(getpeereid getpeerucred)
  ac_static_modules="$ac_static_modules mod_ctrls.o"
  ac_build_static_modules="$ac_build_static_modules modules/mod_ctrls.o"
fi

dnl NLS/LANG support
if test x"$enable_nls" = xyes; then
  ac_static_modules="$ac_static_modules mod_lang.o"
  ac_build_static_modules="$ac_build_static_modules modules/mod_lang.o"

  dnl Make sure that mod_ifsession, if present in the list, appears at the end.
  if test x"$ifsession_requested" = xtrue; then
    ac_static_modules=`echo "$ac_static_modules" | sed -e 's/mod_ifsession\.o//g'`
    ac_static_modules="$ac_static_modules mod_ifsession.o"

    ac_build_static_modules=`echo "$ac_build_static_modules" | sed -e 's/modules\/mod_ifsession\.o//g'`
    ac_build_static_modules="$ac_build_static_modules modules/mod_ifsession.o";
  fi
fi

dnl Controls-related checks
AC_CHECK_MEMBER(struct cmsgcred.cmcred_uid,
  [AC_DEFINE(HAVE_STRUCT_CMSGCRED, 1, [Define if you have struct cmsgcred])],,
  [
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_SYS_SOCKET_H
    # include <sys/socket.h>
    #endif
    #if HAVE_SYS_UIO_H
    # include <sys/uio.h>
    #endif
  ])

AC_CHECK_MEMBER(struct sockcred.sc_uid,
  [AC_DEFINE(HAVE_STRUCT_SOCKCRED, 1, [Define if you have struct sockcred])],,
  [
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_SYS_SOCKET_H
    # include <sys/socket.h>
    #endif
    #if HAVE_SYS_UIO_H
    # include <sys/uio.h>
    #endif
  ])

# See:
#   https://github.com/proftpd/proftpd/issues/75
AC_CHECK_MEMBER(struct sockpeercred.uid,
  [AC_DEFINE(HAVE_STRUCT_SOCKPEERCRED, 1, [Define if you have struct sockpeercred])],,
  [
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_SYS_SOCKET_H
    # include <sys/socket.h>
    #endif
    #if HAVE_SYS_UIO_H
    # include <sys/uio.h>
    #endif
  ])

dnl IPv4/IPv6-related checks
AC_CHECK_MEMBER(struct sockaddr_in.sin_len,
  [AC_DEFINE(SIN_LEN, 1, [Define if you have sockaddr_in.sin_len])],,
  [
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_SYS_SOCKET_H
    # include <sys/socket.h>
    #endif
    #if HAVE_NETINET_IN_H
    # include <netinet/in.h>
    #endif
  ])

dnl NFS-related checks
AC_CHECK_MEMBER(struct statfs.f_fstypename,
  [AC_DEFINE(HAVE_STATFS_F_FSTYPENAME, 1, [Define if you have struct statfs.f_fstypename])],,
  [
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_SYS_PARAM_H
    # include <sys/param.h>
    #endif
    #if HAVE_SYS_MOUNT_H
    # include <sys/mount.h>
    #endif
    #if HAVE_SYS_VFS_H
    # include <sys/vfs.h>
    #endif
    #if HAVE_SYS_STATFS_H
    # include <sys/statfs.h>
    #endif
  ])

AC_CHECK_MEMBER(struct statfs.f_type,
  [AC_DEFINE(HAVE_STATFS_F_TYPE, 1, [Define if you have struct statfs.f_type])],,
  [
    #if HAVE_SYS_TYPES_H
    # include <sys/types.h>
    #endif
    #if HAVE_SYS_PARAM_H
    # include <sys/param.h>
    #endif
    #if HAVE_SYS_MOUNT_H
    # include <sys/mount.h>
    #endif
    #if HAVE_SYS_VFS_H
    # include <sys/vfs.h>
    #endif
    #if HAVE_SYS_STATFS_H
    # include <sys/statfs.h>
    #endif
  ])

dnl Largefile support
if test x"$enable_largefile" = xno; then
  AC_DEFINE(PR_USE_LARGEFILES, 0, [Define if you have largefile support])
else
  AC_DEFINE(PR_USE_LARGEFILES, 1, [Define if you have largefile support])
fi

PR_CHECK_STRUCT_ADDRINFO
PR_CHECK_STRUCT_SS
PR_CHECK_SS_FAMILY
PR_CHECK_SS_LEN

dnl POSIX ACL checks.  Always perform these, in case the administrator
dnl wants to use mod_facl.
AC_CHECK_HEADERS(sys/acl.h acl/libacl.h)
AC_CHECK_LIB(acl, perm_copy_fd,
  [AC_DEFINE(HAVE_LIBACL, 1, [Define if you have libacl])]
)
AC_CACHE_CHECK(
  [which POSIX ACL implementation to use],
  pr_cv_func_facl,
  pr_cv_func_facl="none"

  dnl BSD.
  if test "$pr_cv_func_facl" = "none"; then
    AC_TRY_LINK(
      [ #include <sys/types.h>
        #ifdef HAVE_SYS_ACL_H
        # include <sys/acl.h>
        #endif
        #ifdef HAVE_ACL_LIBACL_H
        # include <acl/libacl.h>
        #endif
      ],
      [ acl_permset_t permset;
        /* On BSD, ACL_READ_DATA is a #define. */
        #ifdef ACL_READ_DATA
        acl_perm_t perm = ACL_READ_DATA;
        #else
        # error "ACL_READ_DATA not #defined on this platform"
        #endif /* ACL_READ_DATA */
        (void)acl_get_perm_np(permset, perm);
      ], pr_cv_func_facl="BSD")
  fi

  dnl Linux.
  if test "$pr_cv_func_facl" = "none"; then
    old_ldflags=$LDFLAGS
    old_libs=$LIBS
    new_ldflags=`echo "$LDFLAGS" | sed -e 's/-L\$(top_srcdir)\/lib//g'`
    LDFLAGS="$new_ldflags"
    LIBS="-lacl $LIBS"
    AC_TRY_LINK(
      [ #include <sys/types.h>
        #ifdef HAVE_SYS_ACL_H
        # include <sys/acl.h>
        #endif
        #ifdef HAVE_ACL_LIBACL_H
        # include <acl/libacl.h>
        #endif
      ],
      [ acl_permset_t permset;
        acl_perm_t perm;
        (void)acl_get_perm(permset, perm);
      ], pr_cv_func_facl="Linux")
    LDFLAGS=$old_ldflags
    LIBS=$old_libs
  fi

  dnl MacOSX.
  if test "$pr_cv_func_facl" = "none"; then
    AC_TRY_LINK(
      [ #include <sys/types.h>
        #ifdef HAVE_SYS_ACL_H
        # include <sys/acl.h>
        #endif
        #ifdef HAVE_ACL_LIBACL_H
        # include <acl/libacl.h>
        #endif
      ],
      [ acl_entry_t ae;
        /* On Mac, ACL_READ_DATA is an enum value. */
        acl_perm_t perm = ACL_READ_DATA;
        (void)acl_get_qualifier(ae);
      ], pr_cv_func_facl="MacOSX")
  fi

  dnl Solaris.
  if test "$pr_cv_func_facl" = "none"; then
    old_ldflags=$LDFLAGS
    LDFLAGS="-lsec $LDFLAGS"
    AC_TRY_LINK(
      [ #ifdef HAVE_STDDEF_H
        # include <stddef.h>
        #endif
        #ifdef HAVE_STDLIB_H
        # include <stdlib.h>
        #endif
        #ifdef HAVE_SYS_TYPES_H
        # include <sys/types.h>
        #endif
        #ifdef HAVE_SYS_ACL_H
        # include <sys/acl.h>
        #endif
        #ifdef HAVE_ACL_LIBACL_H
        # include <acl/libacl.h>
        #endif
      ],
      [ aclent_t ae;
        (void)aclcheck(&ae,0,NULL);
      ], pr_cv_func_facl="Solaris")
    LDFLAGS=$old_ldflags
  fi
)

dnl Now, set the appropriate defines based on our investigations...
if test "$pr_cv_func_facl" != "none"; then
  AC_DEFINE(HAVE_POSIX_ACL, 1, [Define if you have POSIX ACL support])

  case "$pr_cv_func_facl" in
    "BSD")
      AC_DEFINE(HAVE_BSD_POSIX_ACL, 1,
        [Define if you have BSD-flavoured POSIX ACL support])
      ;;

    "Linux")
      AC_DEFINE(HAVE_LINUX_POSIX_ACL, 1,
        [Define if you have Linux-flavoured POSIX ACL support])
      ;;

    "MacOSX")
      AC_DEFINE(HAVE_MACOSX_POSIX_ACL, 1,
        [Define if you have MacOSX-flavoured POSIX ACL support])
      ;;

    "Solaris")
      AC_DEFINE(HAVE_LIBSEC, 1, [Define if you have libsec])
      AC_DEFINE(HAVE_SOLARIS_POSIX_ACL, 1,
        [Define if you have Solaris-flavoured POSIX ACL support])
      ;;
  esac
fi

if test x"$enable_facl" = xyes ; then
  AC_DEFINE(PR_USE_FACL, 1, [Define if using POSIX ACL support.])

  case "$pr_cv_func_facl" in
    "Linux")
      ac_build_addl_libs="-lacl $ac_build_addl_libs"
      ;;

    "Solaris")
      ac_build_addl_libs="-lsec $ac_build_addl_libs"
      ;;
  esac
fi

dnl IPv6.  Remember that the option is --disable-ipv6, despite the
dnl $enable_ipv6 variable name.
if test x"$enable_ipv6" != xno ; then
  AC_DEFINE(PR_USE_IPV6, 1, [Define if using IPv6 support.])
fi

dnl sendfile() checks
if test x"$enable_sendfile" != xno ; then
  AC_CACHE_CHECK(
    [which sendfile() implementation to use],
    pr_cv_func_sendfile,
    pr_cv_func_sendfile="none"

    dnl Linux.
    if test "$pr_cv_func_sendfile" = "none"; then
      AC_TRY_LINK(
        [ #include <sys/types.h>
          #include <sys/sendfile.h>
          #include <unistd.h>
        ],
        [ int i;
          off_t o;
          size_t c;
          (void)sendfile(i,i,&o,c);
        ], pr_cv_func_sendfile="Linux")
    fi

    dnl BSD.
    if test "$pr_cv_func_sendfile" = "none"; then
      AC_TRY_LINK(
        [ #include <sys/types.h>
          #include <sys/socket.h>
          #include <sys/uio.h>
        ],
        [ int i;
          off_t o;
          size_t n;
          struct sf_hdtr h;
          (void)sendfile(i,i,o,n,&h,&o,i);
        ], pr_cv_func_sendfile="BSD")
    fi

    dnl AIX.
    if test "$pr_cv_func_sendfile" = "none"; then
      AC_TRY_LINK(
        [ #include <sys/types.h>
          #include <sys/socket.h>
        ],
        [ uint f;
          int h;
          struct sf_parms p;
          (void)send_file(&(h),&(p),f);
        ], pr_cv_func_sendfile="AIX")
    fi

    dnl Solaris
    if test "$pr_cv_func_sendfile" = "none"; then
      old_ldflags=$LDFLAGS
      LDFLAGS="-lsendfile $LDFLAGS"
      AC_TRY_LINK(
        [ #include <sys/types.h>
          #include <sys/sendfile.h>
          #include <unistd.h>
        ],
        [ int i;
          off_t o;
          size_t c;
          (void)sendfile(i,i,&o,c);
        ], pr_cv_func_sendfile="Solaris")
      LDFLAGS=$old_ldflags
    fi

    dnl Mac OSX (10.5 and later)
    if test "$pr_cv_func_sendfile" = "none"; then
      AC_TRY_LINK(
        [ #include <sys/types.h>
          #include <sys/socket.h>
          #include <sys/uio.h>
        ],
        [ int i;
          off_t o, n;
          struct sf_hdtr h;
          (void)sendfile(i,i,o,&n,&h,i);
        ], pr_cv_func_sendfile="MacOSX")
    fi
  )

  dnl Now, set the appropriate defines based on our investigations...

  if test x"$pr_cv_func_sendfile" != x"none"; then
    dnl Avoid defining these for AIX and its buggy sendfile implementation,
    dnl as per Bug#3675
    if test x"$pr_cv_func_sendfile" != x"AIX"; then
      AC_DEFINE(HAVE_SENDFILE, 1, [Define if you have <sendfile.h>])
      AC_DEFINE(PR_USE_SENDFILE, 1,
        [Define if using sendfile support.])
    fi
  fi

  case "$pr_cv_func_sendfile" in
    "Linux")
      AC_CHECK_HEADERS(sys/sendfile.h)
      AC_DEFINE(HAVE_LINUX_SENDFILE, 1,
        [Define if using Linux sendfile support.])
      ;;

    "BSD")
      AC_DEFINE(HAVE_BSD_SENDFILE, 1,
        [Define if using BSD sendfile support.])
      ;;

    "AIX")
      AC_MSG_WARN([** AIX sendfile support automatically disabled **])
      ;;

    "Solaris")
      AC_CHECK_HEADERS(sys/sendfile.h)
      AC_DEFINE(HAVE_SOLARIS_SENDFILE, 1,
        [Define if using Solaris sendfile support.])
      ac_build_addl_libs="-lsendfile $ac_build_addl_libs"
      ;;

    "MacOSX")
      AC_DEFINE(HAVE_MACOSX_SENDFILE, 1,
        [Define if using MacOSX sendfile support.])
      ;;
  esac
fi

dnl Trace checks
if test x"$enable_trace" != xno ; then
  AC_DEFINE(PR_USE_TRACE, 1, [Define for trace support])
fi

dnl Extended attribute checks
if test x"$enable_xattr" != xno ; then
  # On Free/Net/OpenBSD, it's sys/extattr.h
  AC_CHECK_HEADER(sys/extattr.h,
    [AC_DEFINE(HAVE_SYS_EXTATTR_H, 1, [Define if sys/extattr.h is present.])
     AC_DEFINE(PR_USE_XATTR, 1, [Define if using xattr support.])

     AC_MSG_CHECKING([for extattr_delete_link])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_EXTATTR_H
         # include <sys/extattr.h>
         #endif
       ],
       [
         int res;
         int namespace = 0;
         const char *path = NULL, name = NULL;
         res = extattr_delete_link(path, namespace, name);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_EXTATTR_DELETE_LINK, 1, [Define if you have extattr_delete_link])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )

     AC_MSG_CHECKING([for extattr_get_link])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_EXTATTR_H
         # include <sys/extattr.h>
         #endif
       ],
       [
         ssize_t res;
         int namespace = 0;
         const char *path = NULL, name = NULL;
         void *val = NULL;
         size_t sz = 0;
         res = extattr_get_link(path, namespace, name, val, sz);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_EXTATTR_GET_LINK, 1, [Define if you have extattr_get_link])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )

     AC_MSG_CHECKING([for extattr_list_link])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_EXTATTR_H
         # include <sys/extattr.h>
         #endif
       ],
       [
         ssize_t res;
         int namespace = 0;
         const char *path = NULL;
         void *val = NULL;
         size_t sz = 0;
         res = extattr_list_link(path, namespace, val, sz);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_EXTATTR_LIST_LINK, 1, [Define if you have extattr_list_link])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )

     AC_MSG_CHECKING([for extattr_set_link])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_EXTATTR_H
         # include <sys/extattr.h>
         #endif
       ],
       [
         int res;
         int namespace = 0;
         const char *path = NULL, name = NULL;
         void *val = NULL;
         size_t sz = 0;
         res = extattr_set_link(path, namespace, name, val, sz);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_EXTATTR_SET_LINK, 1, [Define if you have extattr_set_link])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )
   ])

  # On Linux/MacOSX, it's sys/xattr.h
  AC_CHECK_HEADER(sys/xattr.h,
    [AC_DEFINE(HAVE_SYS_XATTR_H, 1, [Define if sys/xattr.h is present.])
     AC_DEFINE(PR_USE_XATTR, 1, [Define if using xattr support.])
     AC_CHECK_HEADERS(attr/xattr.h)

     # Some platforms need libattr for extended attributes
     AC_CHECK_LIB(attr, setxattr)

     AC_MSG_CHECKING([for lgetxattr])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_XATTR_H
         # include <sys/xattr.h>
         #endif
       ],
       [
         ssize_t res;
         const char *path = NULL, *name = NULL;
         void *val = NULL;
         size_t sz = 0;
         res = lgetxattr(path, name, val, sz);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_LGETXATTR, 1, [Define if you have lgetxattr])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )

     AC_MSG_CHECKING([for llistxattr])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_XATTR_H
         # include <sys/xattr.h>
         #endif
       ],
       [
         ssize_t res;
         const char *path = NULL;
         char *names = NUL;
         size_t namessz = 0;
         res = llistxattr(path, names, namessz);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_LLISTXATTR, 1, [Define if you have llistxattr])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )

     AC_MSG_CHECKING([for lremovexattr])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_XATTR_H
         # include <sys/xattr.h>
         #endif
       ],
       [
         ssize_t res;
         const char *path = NULL, *name = NULL;
         res = lremovexattr(path, name);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_LREMOVEXATTR, 1, [Define if you have lremovexattr])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )

     AC_MSG_CHECKING([for lsetxattr])
     AC_TRY_LINK(
       [ #ifdef HAVE_STDDEF_H
         # include <stddef.h>
         #endif
         #ifdef HAVE_STDLIB_H
         # include <stdlib.h>
         #endif
         #ifdef HAVE_SYS_TYPES_H
         # include <sys/types.h>
         #endif
         #ifdef HAVE_SYS_XATTR_H
         # include <sys/xattr.h>
         #endif
       ],
       [
         int res, flags = 0;
         const char *path = NULL, *name = NULL;
         const void *val = NULL;
         res = lsetxattr(path, name, val, flags);
       ],
       [
         AC_MSG_RESULT(yes)
         AC_DEFINE(HAVE_LSETXATTR, 1, [Define if you have lsetxattr])
       ],
       [
         AC_MSG_RESULT(no)
       ]
     )
   ])
fi

dnl Custom-rolled macro for checking return type of setgrent(3)
PR_FUNC_SETGRENT_VOID

if test x"$enable_curses" != xno ; then
  AC_CHECK_LIB(curses, initscr,
    [ CURSES_LIBS="-lcurses"
      AC_DEFINE(HAVE_LIBCURSES, 1, [Define if you have curses])
      AC_DEFINE(PR_USE_CURSES, 1, [Define if using curses support])
    ])

  dnl Check for the libtinfo library, which contains the stdscr symbol,
  dnl halfdelay curses function on some systems (e.g. Gentoo); see
  dnl Issue #1207.  Note that on some systems, this may need to be libtinfow
  dnl instead.
  if test x"$enable_nls" = xyes ; then
    AC_CHECK_LIB(tinfow, halfdelay,
      [ UTILS_LIBS="$UTILS_LIBS -ltinfow"
        AC_DEFINE(HAVE_LIBTINFOW, 1, [Define if you have libtinfow])
      ],
      [ AC_CHECK_LIB(tinfo, halfdelay,
        [ UTILS_LIBS="$UTILS_LIBS -ltinfo"
          AC_DEFINE(HAVE_LIBTINFO, 1, [Define if you have libtinfo])
        ])
      ])
  else
    AC_CHECK_LIB(tinfo, halfdelay,
      [ UTILS_LIBS="$UTILS_LIBS -ltinfo"
        AC_DEFINE(HAVE_LIBTINFO, 1, [Define if you have libtinfo])
      ])
  fi
fi

if test x"$enable_ncurses" != xno ; then
  pr_have_ncursesw="no"

  dnl If NLS support has been enabled, then check for the locale-sensitive
  dnl libncursesw library first; if not found, or if no NLS support, then
  dnl check for libncurses.

  if test x"$enable_nls" = xyes ; then
    AC_CHECK_LIB(ncursesw, initscr,
      [ CURSES_LIBS="-lncursesw"
        pr_have_ncursesw="yes"
        AC_DEFINE(HAVE_LIBNCURSESW, 1, [Define if you have ncursesw])
      ], [
        AC_CHECK_LIB(ncurses, initscr,
          [ CURSES_LIBS="-lncurses"
            AC_DEFINE(HAVE_LIBNCURSES, 1, [Define if you have ncurses])
          ])
      ])

  else
    AC_CHECK_LIB(ncurses, initscr,
      [ CURSES_LIBS="-lncurses"
        AC_DEFINE(HAVE_LIBNCURSES, 1, [Define if you have ncurses])
      ])
  fi

  dnl Check for the libtinfo library, which contains the halfdelay() curses
  dnl function on some systems (e.g. OpenSuSE); see Bug#3718.  Note that on
  dnl some systems, this may need to be libtinfow instead; see Issue #1174.

  if test x"$pr_have_ncursesw" = xyes ; then
    AC_DEFINE(PR_USE_NCURSESW, 1, [Define if using ncursesw support])
    AC_CHECK_LIB(tinfow, halfdelay,
      [ UTILS_LIBS="$UTILS_LIBS -ltinfow"
        AC_DEFINE(HAVE_LIBTINFOW, 1, [Define if you have libtinfow])
      ],
      [ AC_CHECK_LIB(tinfo, halfdelay,
        [ UTILS_LIBS="$UTILS_LIBS -ltinfo"
          AC_DEFINE(HAVE_LIBTINFO, 1, [Define if you have libtinfo])
        ])
      ])
  else
    AC_DEFINE(PR_USE_NCURSES, 1, [Define if using ncurses support])
    AC_CHECK_LIB(tinfo, halfdelay,
      [ UTILS_LIBS="$UTILS_LIBS -ltinfo"
        AC_DEFINE(HAVE_LIBTINFO, 1, [Define if you have libtinfo])
      ])
  fi
fi

if test x"$enable_nonblocking_log_open" != xno; then
  AC_DEFINE(PR_USE_NONBLOCKING_LOG_OPEN, 1, [Define if using nonblocking open of log files])
fi

if test x"$enable_ident" == xyes ; then
  ac_static_modules="mod_ident.o $ac_static_modules"
  ac_build_static_modules="modules/mod_ident.o $ac_build_static_modules"
fi

dnl Check for various argv[] replacing functions on various OSs
AC_CHECK_FUNCS(setproctitle)
AC_CHECK_HEADERS(libutil.h)
AC_CHECK_LIB(util, setproctitle,
	[AC_DEFINE(HAVE_SETPROCTITLE)
		ac_cv_func_setproctitle="yes" ; LIBS="$LIBS -lutil"])

if test "$ac_cv_func_setproctitle" = "yes"; then
  AC_DEFINE(PF_ARGV_TYPE, PF_ARGV_NONE)
else
  pf_argv_set="no"

  AC_CHECK_HEADERS(sys/pstat.h,have_pstat_h="yes",have_pstat_h="no")
  if test "$have_pstat_h" = "yes"; then
    AC_CHECK_FUNCS(pstat)
    
    if test "$ac_cv_func_pstat" = "yes"; then
	AC_DEFINE(PF_ARGV_TYPE, PF_ARGV_PSTAT)
    else
	AC_DEFINE(PF_ARGV_TYPE, PF_ARGV_WRITEABLE)
    fi

    pf_argv_set="yes"
  fi

  if test "$pf_argv_set" = "no"; then
    AC_EGREP_HEADER([#define.*PS_STRINGS.*],sys/exec.h,
			have_psstrings="yes",have_psstrings="no")
    if test "$have_psstrings" = "yes"; then
	AC_DEFINE(PF_ARGV_TYPE, PF_ARGV_PSSTRINGS)
	pf_argv_set="yes"
    fi
  fi

  if test "$pf_argv_set" = "no"; then
    AC_CACHE_CHECK(whether __progname and __progname_full are available,
		    pf_cv_var_progname,
		    AC_TRY_LINK([extern char *__progname, *__progname_full;],
			[__progname = "foo"; __progname_full = "foo bar";],
			pf_cv_var_progname="yes", pf_cv_var_progname="no"))

    if test "$pf_cv_var_progname" = "yes"; then
	AC_DEFINE(HAVE___PROGNAME, 1, [Define if you have __progname])
    fi

    AC_CACHE_CHECK(which argv replacement method to use,
		    pf_cv_argv_type,
		    AC_EGREP_CPP(yes,[
#if defined(__GNU_HURD__)
  yes
#endif
  ],pf_cv_argv_type="new", pf_cv_argv_type="writeable"))

    if test "$pf_cv_argv_type" = "new"; then
	AC_DEFINE(PF_ARGV_TYPE, PF_ARGV_NEW)
	pf_argv_set="yes"
    fi

    if test "$pf_argv_set" = "no"; then
	AC_DEFINE(PF_ARGV_TYPE, PF_ARGV_WRITEABLE)
    fi
  fi
fi

dnl Check for stacktrace support
dnl On FreeBSD, the libexecinfo port is needed for the backtrace(3) function;
dnl we thus also need to check for the libexecinfo library
AC_CHECK_LIB(execinfo, backtrace)

dnl Some libcs need the execinfo.h header for their backtrace symbols, and
dnl some (like Solaris) want ucontext.h.  Check for those headers here.
AC_CHECK_HEADERS(execinfo.h ucontext.h)

dnl Make sure that we can find the backtrace(3) and backtrace_symbols(3)
dnl functions
AC_MSG_CHECKING([for backtrace])
AC_TRY_LINK(
  [
    #ifdef HAVE_STDDEF_H
    # include <stddef.h>
    #endif
    #ifdef HAVE_STDLIB_H
    # include <stdlib.h>
    #endif
    #ifdef HAVE_EXECINFO_H
    # include <execinfo.h>
    #endif
    #ifdef HAVE_UCONTEXT_H
    # include <ucontext.h>
    #endif
  ], [
    void **syms = NULL;
    int res, nsyms = 0;
    res = backtrace(syms, nsyms);
  ], [
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_BACKTRACE, 1, [Define if you have backtrace])
  ], [
    AC_MSG_RESULT(no)
  ]
)

AC_MSG_CHECKING([for backtrace_symbols])
AC_TRY_LINK(
  [
    #ifdef HAVE_STDDEF_H
    # include <stddef.h>
    #endif
    #ifdef HAVE_STDLIB_H
    # include <stdlib.h>
    #endif
    #ifdef HAVE_EXECINFO_H
    # include <execinfo.h>
    #endif
    #ifdef HAVE_UCONTEXT_H
    # include <ucontext.h>
    #endif
  ], [
    void **syms = NULL;
    int nsyms = 0;
    char **res;
    res = backtrace_symbols(syms, nsyms);
  ], [
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_BACKTRACE_SYMBOLS, 1, [Define if you have backtrace_symbols])
  ], [
    AC_MSG_RESULT(no)
  ]
)

dnl Run a small test program to see if the host's printf(3) family can
dnl actually handle the %llu format.
AC_MSG_CHECKING([whether printf supports %llu format]);
AC_TRY_RUN(
  [ #include <stdio.h>
    int main(int argc, char *argv[]) {
      return (fprintf(stderr, "%llu\n", (unsigned long long) 1) == 2 ? 0 : 1);
    }
  ],
  AC_MSG_RESULT(yes); AC_DEFINE(HAVE_LLU, 1, [Define if you have %llu support]),
  AC_MSG_RESULT(no); AC_DEFINE(HAVE_LU, 1, [Define if you have %lu support]),
  AC_MSG_RESULT(cross-compiling); AC_DEFINE(HAVE_LU, 1, [Define if you have %lu support])
)

dnl Add the proftpd support library
LIBS="-lsupp $LIBS"

dnl Module handling.
ac_shared_module_dirs=
ac_static_module_dirs=

dnl Remove any modules in the shared module list from the core and static
dnl module lists
for amodule in $ac_shared_modules; do
  module=`echo "$amodule" | sed -e 's/\.la//g'`.o;

  ac_core_modules=`echo "$ac_core_modules" | sed -e "s/$module//g"`;
  ac_build_core_modules=`echo "$ac_build_core_modules" | sed -e "s/modules\/$module//g"`;

  ac_static_modules=`echo "$ac_static_modules" | sed -e "s/$module//g"`;
  ac_build_static_modules=`echo "$ac_build_static_modules" | sed -e "s/modules\/$module//g"`;
done

GLUE_MODULE_OBJS="$ac_core_modules $ac_static_modules"

dnl Check for any duplicates
my_core_modules=`echo "$ac_core_modules" | sed -e 's/\.o//g'`;
my_static_modules=`echo "$ac_static_modules" | sed -e 's/\.o//g'`;
my_shared_modules=`echo "$ac_shared_modules" | sed -e 's/\.la//g'`;
all_modules="$my_core_modules $my_static_modules $my_shared_modules";

pr_use_mysql="no"
pr_use_postgres="no"

AC_MSG_CHECKING([for duplicate module build requests])
for i in $all_modules; do
  once=no;

  dnl Make sure the OpenSSL define is set if mod_tls, mod_sftp, or other
  dnl whitelisted modules are being used
  if test x"$i" = x"mod_auth_otp"; then
    if test x"$pr_use_openssl" = x ; then
      pr_use_openssl=yes
    fi

  elif test x"$i" = x"mod_digest"; then
    if test x"$pr_use_openssl" = x ; then
      pr_use_openssl=yes
    fi

  elif test x"$i" = x"mod_tls"; then
    if test x"$pr_use_openssl" = x ; then
      pr_use_openssl=yes
    fi

  elif test x"$i" = x"mod_sftp"; then
    if test x"$pr_use_openssl" = x ; then
      pr_use_openssl=yes
    fi

    if test x"$pr_use_sodium" = x ; then
      pr_use_sodium=yes
    fi

  elif test x"$i" = x"mod_sql_passwd"; then
    if test x"$pr_use_openssl" = x ; then
      pr_use_openssl=yes
    fi

    if test x"$pr_use_sodium" = x ; then
      pr_use_sodium=yes
    fi
  fi

  for j in $all_modules; do
    if test x"$i" = x"$j"; then
      if test x"$once" = xno; then
        once=yes;

      else
        AC_MSG_RESULT([yes])
        AC_MSG_ERROR([duplicate build request for $j -- aborting])
      fi
    fi
  done

  dnl Use database-specific config scripts, if we can.  Note that
  dnl these will cause problems for cross-compiles!
  if test x"$i" = x"mod_sql_mysql"; then
    pr_use_mysql="yes"

    if test x"$my_config" != xno; then
      if `$my_config --version 2>/dev/null 1>&2`; then
        # mysql_config --include gives path WITH -I prefix
        mysql_includes=`$my_config --include 2>/dev/null`
        if test ! -z "$mysql_includes"; then
          ac_build_addl_includes="$ac_build_addl_includes $mysql_includes"
        fi
 
        # mysql_config --libs gives ALL options; use just the -L ones.
        #
        # Note that due to Bug#3702, we also need to look for -pthread, and
        # if present, add it to the LIBS list of flags.

        mysql_libdirs=`$my_config --libs 2>/dev/null`
        if test ! -z "$mysql_libdirs"; then
          for my_libdir in $mysql_libdirs; do
            l=`echo -n "$my_libdir" | sed -n '/^-L/{p;}'`
            if test ! -z "$l"; then
              ac_build_addl_libdirs="$ac_build_addl_libdirs $my_libdir"
            fi

            if test x"$my_libdir" = x"-pthread"; then
              LIBS="$LIBS -pthread"
            fi
          done
        fi
      fi
    fi

  elif test x"$i" = x"mod_sql_postgres"; then
    pr_use_postgres="yes"

    if test x"$pg_config" != xno; then
      if `$pg_config 2>/dev/null 1>&2`; then
        # pg_config --includedir gives path, no -I prefix
        pg_includes=`$pg_config --includedir 2>/dev/null`
        if test ! -z "$pg_includes"; then
          ac_build_addl_includes="$ac_build_addl_includes -I$pg_includes"
        fi

        # pg_config --libdir gives path, no -L prefix
        pg_libdirs=`$pg_config --libdir 2>/dev/null`
        if test ! -z "pg_libdirs"; then
          ac_build_addl_libdirs="$ac_build_addl_libdirs -L$pg_libdirs"
        fi

        # I suspect that we will also need to look for -pthread here
        # (a la Bug#3702), as a forums post about proftpd+postgres describes
        # the same "Alarm clock" symptom as seen with MySQL; see:
        #
        #  http://forums.proftpd.org/smf/index.php/topic,1424.0.html

        # pg_config --libs gives libs, with -l prefixes
        pg_libs=`$pg_config --libs 2>/dev/null`
        if test ! -z "$pg_libs"; then
          for pg_lib in $pg_lib; do
            if test x"$pg_lib" = x"-pthread"; then
              LIBS="$LIBS -pthread"
            fi
          done
        fi
      fi
    fi
  fi
done
AC_MSG_RESULT([no])

if test x"$pr_use_mysql" = xyes; then
  dnl Check for other MySQL-specific functionality here
  saved_cppflags="$CPPFLAGS"
  saved_ldflags="$LDFLAGS"
  saved_libs="$LIBS"

  dnl Fiddle with CPPFLAGS, LDFLAGS
  CPPFLAGS="$CPPFLAGS $ac_build_addl_includes"
  LDFLAGS="$LDFLAGS $ac_build_addl_libdirs"

  dnl Splice out -lsupp FROM LIBS, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="$LIBS -lm -lmysqlclient -lz"

  AC_MSG_CHECKING([for mysql_get_option])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <mysql.h>
    ],
    [
      (void) mysql_get_option(NULL, 0, NULL);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_MYSQL_GET_OPTION, 1, [Define if you have the mysql_get_option function])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  dnl For Bug#3669, we need to check for make_scrambled_password_323.
  dnl While we're at it, check for other variants as well.

  AC_MSG_CHECKING([for MySQL's make_scrambled_password])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <mysql.h>
    ],
    [
      char output[32];
      char *input = NULL;
      (void) make_scrambled_password(output, input);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_MYSQL_MAKE_SCRAMBLED_PASSWORD, 1, [Define if you have MySQL's make_scrambled_password function])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  AC_MSG_CHECKING([for MySQL's make_scrambled_password_323])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <mysql.h>
    ],
    [
      char output[32];
      char *input = NULL;
      (void) make_scrambled_password_323(output, input);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_MYSQL_MAKE_SCRAMBLED_PASSWORD_323, 1, [Define if you have MySQL's make_scrambled_password_323 function])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  # For Bug#3669 in the RedHat case, we need to check for
  # my_make_scrambled_password.
  AC_MSG_CHECKING([for MySQL's my_make_scrambled_password])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <mysql.h>
    ],
    [
      char output[32];
      char *input = NULL;
      size_t inputlen = 0;
      (void) my_make_scrambled_password(output, input, inputlen);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_MYSQL_MY_MAKE_SCRAMBLED_PASSWORD, 1, [Define if you have MySQL's my_make_scrambled_password function])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  AC_MSG_CHECKING([for MySQL's my_make_scrambled_password_323])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <mysql.h>
    ],
    [
      char output[32];
      char *input = NULL;
      size_t inputlen = 0;
      (void) my_make_scrambled_password_323(output, input, inputlen);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_MYSQL_MY_MAKE_SCRAMBLED_PASSWORD_323, 1, [Define if you have MySQL's my_make_scrambled_password_323 function])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  AC_MSG_CHECKING([for MySQL's mysql_ssl_set])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <mysql.h>
    ],
    [
      MYSQL *mysql = NULL;
      (void) mysql_ssl_set(mysql, NULL, NULL, NULL, NULL, NULL);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_MYSQL_MYSQL_SSL_SET, 1, [Define if you have MySQL's mysql_ssl_set function])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  AC_MSG_CHECKING([for MySQL's mysql_get_ssl_cipher])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <mysql.h>
    ],
    [
      MYSQL *mysql = NULL;
      (void) mysql_get_ssl_cipher(mysql);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_MYSQL_MYSQL_GET_SSL_CIPHER, 1, [Define if you have MySQL's mysql_get_ssl_cipher function])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  dnl Restore CPPFLAGS, LDFLAGS
  CPPFLAGS="$saved_cppflags"
  LDFLAGS="$saved_ldflags"
  LIBS="$saved_libs"
fi

if test x"$pr_use_openssl" = xyes; then
  AC_DEFINE(PR_USE_OPENSSL, 1, [Define if using OpenSSL support.])
  ac_build_addl_libs="$ac_build_addl_libs -lssl -lcrypto"

  AC_MSG_CHECKING([whether OpenSSL is compiled with FIPS support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="$LIBS -lcrypto"

  AC_TRY_RUN(
    [
      #include <openssl/crypto.h>
      #include <openssl/fips.h>
      int main(int argc, char *argv[]) {
      # ifdef OPENSSL_FIPS
        return 0;
      # else
        return 1;
      #endif
      }
    ],
    AC_MSG_RESULT(yes); AC_DEFINE(PR_USE_OPENSSL_FIPS, 1, [Define if your OpenSSL supports FIPS]),
    AC_MSG_RESULT(no),
    AC_MSG_RESULT(unknown)
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether linking with OpenSSL functions succeeds])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto $LIBS"

  AC_TRY_LINK(
    [
      #include <openssl/evp.h>
    ],
    [
      SSLeay_add_all_algorithms();
    ],
    [
      AC_MSG_RESULT(yes)
      LIBS="$saved_libs"
    ],
    [
      AC_MSG_RESULT(no)

      AC_MSG_CHECKING([whether linking with OpenSSL functions requires -ldl])
      LIBS="-lcrypto -ldl $LIBS"
      AC_TRY_LINK(
        [
          #include <openssl/evp.h>
        ],
        [
          SSLeay_add_all_algorithms();
        ],
        [
          AC_MSG_RESULT(yes)
          LIBS="$saved_libs -ldl"
        ],
        [
          AC_MSG_RESULT(no)
          LIBS="$saved_libs"
        ]
      ) 

      AC_MSG_CHECKING([whether linking with OpenSSL functions requires -lz])
      saved_libs="$LIBS"

      dnl Splice out -lsupp, since that library hasn't been built yet
      LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
      LIBS="-lcrypto -lz $LIBS"

      AC_TRY_LINK(
        [
          #include <openssl/evp.h>
          #include <openssl/bio.h>
          #include <openssl/comp.h>
        ],
        [
          BIO *bio;
          SSLeay_add_all_algorithms();
          bio = BIO_new(BIO_f_zlib());
        ],
        [
          AC_MSG_RESULT(yes)
          LIBS="$saved_libs -lz"
        ],
        [
          AC_MSG_RESULT(no)
          LIBS="$saved_libs"
        ]
      )
    ]
  )

  AC_MSG_CHECKING([whether OpenSSL has complete ECC support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto $LIBS"

  AC_TRY_LINK(
    [
      #include <openssl/ec.h>
      #include <openssl/ecdh.h>
      #include <openssl/ecdsa.h>
      #include <openssl/evp.h>
      #include <openssl/objects.h>
      #include <openssl/opensslv.h>
      #if OPENSSL_VERSION_NUMBER < 0x0090807f /* 0.9.8g */
      # error "OpenSSL < 0.9.8g has unreliable ECC code"
      #endif
    ],
    [
      EC_KEY *e = EC_KEY_new_by_curve_name(NID_secp521r1);
      const EVP_MD *m = EVP_sha512();
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_ECC, 1, [Define if your OpenSSL supports ECC])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has EVP_CipherInit_ex support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto $LIBS"

  AC_TRY_LINK(
    [
      #include <openssl/evp.h>
    ],
    [
      (void) EVP_CipherInit_ex(NULL, NULL, NULL, NULL, NULL, 1);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_EVP_CIPHERINIT_EX, 1, [Define if your OpenSSL supports EVP_CipherInit_ex])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has ALPN support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto -lssl $LIBS"

  AC_TRY_LINK(
    [
      #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #include <openssl/ssl.h>
    ],
    [
      SSL_CTX *ctx = NULL;
      SSL_CTX_set_alpn_select_cb(ctx, NULL, NULL);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_ALPN, 1, [Define if your OpenSSL supports ALPN])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has NPN support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto -lssl $LIBS"

  AC_TRY_LINK(
    [
      #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #include <openssl/ssl.h>
    ],
    [
      SSL_CTX *ctx = NULL;
      SSL_CTX_set_next_protos_advertised_cb(ctx, NULL, NULL);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_NPN, 1, [Define if your OpenSSL supports NPN])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has OCSP support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto -lssl $LIBS"

  AC_TRY_LINK(
    [
      #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #include <openssl/ssl.h>
      #include <openssl/ocsp.h>
    ],
    [
      SSL_CTX *ctx = NULL;
      SSL_CTX_set_tlsext_status_cb(ctx, NULL);
      SSL_CTX_set_tlsext_status_arg(ctx, NULL);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_OCSP, 1, [Define if your OpenSSL supports OCSP])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has SSL num tickets support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto -lssl $LIBS"

  AC_TRY_LINK(
    [
      #include <openssl/ssl.h>
    ],
    [
      (void) SSL_set_num_tickets(NULL, 0);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_SSL_NUM_TICKETS, 1, [Define if your OpenSSL supports SSL num tickets])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has SSL read ahead support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto -lssl $LIBS"

  AC_TRY_LINK(
    [
      #include <openssl/ssl.h>
    ],
    [
      (void) SSL_set_read_ahead(NULL, 1);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_SSL_READ_AHEAD, 1, [Define if your OpenSSL supports SSL read ahead])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has SSL session ID context set support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto -lssl $LIBS"

  AC_TRY_LINK(
    [
      #include <openssl/ssl.h>
    ],
    [
      (void) SSL_SESSION_set1_id_context(NULL, NULL, 0);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_SSL_SESSION_SET1_ID_CONTEXT, 1, [Define if your OpenSSL supports setting SSL session ID context])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  AC_MSG_CHECKING([whether OpenSSL has SSL session ticket callback support])
  saved_libs="$LIBS"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="-lcrypto -lssl $LIBS"

  AC_TRY_LINK(
    [
      #include <openssl/ssl.h>
    ],
    [

      (void) SSL_CTX_set_session_ticket_cb(NULL, NULL, NULL, NULL);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(PR_USE_OPENSSL_SSL_SESSION_TICKET_CALLBACK, 1, [Define if your OpenSSL supports SSL session ticket callbacks])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )
  LIBS="$saved_libs"

  pr_use_pthread_for_openssl="no"
  if test x"$openssl_cmdline" != xno; then
    if `$openssl_cmdline version 2>/dev/null 1>&2`; then
      openssl_cflags=`$openssl_cmdline version -f 2>/dev/null`
      if test ! -z "$openssl_cflags"; then
        # Look for the -pthread flag, indicating that this OpenSSL was built
        # with threads support (see Bug#3795)
        for openssl_cflag in $openssl_cflags; do 
          if test x"$openssl_cflag" = x"-pthread"; then
            pr_use_pthread_for_openssl="yes"
          fi
        done

        if test x"$pr_use_pthread_for_openssl" = xno ; then
          # If we're on FreeBSD, AND if OpenSSL is being used, AND if
          # openssl version -f shows no flags, then ASSUME that we do need
          # the -pthread flag, to avoid regressions of Bug#3795.
          if test `echo $ostype | grep -c FREEBSD` != "0" ; then
            pr_use_pthread_for_openssl="yes"
          fi
        fi
      fi
    fi
  else
    # If we're on FreeBSD, AND if OpenSSL is being used, then ASSUME that we
    # do need the -pthread flag, to avoid regressions of Bug#3795.
    if test `echo $ostype | grep -c DFREEBSD` != "0" ; then
      pr_use_pthread_for_openssl="yes"
    fi
  fi

  if test x"$pr_use_pthread_for_openssl" = xyes ; then
    LIBS="$LIBS -pthread"
  fi
fi

if test x"$pr_use_postgres" = xyes; then
  # Check for other Postgres-specific functionality here
  saved_ldflags="$LDFLAGS"
  saved_libs="$LIBS"
  saved_cppflags="$CPPFLAGS"

  # fiddle with CPPFLAGS, LDFLAGS
  CPPFLAGS="$CPPFLAGS $ac_build_addl_includes"
  LDFLAGS="$LDFLAGS $ac_build_addl_libdirs"

  dnl Splice out -lsupp, since that library hasn't been built yet
  LIBS=`echo "$LIBS" | sed -e 's/-lsupp//g'`;
  LIBS="$LIBS -lm -lpq"

  AC_MSG_CHECKING([for Postgres's PQescapeStringConn])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <libpq-fe.h>
    ],
    [
      char *input = NULL, *output = NULL;
      size_t inputlen = 0;
      PGconn *postgres = NULL;
      int pgerr = 0;
      PQescapeStringConn(postgres, output, input, inputlen, &pgerr);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_POSTGRES_PQESCAPESTRINGCONN, 1, [Define if you have Postgres's PQescapeStringConn])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  AC_MSG_CHECKING([for Postgres's PQgetssl])
  AC_TRY_LINK(
    [ #ifdef HAVE_STDDEF_H
      # include <stddef.h>
      #endif
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <libpq-fe.h>
    ],
    [
      const PGconn *pg = NULL;
      (void) PQgetssl(pg);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_POSTGRES_PQGETSSL, 1, [Define if you have Postgres's PQgetssl])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  # restore CPPFLAGS, LDFLAGS
  CPPFLAGS="$saved_cppflags"
  LDFLAGS="$saved_ldflags"

  AC_MSG_CHECKING([for Postgres's PQinitOpenSSL])
  AC_TRY_LINK(
    [
      #ifdef HAVE_STDLIB_H
      # include <stdlib.h>
      #endif
      #ifdef HAVE_SYS_TYPES_H
      # include <sys/types.h>
      #endif
      #include <libpq-fe.h>
    ],
    [
      int init_ssl = 0, init_crypto = 0;
      PQinitOpenSSL(init_ssl, init_crypto);
    ],
    [
      AC_MSG_RESULT(yes)
      AC_DEFINE(HAVE_POSTGRES_PQINITOPENSSL, 1, [Define if you have Postgres's PQinitOpenSSL])
    ],
    [
      AC_MSG_RESULT(no)
    ]
  )

  # restore CPPFLAGS, LDFLAGS
  CPPFLAGS="$saved_cppflags"
  LDFLAGS="$saved_ldflags"
  LIBS="$saved_libs"
fi

if test x"$pr_use_sodium" = xyes; then
  AC_CHECK_HEADER(sodium.h,
    [AC_DEFINE(HAVE_SODIUM_H, 1, [Define if sodium.h is present.])
     AC_DEFINE(PR_USE_SODIUM, 1, [Define if using Sodium support.])
     ac_build_addl_libs="$ac_build_addl_libs -lsodium"
     ac_orig_libs="$ac_orig_libs -lsodium"
     SHARED_MODULE_LIBS="$SHARED_MODULE_LIBS -lsodium"
    ])
fi

for module in $ac_shared_modules ; do
  moduledir=`echo "$module" | sed -e 's/\.la$//'`;

  if test -f $srcdir/modules/$src -o -f $srcdir/contrib/$src; then
    continue

  elif test -d $srcdir/modules/$moduledir; then
    ac_shared_module_dirs="$ac_shared_module_dirs modules/$moduledir";
    ac_shared_modules=`echo "$ac_shared_modules" | sed -e "s/$module//"`

  elif test -d $srcdir/contrib/$moduledir; then
    ac_shared_module_dirs="$ac_shared_module_dirs contrib/$moduledir";
    ac_shared_modules=`echo "$ac_shared_modules" | sed -e "s/$module//"`
  fi
done

for module in $ac_static_modules ; do
  moduledir=`echo "$module" | sed -e 's/\.o$//'`;

  if test -f $srcdir/modules/$src -o -f $srcdir/contrib/$src; then
    continue

  elif test -d $srcdir/modules/$moduledir; then
    ac_static_module_dirs="$ac_static_module_dirs modules/$moduledir";
    ac_static_modules=`echo "$ac_static_modules" | sed -e "s/$module//"`

  elif test -d $srcdir/contrib/$moduledir; then
    ac_static_module_dirs="$ac_static_module_dirs contrib/$moduledir";
    ac_static_modules=`echo "$ac_static_modules" | sed -e "s/$module//"`

    addonlibs=""
    src=`echo "$module" | sed -e 's/\.o$//'`.c;
    srcinc=`echo "$module" | sed -e 's/\.o$//'`.h;

    if test -f $srcdir/contrib/$moduledir/$src ; then
      srcarch=`sed -n '/.*\$Archive: \(.*\)\$.*/s//\1/p' "$srcdir/contrib/$moduledir/$src"`
      srclib=`sed -n '/.*\$Libraries: \(.*\)\$.*/s//\1/p' "$srcdir/contrib/$moduledir/$src"`
    else
      srcarch=
      srclib=
    fi

    if test -f $srcdir/contrib/$moduledir/$srcinc ; then
      if test -z $srcarch ; then
        incarch=`sed -n '/.*\$Archive: \(.*\)\$.*/s//\1/p' "$srcdir/contrib/$moduledir/$srcinc"`
      else
        incarch=
      fi

      inclib=`sed -n '/.*\$Libraries: \(.*\)\$.*/s//\1/p' "$srcdir/contrib/$moduledir/$srcinc"`
    else
      incarch= 
      inclib= 
    fi

    dnl If the module will be providing an archive (.a file), then remove it
    dnl from the list of static module objects (.o files).
    for thearch in $srcarch $incarch; do
      archive=`echo "$thearch" | sed -e 's/\.a$//'`

      if test x"$archive" != x"$moduledir" ; then
        AC_MSG_ERROR([specified archive '$thearch' does not match expected module archive name '$moduledir.a' -- aborting])
      fi

      ac_static_modules=`echo "$ac_static_modules" | sed -e "s/$module//g"`
      ac_build_static_modules=`echo "$ac_build_static_modules" | sed -e "s/modules\/$module//g"`
      ac_build_static_module_archives="$ac_build_static_module_archives contrib/$moduledir/$thearch"
    done

    dnl Test for duplicate libraries, just in case.
    for thelib in $srclib $inclib; do
      dup="xno"

      for somelib in $ac_addl_libs $LIBS; do
        if test "$thelib" = "$somelib"; then
          dup="xyes"
          break
        fi
      done
	
      if test "$dup" = x"no"; then
        addonlibs="$addonlibs $thelib"
      fi
    done

    test x"$addonlibs" = x || ac_addl_libs="$addonlibs $ac_addl_libs"
  fi
done

dnl Yes, I know that this is not recommended Autoconf practice.  I doubt,
dnl though, that many users will require the use of
dnl `./configure --help=recursive' to see all of the options.
if test ! -z "$ac_shared_module_dirs" ; then
  AC_CONFIG_SUBDIRS($ac_shared_module_dirs)
else
  ac_shared_module_dirs="\"\""
fi

if test ! -z "$ac_static_module_dirs" ; then
  AC_CONFIG_SUBDIRS($ac_static_module_dirs)
else
  ac_static_module_dirs="\"\""
fi

for module in $ac_shared_modules; do
  if test x"$enable_dso" != xyes ; then
    AC_MSG_ERROR([cannot build shared modules without DSO support -- aborting])
  fi

  moduledir=`echo "$module" | sed -e 's/\.la$//'`;
  src=`echo "$module" | sed -e 's/\.la$//'`.c;
  srcinc=`echo "$module" | sed -e 's/\.la$//'`.h;

  if test -f $srcdir/modules/$src -o -f $srcdir/contrib/$src; then
    if test ! -e $srcdir/modules/$src; then
      olddir=`pwd`
      cd $srcdir/modules
      ln -s ../contrib/$src $src
      cd $olddir
    fi

    if test ! -e $srcdir/include/$srcinc -a -f $srcdir/contrib/$srcinc ; then
      olddir=`pwd`
      cd $srcdir/include
      ln -s ../contrib/$srcinc $srcinc
      cd $olddir
    fi

  elif test -d $srcdir/modules/$moduledir -o -d $srcdir/contrib/$moduledir; then
    continue

  else
    AC_MSG_ERROR([source file '$srcdir/modules/$src' cannot be found -- aborting])
  fi
done

for module in $ac_static_modules; do
  addonlibs=""
  moduledir=`echo "$module" | sed -e 's/\.o$//'`;
  src=`echo "$module" | sed -e 's/\.o$//'`.c;
  srcinc=`echo "$module" | sed -e 's/\.o$//'`.h;

  if test -f $srcdir/modules/$src -o -f $srcdir/contrib/$src; then
    if test ! -e $srcdir/modules/$src; then
      olddir=`pwd`
      cd $srcdir/modules
      ln -s ../contrib/$src $src
      cd $olddir
    fi

    if test ! -f $srcdir/include/$srcinc -a -f $srcdir/contrib/$srcinc ; then
      olddir=`pwd`
      cd $srcdir/include
      ln -s ../contrib/$srcinc $srcinc
      cd $olddir
    fi

    srclib=`sed -n '/.*\$Libraries: \(.*\)\$.*/s//\1/p' "$srcdir/modules/$src"`

    if test -f $srcdir/include/$srcinc ; then
      inclib=`sed -n '/.*\$Libraries: \(.*\)\$.*/s//\1/p' "$srcdir/include/$srcinc"`
    else
      inclib= 
    fi

    dnl Test for duplicate libraries, just in case.
    for thelib in $srclib $inclib; do
      dup="xno"

      for somelib in $ac_addl_libs $LIBS; do
        if test "$thelib" = "$somelib"; then
          dup="xyes"
          break
        fi
      done
	
      if test "$dup" = x"no"; then
        addonlibs="$addonlibs $thelib"
      fi
    done

    test x"$addonlibs" = x || ac_addl_libs="$addonlibs $ac_addl_libs"

  elif test -d $srcdir/modules/$moduledir -o -d $srcdir/contrib/$moduledir; then
    continue

  else
    AC_MSG_ERROR([source file '$srcdir/modules/$src' cannot be found -- aborting])
  fi
done

ac_addl_libs=`echo "$ac_addl_libs" | sed -e 's/ *\$//'`
ac_addl_libs=`echo "$ac_addl_libs" | sed -e 's/^ *//'`
test "x$ac_addl_libs" = x || ac_build_addl_libs="$ac_build_addl_libs $ac_addl_libs"
ac_addl_dirs=`echo "$ac_addl_dirs" | sed -e 's/ *\$//'`
ac_addl_dirs=`echo "$ac_addl_dirs" | sed -e 's/^ *//'`
ADDL_DIRS="$ac_addl_dirs"

ADDL_CPPFLAGS="$ac_orig_cppflags"

# Restore the original CPPFLAGS and LDFLAGS settings
CPPFLAGS="$ac_orig_cppflags"
LDFLAGS="$ac_orig_ldflags"

INCLUDES="$ac_build_addl_includes"
LIBDIRS="$ac_build_addl_libdirs"
LIBRARIES="$ac_build_addl_libs"
SHARED_MODULE_DIRS="$ac_shared_module_dirs"
if test ! -z "$ac_shared_modules" ; then
  SHARED_MODULE_OBJS="$ac_shared_modules"
else
  SHARED_MODULE_OBJS="\"\""
fi
STATIC_MODULE_DIRS="$ac_static_module_dirs"
STATIC_MODULE_OBJS="$ac_core_modules $ac_static_modules"
BUILD_SHARED_MODULE_OBJS="$ac_build_shared_modules"
BUILD_STATIC_MODULE_ARCHIVES="$ac_build_static_module_archives"
BUILD_STATIC_MODULE_OBJS="$ac_build_core_modules $ac_build_static_modules"
SHARED_MODULE_LIBS="$ac_orig_libs"

build_version=`cat $srcdir/include/version.h | grep "#define PROFTPD_VERSION_TEXT" | sed -e 's/^.*\"\(.*\)\"/\1/'`
BUILD_VERSION="$build_version"
RELEASE_VERSION=`echo $build_version | sed 's/rc.*$//'`
RC_VERSION=""
if test `echo $build_version | grep -c rc` != "0"; then
  RC_VERSION=`echo $build_version | sed 's/^.*rc/rc/'`
fi

dnl Substitute in our configuration paths as appropriate.  We also have to
dnl sweet talk the environment into helping us get this right, like so...
pr_saved_prefix="$prefix"
pr_saved_exec_prefix="$exec_prefix"

test "x$prefix" = xNONE && prefix=$ac_default_prefix
test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'

AC_DEFINE_UNQUOTED(PR_CONFIG_DIR, "`eval echo "${sysconfdir}"`", [Define the config directory])
AC_DEFINE_UNQUOTED(PR_INCLUDE_DIR, "`eval echo "${includedir}"`", [Define the include directory])
AC_DEFINE_UNQUOTED(PR_LIBEXEC_DIR, "`eval echo "${libexecdir}"`", [Define the libexec directory])

dnl We evaluate the string twice.  Once to go from $datadir to $prefix/share,
dnl and once more to substitute in the $prefix value.  What a pain.
locale_dir="`eval echo ${localedir}`"
locale_dir="`eval echo ${locale_dir}`"
AC_DEFINE_UNQUOTED(PR_LOCALE_DIR, "`eval echo "${locale_dir}"`", [Define the locale directory])

AC_DEFINE_UNQUOTED(PR_RUN_DIR, "`eval echo "${localstatedir}"`", [Define the run directory])
AC_DEFINE_UNQUOTED(PR_CONFIG_FILE_PATH, "`eval echo "${sysconfdir}/proftpd.conf"`", [Define the configuration file path])
AC_DEFINE_UNQUOTED(PR_PID_FILE_PATH, "`eval echo "${localstatedir}/proftpd.pid"`", [Define the PID file path])

prefix="$pr_saved_prefix"
exec_prefix="$pr_saved_exec_prefix"

if test x"$pkgconfigdir" = xNONE ; then
  pkgconfigdir="${libdir}/pkgconfig"
else
  pkgconfigdir="${prefix}/$pkgconfigdir"
fi
AC_SUBST(pkgconfigdir)

if test x"$devel" = xyes ; then
  AC_DEFINE(PR_USE_DEVEL, 1, [Define if enabling developer support])

  dnl Remove optimization flags from CFLAGS
  CFLAGS=`echo "$CFLAGS" | sed -e 's/\-O2//'`
  CFLAGS="$CFLAGS $pr_devel_cflags"
  LIBS="$LIBS $pr_devel_libs"

  if test x"$GCC" = xyes; then

    dnl Some C compilers (e.g. older gcc versions) may not accept these
    dnl options.  Check if they are supported.  They will be added to
    dnl CFLAGS if supported.
    PR_CHECK_CC_OPT(Wcomment)
    PR_CHECK_CC_OPT(Wdangling-else)
    PR_CHECK_CC_OPT(Wdeclaration-after-statement)
    PR_CHECK_CC_OPT(Wduplicated-branches)
    PR_CHECK_CC_OPT(Wfloat-equal)
    PR_CHECK_CC_OPT(Wformat)
    PR_CHECK_CC_OPT(Wformat-security)
    PR_CHECK_CC_OPT(Wimplicit-function-declaration)
    PR_CHECK_CC_OPT(Wmaybe-uninitialized)
    PR_CHECK_CC_OPT(Wmissing-braces)
    PR_CHECK_CC_OPT(Wparentheses)
    PR_CHECK_CC_OPT(Wpointer-to-int-cast)
    PR_CHECK_CC_OPT(Wreturn-local-addr)
    PR_CHECK_CC_OPT(Wstack-protector)
    PR_CHECK_CC_OPT(Wstrict-overflow)
    PR_CHECK_CC_OPT(Wswitch)
    PR_CHECK_CC_OPT(Wunreachable-code)
    PR_CHECK_CC_OPT(Wunused-variable)
    PR_CHECK_CC_OPT(fstack-protector)
    PR_CHECK_CC_OPT(fstack-protector-all)
    PR_CHECK_CC_OPT(fno-common)
  fi

  dnl Do not strip symbols from developer object files.
  INSTALL_STRIP=""
else

  if test x"$keepsyms" = xyes ; then
    dnl Do not strip symbols from object files.
    INSTALL_STRIP=""

  else 
    dnl Make sure to strip symbols from object files.
    INSTALL_STRIP="-s"
  fi
fi

if test "$OSTYPE" = "-DDARWIN6" -o "$OSTYPE" = "-DDARWIN7" -o "$OSTYPE" = "-DDARWIN8"; then
  dnl Check whether the C compiler accepts -Wno-long-double. This helps to
  dnl quell unnecessary OSX compiler complaints.  Use of this macro should
  dnl be as late in the configure script as possible, for it changes the
  dnl CFLAGS environment variable (which may possible cause other autoconf
  dnl tests to fail).
  PR_CHECK_CC_OPT(Wno-long-double)
fi

my_cflags="\"$CFLAGS\""
AC_DEFINE_UNQUOTED(PR_BUILD_CFLAGS, $my_cflags, [Define the build CFLAGS])
my_ldflags="\"$LDFLAGS $LIBDIRS\""
AC_DEFINE_UNQUOTED(PR_BUILD_LDFLAGS, $my_ldflags, [Define the build LDFLAGS])
my_libs="\"$LIBRARIES $LIBS\""
AC_DEFINE_UNQUOTED(PR_BUILD_LIBS, $my_libs, [Define the build LIBS])

dnl And finally, generate the appropriate Make* and config.h
AC_SUBST(ENABLE_NLS)
AC_SUBST(ENABLE_TESTS)
AC_SUBST(GLUE_MODULE_OBJS)
AC_SUBST(INSTALL_STRIP)
AC_SUBST(INSTALL_DEPS)
AC_SUBST(LIB_DEPS)
AC_SUBST(LIB_OBJS)
AC_SUBST(LIBTOOL_DEPS)
AC_SUBST(LIBLTDL)
AC_SUBST(LTDLINCL)
AC_SUBST(MAIN_LDFLAGS)
AC_SUBST(MAIN_LIBS)
AC_SUBST(MODULE_DEPS)
AC_SUBST(MODULE_LDFLAGS)
AC_SUBST(SHARED_MODULE_DIRS)
AC_SUBST(SHARED_MODULE_LIBS)
AC_SUBST(SHARED_MODULE_OBJS)
AC_SUBST(STATIC_MODULE_DIRS)
AC_SUBST(STATIC_MODULE_OBJS)
AC_SUBST(BUILD_SHARED_MODULE_OBJS)
AC_SUBST(BUILD_STATIC_MODULE_ARCHIVES)
AC_SUBST(BUILD_STATIC_MODULE_OBJS)
AC_SUBST(ADDL_CPPFLAGS)
AC_SUBST(ADDL_DIRS)
AC_SUBST(INCLUDES)
AC_SUBST(LIBDIRS)
AC_SUBST(LIBRARIES)
AC_SUBST(CURSES_LIBS)
AC_SUBST(UTILS_LIBS)
AC_SUBST(BUILD_VERSION)
AC_SUBST(RELEASE_VERSION)
AC_SUBST(RC_VERSION)

AC_CONFIG_HEADER(config.h)

AC_OUTPUT(
  contrib/Makefile
  include/Makefile
  lib/Makefile
  locale/Makefile
  modules/Makefile
  src/Makefile
  src/prxs
  src/ftpdctl.8
  src/proftpd.8
  src/proftpd.conf.5
  src/xferlog.5
  tests/Makefile
  utils/Makefile
  utils/ftpcount.1
  utils/ftpscrub.8
  utils/ftpshut.8
  utils/ftptop.1
  utils/ftpwho.1
  Makefile
  Make.rules,
  [echo timestamp > stamp-h]
)

dnl After everything has been generated, we need to double-check for
dnl header files in the module directories.
builddir=.
for moduledir in $ac_shared_module_dirs $ac_static_module_dirs; do
  srcinc=`echo "$moduledir" | sed -e 's/^.*\///'`.h;

  if test -d $builddir/$moduledir ; then
    if test ! -e $builddir/include/$srcinc ; then
      if test -e $builddir/$moduledir/$srcinc ; then
        olddir=`pwd`
        cd $builddir/include
        ln -s ../$moduledir/$srcinc $srcinc
        cd $olddir
      fi
    fi
  fi
done

# Display a summary of what modules will be compiled
echo
echo "--------------"
echo "Build Summary"
echo "--------------"
if test ! -z "$my_static_modules"; then
  echo "Building the following static modules:"
  for amodule in $my_static_modules; do
   echo "  $amodule"
  done
fi

if test ! -z "$my_shared_modules"; then
  echo
  echo "Building the following shared modules:"
  for amodule in $my_shared_modules; do
    echo "  $amodule"
  done
fi

echo
echo "--------------"
