dnl
dnl  Copyright 2021 Northern.tech AS
dnl
dnl  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
dnl
dnl  This program is free software; you can redistribute it and/or modify it
dnl  under the terms of the GNU General Public License as published by the
dnl  Free Software Foundation; version 3.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
dnl
dnl To the extent this program is licensed as part of the Enterprise
dnl versions of CFEngine, the applicable Commercial Open Source License
dnl (COSL) may apply to this file if you as a licensee so wish it. See
dnl included file COSL.txt.
dnl
dnl ##########################################################################
dnl #
dnl # Build CFEngine
dnl #
dnl # Run ./autogen.sh to build configure script
dnl #
dnl ##########################################################################

AC_PREREQ(2.63)

m4_define([cfversion_from_env],  m4_normalize(m4_esyscmd([echo $EXPLICIT_VERSION])))
m4_define([cfversion_from_file], m4_normalize(m4_esyscmd([cat CFVERSION])))
m4_ifval(cfversion_from_env, [
    m4_define([cfversion], cfversion_from_env)
], [
    m4_ifval(cfversion_from_file, [
        m4_define([cfversion], cfversion_from_file)
    ], [
        m4_fatal([Could not determine CFEngine version;
                  please set EXPLICIT_VERSION in the environment,
                  or make sure all git tags are up to date and rerun autogen.sh])
    ])
])


AC_INIT(cfengine, cfversion)
AC_CONFIG_SRCDIR([libpromises/generic_agent.c])
AC_CANONICAL_TARGET

dnl
dnl This program needs to be checked early, as MAKEINFO variable is expanded in
dnl AM_INIT_AUTOMAKE.
dnl
AC_CHECK_PROG(MAKEINFO, makeinfo, makeinfo)


dnl Parallel unit tests are causing spurious failures across several systems,
dnl particularly those doing process testing.
dnl Unfortunately the option to disable parallel tests (serial-tests) doesn't
dnl exist in automake 1.11 and earlier, so we need to do this complicated logic
dnl to determine whether we can disable it or not. If it doesn't exist as an
dnl option, then serial tests are already the default.
AC_MSG_CHECKING([automake version])
m4_define(AUTOMAKE_VERSION, m4_normalize(m4_esyscmd([automake --version 2>&1 | sed -ne '/^automake/{s/^automake.* \([^ ][^ ]*\)$/\1/; p;}'])))
m4_define(SERIAL_TESTS, m4_bmatch(AUTOMAKE_VERSION, [^1\.\([0-9]\|1[0-1]\)\(\.\|$\)], [], [serial-tests]))
AC_MSG_RESULT(AUTOMAKE_VERSION)


dnl tar-pax rather than tar-ustar so that $(am__tar) emits the posix format the
dnl reproducibility options in Makefile.am ask for, as in masterfiles.
AM_INIT_AUTOMAKE([tar-pax] SERIAL_TESTS)
AM_MAINTAINER_MODE([enable])

m4_divert_text([DEFAULTS], [: "${AR_FLAGS=cr}"])

cfengine_version=cfversion

m4_undefine([cfversion])
m4_undefine([cfversion_from_file])
m4_undefine([cfversion_from_env])

AS_IF([test -n "$RELEASE"],
      [AC_DEFINE_UNQUOTED(RELEASE, ["$RELEASE"], [Release number])],
      [AC_DEFINE(RELEASE, ["1"], [Release number])])

AC_DEFINE(BUILD_YEAR, esyscmd([date +%Y | tr -d '\n']), "Software build year")

AC_DEFINE(CF_WEBSITE, "https://cfengine.com", "Website. To be used in help screens")

AC_DEFINE(CF_COPYRIGHT, "2026 Northern.tech AS", "Copyright. To be used in help screens")

AC_DEFINE_UNQUOTED(ABS_TOP_SRCDIR,
"`cd -- "$srcdir"; pwd`",
[Absolute path of source tree])

m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])

AC_CONFIG_HEADERS([config.h])

dnl Libtool madness

AC_CONFIG_MACRO_DIR([m4])

dnl
dnl hide [s]include macros, so old aclocal (automake < 1.10) won't find them and
dnl won't complain about something/something.m4 not found
dnl
m4_define(incstart,sinc)
m4_define(incend,lude)

dnl
dnl Prevent AC_PROG_CC from defaulting to "-g -O2".
dnl CFLAGS is left for the user; project flags go into CF3_CFLAGS via cf3_gcc_flags.m4.
dnl
: ${CFLAGS=""}
: ${CPPFLAGS=""}

dnl ######################################################################
dnl Checks for programs.
dnl ######################################################################

AC_PROG_CC
AC_PROG_MKDIR_P
AC_EXEEXT

AS_CASE([${target_os}],
[darwin*], [
    AC_CHECK_PROG([BREW], [brew], [brew])
    AC_MSG_CHECKING([for OS X Homebrew])
    AS_IF([test "x$BREW" = "xbrew"], [
            have_brew=yes
            AC_MSG_RESULT([OS X Homebrew detected])
        ],
        [have_brew=])
])

dnl GCC specific flags
m4_include([m4/cf3_gcc_flags.m4])

# Use either new LT_INIT or old AC_DISABLE_STATIC/AC_PROG_LIBTOOL macros
m4_ifdef([LT_INIT],
  [LT_INIT([disable-static])],
  [AC_DISABLE_STATIC
   AC_PROG_LIBTOOL])

AM_PROG_LEX
AC_PROG_YACC
AC_PROG_INSTALL
AC_PATH_PROG([PERL], [perl])
AC_CONFIG_LIBOBJ_DIR(libcfecompat)
CFECOMPAT_CPPFLAGS="-I$srcdir/libcfecompat"
AC_PATH_PROG(GETCONF, getconf, false, $PATH:$prefix/bin:/usr/bin:/usr/local/bin:/sw/bin)
AM_CONDITIONAL(CROSS_COMPILING, test "x$cross_compiling" = "xyes")

# Check whether `pkg-config' is available
AC_ARG_VAR([PKG_CONFIG], [path to pkg-config])
AC_ARG_VAR([PKG_CONFIG_PATH], [directories to add to the pkg-config search path])
AC_ARG_VAR([PKG_CONFIG_LIBDIR], [path overriding pkg-config's search path])

if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
    AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
fi

dnl ######################################################################
dnl Use pthreads if available
dnl ######################################################################

AC_ARG_WITH([pthreads],
    [AS_HELP_STRING([--with-pthreads[[=PATH]]],
                    [Specify path to pthreads, if not the part of operating system])])

if test "x$with_pthreads" != x && test "x$with_pthreads" != "xyes" && test "x$with_pthreads" != "xno"; then
   LIBS="$LIBS -L$with_pthreads/lib"
   CPPFLAGS="-I$with_pthreads/include $CPPFLAGS"
fi

ACX_PTHREAD([],
            [AC_MSG_ERROR(pthread-compatible library is required to build CFEngine)])

CC="$PTHREAD_CC"
CFLAGS="$PTHREAD_CFLAGS $CFLAGS"
LIBS="$PTHREAD_LIBS $LIBS"

dnl ######################################################################
dnl libpromises/evalfunction.c isreadable() uses pthread_cancel so check
dnl if available and reverts to pthread_kill if not.
dnl This check must come after ACX_PTHREAD is called so that CC, CFLAGS,
dnl and LIBS are appropriately set for the local pthreads situation.
dnl ######################################################################
AC_CHECK_FUNCS([pthread_cancel])

dnl ######################################################################
dnl Whether to build extensions as builtin extensions or a separate
dnl plugin. The default is plugin.
dnl --enable-builtin-extensions is the default on Windows, and can be
dnl used on other platforms for debugging purposes, but isn't otherwise
dnl supported.
dnl ######################################################################

AC_ARG_ENABLE([builtin-extensions], [AS_HELP_STRING([Build binaries with builtin extensions])],
              [],
              [
                  AS_CASE([${target_os}], [mingw*], [enable_builtin_extensions=yes], [enable_builtin_extensions=no])
              ])
AM_CONDITIONAL([BUILTIN_EXTENSIONS], [test "x$enable_builtin_extensions" = "xyes"])
AS_IF([test "x$enable_builtin_extensions" = "xyes"], [AC_DEFINE([BUILTIN_EXTENSIONS], [1], [Define if you want builtin Enterprise extensions])])
AC_SUBST([enable_builtin_extensions])

dnl ######################################################################
dnl Configure files layout
dnl ######################################################################

AC_ARG_ENABLE([fhs],
        [AS_HELP_STRING([--enable-fhs], [Enable FHS compliance. Defaults to custom CFEngine files layout])])

#
# pkglibdir/pkgdatadir are not overridable, so use our own invention instead.
#

AS_IF([test x"$enable_fhs" = xyes], [
  projlibdir='${pkglibdir}'
  WORKDIR='${localstatedir}/lib/${PACKAGE}'
  MASTERDIR='default'
  INPUTDIR='default'
  DATADIR='default'
  KEYDIR='default'
  LOGDIR='${localstatedir}/log/${PACKAGE}'
  MODULEDIR='default'
  PIDDIR='${runstatedir:-${localstatedir}/run}/${PACKAGE}'
  STATEDIR='default'
], [
  if test x"$prefix" = xNONE || test x"$prefix" = x/var/cfengine; then
    prefix=/var/cfengine
    case "$target_os" in
      mingw*)
        WORKDIR=$(wine cmd.exe /c "echo %PROGRAMFILES%\\Cfengine" 2>/dev/null |  sed 's/\\/\\\\/g')
        MASTERDIR=default
        INPUTDIR=default
        DATADIR=default
        KEYDIR=default
        LOGDIR=$(wine cmd.exe /c "echo %PROGRAMFILES%\\Cfengine" 2>/dev/null |  sed 's/\\/\\\\/g')
        MODULEDIR=default
        PIDDIR=$(wine cmd.exe /c "echo %PROGRAMFILES%\\Cfengine" 2>/dev/null |  sed 's/\\/\\\\/g')
        STATEDIR=default
      ;;
      *)
        WORKDIR=/var/cfengine
        MASTERDIR=default
        INPUTDIR=default
        DATADIR=default
        KEYDIR=default
        LOGDIR=/var/cfengine
        MODULEDIR=default
        PIDDIR=/var/cfengine
        STATEDIR=default
      ;;
    esac
  else
    WORKDIR="${localstatedir}/${PACKAGE}"
    MASTERDIR="default"
    INPUTDIR="default"
    DATADIR="default"
    KEYDIR="default"
    LOGDIR="${localstatedir}/${PACKAGE}"
    MODULEDIR="default"
    PIDDIR="${localstatedir}/${PACKAGE}"
    STATEDIR="default"
  fi
  bindir="${bindir:-${exec_prefix}/bin}"
  projlibdir='${libdir}'
])

AC_SUBST(projlibdir)

dnl ######################################################################
dnl Platform specific compiler flags.
dnl ######################################################################

AS_CASE([${target_os}],
  [mingw*],
    # Disable printf format warnings, because our wrapper supports more
    # flags than vanilla Windows version, so they are false positives.
    [CFLAGS="$CFLAGS -Wno-format"])

AS_CASE([${target_os}], [mingw*], [AM_CONDITIONAL([WINDOWS], [true])], [AM_CONDITIONAL([WINDOWS], [false])])

dnl ######################################################################
dnl Enable debugging
dnl ######################################################################

AC_ARG_ENABLE(debug,
              AS_HELP_STRING([--enable-debug], [Enable debugging]),
              [debug=$enableval],
              [debug=no])
AM_CONDITIONAL([NDEBUG], [test x"$debug" = x"no"])

AC_MSG_CHECKING([for debug option])
if test x"$debug" = x"yes"
then
    AC_MSG_RESULT(yes)
    DEBUG_CFLAGS="-g3 -O0"
else
    AC_MSG_RESULT(no)
    DEBUG_CFLAGS="-O2 -DNDEBUG"
fi
AC_SUBST([DEBUG_CFLAGS])

dnl ######################################################################
dnl Checks for libraries.
dnl ######################################################################

dnl Now check for database connectors

# TODO remove
AC_ARG_WITH([sql],
    [AS_HELP_STRING([--with-sql[[=PATH]]],
        [Enable SQL database connectors (deprecated, use \
        --with[[out]]-postgresql and --with[[out]]-mysql instead)])
    ],
    [with_postgresql=$with_sql; with_mysql=$with_sql],
    [])

dnl PostgreSQL

AC_ARG_WITH([postgresql],
    [AS_HELP_STRING([--with-postgresql[[=PATH]]],
        [Enable PostgreSQL connector])
    ],
    [],
    [with_postgresql=check])

if test "x$with_postgresql" != "xno"
then
    if test "x$with_postgresql" != xyes && test "x$with_postgresql" != xcheck
    then
        PG_CONFIG=$with_postgresql/bin/pg_config
    else
        PG_CONFIG=pg_config
    fi

    # pg_config is only for native builds
    if test "x$cross_compiling" = "xno"
    then
        if test x`command -v $PG_CONFIG` != x
        then
            pg_include_dir=`$PG_CONFIG --includedir`
            if test -n "$pg_include_dir"
            then
                POSTGRESQL_CPPFLAGS="-I$pg_include_dir"
            fi
        fi
    fi

    CF3_WITH_LIBRARY(postgresql,
        [AC_CHECK_LIB(pq, PQconnectdb,
            [],
            [if test "x$with_postgresql" != xcheck; then
                AC_MSG_ERROR(Cannot find PostgreSQL client library); fi]
         )
         AC_CHECK_HEADERS(libpq-fe.h,
             [],
             [if test "x$with_postgresql" != xcheck; then
                 AC_MSG_ERROR(Cannot find PostgreSQL client library); fi]
         )]
    )
fi

dnl MySQL

AC_ARG_WITH([mysql],
    [AS_HELP_STRING([--with-mysql[[=PATH]]],
        [Enable MySQL connector])
    ],
    [],
    [with_mysql=check])

if test "x$with_mysql" != "xno"
then
    if test "x$with_mysql" != xyes &&
       test "x$with_mysql" != xcheck
    then
        MYSQL_CONFIG=$with_mysql/bin/mysql_config
    else
        MYSQL_CONFIG=mysql_config
    fi

    # mysql_config is only for native builds
    if test "x$cross_compiling" = "xno"
    then
        if test x`command -v $MYSQL_CONFIG` != x
        then
            mysql_include_dir=`$MYSQL_CONFIG --include`
            if test -n "$mysql_include_dir"
            then
                MYSQL_CPPFLAGS="$mysql_include_dir"
            fi
        fi
    fi

    AS_IF(
      [(test "x$with_mysql" = xyes || test "x$with_mysql" = xcheck) \
       && test "x$have_brew" = "xyes" && test -d $(brew --prefix mysql)/],
      [with_mysql=$(brew --prefix mysql)])

    CF3_WITH_LIBRARY(mysql,
        [AC_CHECK_LIB(mysqlclient, mysql_real_connect,
            [],
            [if test "x$with_mysql" != xcheck; then
                AC_MSG_ERROR(Cannot find MySQL client library); fi]
        )
        AC_CHECK_HEADERS(mysql.h,
            [],
            [if test "x$with_mysql" != xcheck; then
                AC_MSG_ERROR(Cannot find MySQL clientlibrary); fi]
        )
        AC_CHECK_LIB(mysqlclient, EVP_CIPHER_CTX_init,
            [AC_MSG_ERROR([MySQL client library exports symbols clashing \
                with OpenSSL. Get the update from distribution provider, \
                recompile MySQL library or disable MySQL connector. See \
                http://bugs.mysql.com/bug.php?id=65055 for details.])
            ]
        )]
    )
fi

dnl
dnl In-process databases
dnl

dnl QDBM

AC_ARG_WITH([qdbm],
    [AS_HELP_STRING([--with-qdbm[[=PATH]]], [use QDBM to store runtime data])])
AS_IF([test -n "$with_qdbm" && test "x$with_qdbm" != "xno"], [WITH_QDBM=1], [WITH_QDBM=0])

if test $WITH_QDBM = 1; then
  CF3_WITH_LIBRARY(qdbm, [
    AC_CHECK_LIB(qdbm, dpopen, [], [AC_MSG_ERROR(Cannot find QDBM)])
    AC_CHECK_HEADERS(qdbm/depot.h, [], [AC_MSG_ERROR(Cannot find QDBM)])
    AC_DEFINE(QDB, 1, [Define if QDBM is available])
  ])
fi

dnl Tokyo Cabinet

AC_ARG_WITH([tokyocabinet],
    [AS_HELP_STRING([--with-tokyocabinet[[=PATH]]], [use Tokyo Cabinet to store runtime data])])

AS_IF([test $WITH_QDBM -eq 0 && test -n "$with_tokyocabinet" && test "x$with_tokyocabinet" != "xno"], [WITH_TOKYO=1], [WITH_TOKYO=0])

if test $WITH_TOKYO = 1; then
  CF3_WITH_LIBRARY(tokyocabinet, [
    AC_CHECK_LIB(tokyocabinet, tchdbnew, [], [AC_MSG_ERROR(Cannot find Tokyo Cabinet)])
    AC_CHECK_HEADERS(tcutil.h, [], [AC_MSG_ERROR(Cannot find Tokyo Cabinet)])
    AC_CHECK_HEADERS(tchdb.h, [], [AC_MSG_ERROR(Cannot find Tokyo Cabinet)])
    AC_DEFINE(TCDB, 1, [Define if Tokyo Cabinet is available.])
  ])
fi

dnl OpenLDAP Lightning MDB

AC_ARG_WITH([lmdb],
    [AS_HELP_STRING([--with-lmdb[[=PATH]]], [use Lightning MDB to store runtime data])])

AS_IF([test $WITH_TOKYO -eq 0 && test $WITH_QDBM -eq 0 && (! test -n "$with_lmdb" || test "x$with_lmdb" != "xno")], [WITH_LMDB=1], [WITH_LMDB=0])

if test $WITH_LMDB = 1; then
  AS_IF(
    [(test "x$with_lmdb" = xyes || test "x$with_lmdb" = xcheck || test "x$with_lmdb" = x) \
     && test "x$have_brew" = "xyes" && test -d $(brew --prefix lmdb)/],
    [with_lmdb=$(brew --prefix lmdb)])

  CF3_WITH_LIBRARY(lmdb, [
    AC_CHECK_LIB(lmdb, mdb_dbi_open, [], [AC_MSG_ERROR(Cannot find Lightning MDB)])
    AC_CHECK_HEADERS(lmdb.h, [], [AC_MSG_ERROR(Cannot find Lightning MDB)])
    AC_DEFINE(LMDB, 1, [Define if Lightning MDB is available])
  ])
fi

if test $WITH_QDBM -eq 0 && test $WITH_TOKYO -eq 0 && test $WITH_LMDB -eq 0; then
   AC_MSG_ERROR(Either Tokyo Cabinet, LMDB, or QDBM must be selected to compile CFEngine)
fi

dnl
dnl OpenSSL
dnl

AC_ARG_WITH(openssl,
    [AS_HELP_STRING([--with-openssl[[=PATH]]],
    [Specify OpenSSL path])], [], [with_openssl=yes])

if test x"$with_openssl" = xno ; then
    AC_MSG_ERROR([This release of CFEngine requires OpenSSL >= 0.9.7])
fi

AS_IF(
  [(test "x$with_openssl" = xyes || test "x$with_openssl" = xcheck) \
   && test "x$have_brew" = "xyes" && test -d $(brew --prefix openssl)/],
  [with_openssl=$(brew --prefix openssl)])

CF3_WITH_LIBRARY(openssl, [
   AC_CHECK_LIB(crypto, RSA_generate_key_ex, [], [])
   AC_CHECK_LIB(ssl, SSL_free, [], [])
   AC_CHECK_DECLS([SSL_CTX_clear_options], [], [], [[#include <openssl/ssl.h>]])
   AC_CHECK_HEADERS([openssl/opensslv.h], [], [AC_MSG_ERROR(Cannot find OpenSSL)])

   AC_MSG_CHECKING(for OpenSSL version)

   AC_PREPROC_IFELSE([AC_LANG_SOURCE([[
   #include <openssl/opensslv.h>

   #if OPENSSL_VERSION_NUMBER < 0x1000000fL
   #This OpenSSL is too old
   #endif
   ]])],[AC_MSG_RESULT(OK)],[AC_MSG_ERROR(This release of CFEngine requires OpenSSL >= 1.0.0)])

   if test "x$ac_cv_lib_crypto_RSA_generate_key_ex" = "xno" ; then
      AC_MSG_ERROR(Cannot find OpenSSL)
   fi

   AC_DEFINE([OPENSSL_SUPPRESS_DEPRECATED], [1], [Suppress deprecation warnings from OpenSSL 3])

   AC_CHECK_DECL([SSL_OP_NO_TLSv1_1],
    [AC_DEFINE([HAVE_TLS_1_1], [1], [Define if TLS 1.1 is supported by OpenSSL])],
    [], [[#include <openssl/ssl.h>]]
    )
   AC_CHECK_DECL([SSL_OP_NO_TLSv1_2],
    [AC_DEFINE([HAVE_TLS_1_2], [1], [Define if TLS 1.2 is supported by OpenSSL])],
    [], [[#include <openssl/ssl.h>]]
    )
   AC_CHECK_DECL([SSL_OP_NO_TLSv1_3],
    [AC_DEFINE([HAVE_TLS_1_3], [1], [Define if TLS 1.3 is supported by OpenSSL])],
    [], [[#include <openssl/ssl.h>]]
    )
])

dnl PCRE2

AC_ARG_WITH([pcre2], [AS_HELP_STRING([--with-pcre2[[=PATH]]], [Specify PCRE2 path])], [], [with_pcre2=yes])

if test "x$with_pcre2" = "xno"; then
   AC_MSG_ERROR([PCRE2 is required])
fi

AS_IF(
  [(test "x$with_pcre2" = xyes || test "x$with_pcre2" = xcheck) \
    && test "x$have_brew" = "xyes" && test -d $(brew --prefix pcre2)/],
  [with_pcre2=$(brew --prefix pcre2)])

CF3_WITH_LIBRARY(pcre2, [
  AC_CHECK_LIB(pcre2-8, pcre2_compile_8, [], [AC_MSG_ERROR(Cannot find PCRE2)])
  AC_CHECK_HEADERS(
    [pcre2.h],
    [],
    AC_MSG_ERROR(Cannot find PCRE2),
    [#define PCRE2_CODE_UNIT_WIDTH 8]
  )]
)

dnl librsync

AC_ARG_WITH([librsync], [AS_HELP_STRING([--with-librsync[[=PATH]]], [Specify librsync path])], [], [with_librsync=yes])

if test "x$with_librsync" = "xno"; then
   AC_MSG_ERROR([librsync is required])
fi

AS_IF(
  [(test "x$with_librsync" = xyes || test "x$with_librsync" = xcheck) \
   && test "x$have_brew" = "xyes" && test -d $(brew --prefix librsync)/],
  [with_librsync=$(brew --prefix librsync)])

CF3_WITH_LIBRARY(librsync, [
    AC_CHECK_HEADERS([librsync.h], [], AC_MSG_ERROR(Cannot find librsync))
    AC_CHECK_LIB(rsync, rs_job_iter, [], [AC_MSG_ERROR(Cannot find librsync)])
  ]
)

dnl defined for libntech

AC_DEFINE(WITH_PCRE2, 1, [Define if PCRE2 is being used])


dnl libvirt

AC_ARG_WITH([libvirt],
    [AS_HELP_STRING([--with-libvirt[[=PATH]]], [support virtual machine management])],
    [], [with_libvirt=check])

if test "x$with_libvirt" != xno
then
     AS_IF(
       [(test "x$with_libvirt" = xyes || test "x$with_libvirt" = xcheck) \
        && test "x$have_brew" = "xyes" && test -d $(brew --prefix libvirt)/],
       [with_libvirt=$(brew --prefix libvirt)])
   CF3_WITH_LIBRARY(libvirt, [
      AC_CHECK_LIB(virt, virConnectOpen, [], [if test "x$with_libvirt" != xcheck; then AC_MSG_ERROR(Cannot find libvirt library); fi])
      AC_CHECK_HEADERS(libvirt/libvirt.h, [], [if test "x$with_libvirt" != xcheck; then AC_MSG_ERROR(Cannot find libvirt library headers); fi])
   ])
fi

dnl  BIG FAT WARNING FOR AC_CHECK_LIB: *always* leave the
dnl  "action-if-yes" parameter empty else LIBS is not changed and C
dnl  define is not generated!  Or use AC_SEARCH_LIBS for a better
dnl  interface.

dnl libacl
dnl It is enough for any of the header files [acl.h sys/acl.h acl/libacl.h] to be present

AC_ARG_WITH([libacl],
    [AS_HELP_STRING([--with-libacl[[=PATH]]], [Specify libacl path])], [], [with_libacl=check])

if test "x$with_libacl" != xno
then
  CF3_WITH_LIBRARY(libacl, [
    AC_CHECK_LIB(acl, acl_init,
      [],
      [if test "x$with_libacl" != xcheck; then AC_MSG_ERROR(Cannot find libacl library); fi])
    AC_CHECK_HEADERS([acl.h sys/acl.h acl/libacl.h],
      [libacl_header_found=yes])
    if test "x$libacl_header_found" != "xyes" && test "x$with_libacl" != xcheck;
    then
      AC_MSG_ERROR(Cannot find libacl library headers);
    fi
  ])
fi

dnl libcurl

AC_ARG_WITH([libcurl],
    [AS_HELP_STRING([--with-libcurl[[=PATH]]], [Specify libcurl path])], [], [with_libcurl=check])

if test "x$with_libcurl" != xno
then
  CF3_WITH_LIBRARY(libcurl, [
    AC_CHECK_LIB(curl, curl_global_init,
      [],
      [  libcurl_lib_found=no;
         if test "x$with_libcurl" != xcheck;
         then
             AC_MSG_ERROR(Cannot find libcurl library);
         fi
      ])
    AC_CHECK_HEADERS([curl/curl.h],
      [libcurl_header_found=yes],
      [if test "x$with_libcurl" != xcheck; then AC_MSG_ERROR(Cannot find libcurl header files); fi])
  ])
fi

AM_CONDITIONAL(HAVE_LIBCURL,
        [test "x$libcurl_lib_found" != xno && test "x$libcurl_header_found" = xyes])

dnl libyaml

AC_ARG_WITH([libyaml],
    [AS_HELP_STRING([--with-libyaml[[=PATH]]], [Specify libyaml path])], [], [with_libyaml=check])

if test "x$with_libyaml" != xno
then
  AS_IF(
    [(test "x$with_libyaml" = xyes || test "x$with_libyaml" = xcheck) \
     && test "x$have_brew" = "xyes" && test -d $(brew --prefix libyaml)/],
    [with_libyaml=$(brew --prefix libyaml)])

  CF3_WITH_LIBRARY(libyaml, [
    AC_CHECK_LIB(yaml, yaml_parser_initialize,
      [],
      [if test "x$with_libyaml" != xcheck; then AC_MSG_ERROR(Cannot find libyaml library); fi])
    AC_CHECK_HEADERS(yaml.h,
      [libyaml_header_found=yes],
      [if test "x$with_libyaml" != xcheck; then AC_MSG_ERROR(Cannot find libyaml header files); fi])
  ])
fi

dnl libxml2

AC_ARG_WITH([libxml2],
    [AS_HELP_STRING([--with-libxml2[[=PATH]]],
        [Specify libxml2 path])
    ],
    [],
    [with_libxml2=check])

have_libxml2="no"

if test "x$with_libxml2" != "xno"; then
    if test "x$have_brew" = "xyes" && \
        test -d $(brew --prefix libxml2)/; then
            with_libxml2=$(brew --prefix libxml2)
            echo "OS X Homebrew detected"
            echo "Defaulting to: --with-libxml2=$with_libxml2"
            have_libxml2="yes"
    elif test -n "$PKG_CONFIG"; then
        AC_MSG_CHECKING([for libxml2 via pkg-config])
        if `$PKG_CONFIG --exists libxml-2.0`; then
            LIBXML2_CFLAGS=`$PKG_CONFIG --cflags libxml-2.0`
            LIBXML2_CPPFLAGS="$LIBXML2_CFLAGS"
            LIBXML2_LIBS=`$PKG_CONFIG --libs libxml-2.0`
            LIBXML2_VERSION=`$PKG_CONFIG --modversion libxml-2.0`
            AC_MSG_RESULT([found version $LIBXML2_VERSION])
            have_libxml2="yes"
        else
            AC_MSG_RESULT([not found])
        fi
    fi

    if test "x$have_libxml2" = "xno"; then
        if test "x$with_libxml2" != "xyes" &&
           test "x$with_libxml2" != "xcheck" &&
           test -x "$with_libxml2/bin/xml2-config"
        then
            XML2_CONFIG=$with_libxml2/bin/xml2-config
        else
            AC_PATH_PROG([XML2_CONFIG], [xml2-config])
        fi

        # xml2-config is only for native builds
        if test "x$cross_compiling" = "xno" && test -n "$XML2_CONFIG"; then
            xml2_include_dir=`$XML2_CONFIG --cflags`
            if test -n "$xml2_include_dir"; then
                LIBXML2_CPPFLAGS="$xml2_include_dir"
            fi
        else # xml2-config not found
            # if a path, e.g. /var/cfengine was given, then we
            # must take into account that libxml2 includes are in
            # /var/cfengine/include/libxml2
            LIBXML2_CPPFLAGS=-I$with_libxml2/include/libxml2
        fi
    fi

    CF3_WITH_LIBRARY(libxml2,
        [AC_CHECK_LIB(xml2, xmlFirstElementChild,
        [],
        [if test "x$with_libxml2" != xcheck; then
            AC_MSG_ERROR(Cannot find libxml2); fi]
        )
        AC_CHECK_HEADERS([libxml/xmlwriter.h], [break],
            [if test "x$with_libxml2" != xcheck; then
                AC_MSG_ERROR(Cannot find libxml2); fi]
        )]
    )

fi

AM_CONDITIONAL([HAVE_LIBXML2],
    [test "x$with_libxml2" != xno &&
     test "x$ac_cv_lib_xml2_xmlFirstElementChild" = xyes])

dnl avahi

AC_ARG_WITH([avahi],
    [AS_HELP_STRING([--with-avahi@<:@=yes/no/check@:>@], [Compile with Avahi support @<:@default=no@:>@])],
    [],
    [with_avahi=no])

if test "x$with_avahi" != xno
then
    AC_CHECK_HEADERS([avahi-client/client.h],
        [AM_CONDITIONAL(HAVE_AVAHI_CLIENT, true)],
        [AM_CONDITIONAL(HAVE_AVAHI_CLIENT, false)
         test "x$with_avahi" != "xcheck" && AC_MSG_ERROR(Cannot find avahi-client/client.h)])
    AC_CHECK_HEADERS([avahi-common/address.h],
        [AM_CONDITIONAL(HAVE_AVAHI_COMMON, true)],
        [AM_CONDITIONAL(HAVE_AVAHI_COMMON, false)
         test "x$with_avahi" != "xcheck" && AC_MSG_ERROR(Cannot find avahi-common/address.h)])
else
    AM_CONDITIONAL(HAVE_AVAHI_CLIENT, false)
    AM_CONDITIONAL(HAVE_AVAHI_COMMON, false)
fi

dnl

dnl ######################################################################
dnl Checks for header files.
dnl ######################################################################

AC_CHECK_HEADERS(unistd.h stdlib.h sys/loadavg.h)
AC_CHECK_HEADERS(sys/param.h sys/resource.h)

# sys/param.h is required for sys/mount.h on OpenBSD
AC_CHECK_HEADERS(sys/mount.h, [], [], [AC_INCLUDES_DEFAULT
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
])

# Required on BSD to get struct sockaddr_dl (for retrieving MAC addresses from getifaddrs())
AC_CHECK_HEADERS(net/if_dl.h)

# Required on Solaris to get struct arpreq (for retrieving MAC addresses)
AC_CHECK_HEADERS(net/if_arp.h, , , [AC_INCLUDES_DEFAULT
#include <sys/socket.h>
])

AC_CHECK_HEADERS(getopt.h, [system_getopt_h=1], [system_getopt_h=0])
AM_CONDITIONAL([NO_SYSTEM_GETOPT_H], [test x$system_getopt_h = x0])
AC_CHECK_HEADERS(utime.h)
AC_CHECK_HEADERS(time.h)
AC_CHECK_HEADERS(sys/time.h)
AC_CHECK_HEADERS(malloc.h sys/malloc.h)
AC_CHECK_HEADERS(vfs.h)
AC_CHECK_HEADERS(sys/vfs.h)
AC_CHECK_HEADERS(sys/sockio.h)
AC_CHECK_HEADERS(sys/statvfs.h)
AC_CHECK_HEADERS(sys/statfs.h)
AC_CHECK_HEADERS(fcntl.h)
AC_CHECK_HEADERS(sys/filesys.h)
AC_CHECK_HEADERS(dustat.h)
AC_CHECK_HEADERS(sys/systeminfo.h)
AC_CHECK_HEADERS(ieeefp.h)
AC_CHECK_HEADERS(winsock2.h)
AC_CHECK_HEADERS(ws2tcpip.h)
AC_CHECK_HEADERS(zone.h)
AC_CHECK_HEADERS(sys/uio.h)
AC_CHECK_HEADERS_ONCE([sys/sysmacros.h]) dnl glibc deprecated inclusion in sys/type.h
AC_CHECK_HEADERS(sys/types.h)
AC_CHECK_HEADERS(sys/mpctl.h) dnl For HP-UX $(sys.cpus) - Mantis #1069
AC_CHECK_HEADERS(shadow.h)
AC_CHECK_HEADERS(sys/jail.h, [], [], [AC_INCLUDES_DEFAULT
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
])
AC_CHECK_HEADERS(net/route.h netinet/in.h netinet/ip.h)


AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_SYS_WAIT
AC_HEADER_DIRENT
AC_HEADER_STDBOOL

dnl ######################################################################
dnl Checks for data types
dnl ######################################################################

AC_TYPE_MODE_T
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_TYPE_PID_T
AC_CHECK_TYPES(clockid_t, [], [], [[
#ifdef HAVE_TIME_H
# include <time.h>
#endif
]])

AC_CHECK_TYPES(socklen_t, [], [], [[
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#include <sys/socket.h>
]])

AC_CHECK_SIZEOF(time_t)
if test "x$ac_cv_sizeof_time_t" = "x8"; then
	AC_DEFINE(HAVE_64BIT_TIME_T, 1, [Define if time_t is 64-bit])
fi

dnl ######################################################################
dnl Checks for typedefs, structures, and compiler characteristics.
dnl ######################################################################

AC_C_CONST

AC_SYS_LARGEFILE
AC_TYPE_OFF_T

#
# AC_SYS_LARGEFILE correctly figures out necessary macros for large files, but
# on AIX there is a gotcha:
#
# Code generated by flex #includes <stdio.h> at the beginning of the file, which
# picks up 32-bit wide off_t. Then it #includes <config.h> which provides LFS
# macros, and finally it includes another system header, now with 64-bit wide
# off_t, which causes a conflict.
#
if test "x$ac_cv_sys_large_files" = x1; then
  CPPFLAGS="-D_LARGE_FILES=1 $CPPFLAGS"
fi

dnl ######################################################################
dnl Check for libraries
dnl ######################################################################

AC_CHECK_LIB(m, sqrt)
AC_CHECK_LIB(rt, clock_gettime)
AC_CHECK_LIB(dl, dlopen)

dnl ######################################################################
dnl Check for special functions
dnl ######################################################################

AC_CHECK_DECLS(clock_gettime, [], [], [[#include <time.h>]])

AC_CHECK_DECLS(unsetenv)

AC_CHECK_DECLS(strnlen)

AC_CHECK_DECLS(seteuid)

AC_CHECK_DECLS(setlinebuf)

AC_CHECK_DECLS(strlcat)

AC_CHECK_DECLS(strlcpy)

AC_CHECK_DECLS(realpath)

AC_CHECK_DECLS(strdup)

AC_CHECK_DECLS(memrchr)

AC_CHECK_DECLS(round, [], [], [[#include <math.h>]])

AC_CHECK_DECLS(nanosleep)

AC_CHECK_DECLS(memdup)

AC_CHECK_DECLS(memmem)

AC_CHECK_DECLS(srand48)

AC_CHECK_DECLS(drand48)

AC_CHECK_DECLS(strerror)

AC_CHECK_DECLS(strstr)

AC_CHECK_DECLS(strcasestr)

AC_CHECK_DECLS(strcasecmp)

AC_CHECK_DECLS(strncasecmp)

AC_CHECK_DECLS(strsep)

AC_CHECK_DECLS(strsignal)

AC_CHECK_DECLS(gmtime_r, [], [], [[#include <time.h>]])

AC_CHECK_DECLS(getline, [], [], [#define _GNU_SOURCE 1
AC_INCLUDES_DEFAULT])

AC_CHECK_DECLS(strchrnul, [], [], [#define _GNU_SOURCE 1
AC_INCLUDES_DEFAULT])

AC_CHECK_DECLS(localtime_r, [], [], [[#include <time.h>]])

AC_CHECK_DECLS(fgetgrent, [], [], [[#include <grp.h>]])

AC_CHECK_DECLS(isfinite, [], [], [[#include <math.h>]])

AC_CHECK_DECL([SCNd64], [],
  [AC_MSG_ERROR(Cannot find SCNd64 in inttypes.h)],
  [[#include <inttypes.h>]]
  )

AC_CHECK_FUNCS(getpwent setpwent endpwent)

AC_CHECK_FUNCS(fgetspent lckpwdf ulckpwdf)
AC_CHECK_LIB([sec], [fgetspent], [
    AC_DEFINE([HAVE_LIBSEC], 1, [Define if -lsec is available])
    AC_DEFINE([HAVE_FGETSPENT], 1, [Define if fgetspent is available])
    LIBS="-lsec $LIBS"
])

AC_CHECK_DECLS(getloadavg)
AC_FUNC_GETLOADAVG
AC_FUNC_ALLOCA

AC_C_BIGENDIAN
AC_CHECK_HEADERS([endian.h])
AC_CHECK_DECLS(le32toh, [], [], [[#include <endian.h>]])

AC_CHECK_DECLS(closefrom, [], [], [[#include <unistd.h>
                                    #include <stdlib.h>]])

AC_CHECK_HEADERS([sys/pstat.h])
AC_CHECK_FUNCS(pstat_getfile2)

CF3_PATH_ROOT_PROG([CHPASSWD], [chpasswd], [], [/sbin:/usr/sbin:/bin:/usr/bin:$PATH])
AS_IF([test "x$CHPASSWD" != "x"],
      [AC_DEFINE(HAVE_CHPASSWD, 1, [Define if chpasswd tool is present])]
      [AC_DEFINE_UNQUOTED(CHPASSWD, ["$CHPASSWD"], [Path to chpasswd tool])]
     )

dnl AIX has this.
CF3_PATH_ROOT_PROG([PWDADM], [pwdadm], [], [/sbin:/usr/sbin:/bin:/usr/bin:$PATH])
AS_IF([test "x$PWDADM" != "x"],
      [AC_DEFINE(HAVE_PWDADM, 1, [Define if pwdadm tool is present])]
      [AC_DEFINE_UNQUOTED(PWDADM, ["$PWDADM"], [Path to pwdadm tool])]
     )

CF3_PATH_ROOT_PROG([USERADD], [useradd], [], [/sbin:/usr/sbin:/bin:/usr/bin:$PATH])
AS_IF([test "x$USERADD" != "x"],
      [AC_DEFINE(HAVE_USERADD, 1, [Define if useradd tool is present])]
      [AC_DEFINE_UNQUOTED(USERADD, ["$USERADD"], [Path to useradd tool])]
     )

CF3_PATH_ROOT_PROG([USERMOD], [usermod], [], [/sbin:/usr/sbin:/bin:/usr/bin:$PATH])
AS_IF([test "x$USERMOD" != "x"],
      [AC_DEFINE(HAVE_USERMOD, 1, [Define if usermod tool is present])]
      [AC_DEFINE_UNQUOTED(USERMOD, ["$USERMOD"], [Path to usermod tool])]
     )

CF3_PATH_ROOT_PROG([USERDEL], [userdel], [], [/sbin:/usr/sbin:/bin:/usr/bin:$PATH])
AS_IF([test "x$USERDEL" != "x"],
      [AC_DEFINE(HAVE_USERDEL, 1, [Define if userdel tool is present])]
      [AC_DEFINE_UNQUOTED(USERDEL, ["$USERDEL"], [Path to userdel tool])]
     )

AS_IF([test "x$USERADD" != x && \
       test "x$USERMOD" != x && test "x$USERDEL" != x],
      [have_userprogs=yes],
      [have_userprogs=no]
)

dnl FreeBSD
CF3_PATH_ROOT_PROG([CHPASS], [chpass], [], [/usr/bin:$PATH])
AS_IF([test "x$CHPASS" != "x"],
      [AC_DEFINE(HAVE_CHPASS, 1, [Define if chpass tool is present])]
      [AC_DEFINE_UNQUOTED(CHPASS, ["$CHPASS"], [Path to chpass tool])]
     )

CF3_PATH_ROOT_PROG([PW], [pw], [], [/usr/sbin:$PATH])
AS_IF([test "x$PW" != "x"],
      [AC_DEFINE(HAVE_PW, 1, [Define if pw tool is present])]
      [AC_DEFINE_UNQUOTED(PW, ["$PW"], [Path to pw tool])]
     )

AS_IF([test "x$CHPASS" != x && \
       test "x$PW" != x],
      [have_userprogs=yes],
      [have_userprogs=no]
)

AC_ARG_WITH([pam], AS_HELP_STRING([--with-pam], [Compile with PAM support]))
AS_IF([test x$with_pam != xno],
[
  CF3_WITH_LIBRARY(pam, [
    AC_CHECK_LIB(pam, pam_start)
    AC_CHECK_HEADERS([security/pam_appl.h])
  ])
  dnl These platforms must have pam, others can have it, but not required.
  AS_CASE([$target_os],
          [*gnu*|*solaris*|*aix*|*hpux*|*hp-ux*], [
              AS_IF([test "x$ac_cv_lib_pam_pam_start" != "xyes"],
                    [AC_MSG_ERROR(Cannot find PAM library)]
              )
              AS_IF([test "x$ac_cv_header_security_pam_appl_h" != "xyes"],
                    [AC_MSG_ERROR(Cannot find PAM headers)]
          )]
  )
  AS_IF([test "x$ac_cv_lib_pam_pam_start" = "xyes" && \
         test "x$ac_cv_header_security_pam_appl_h" = "xyes"],
        [have_pam=yes],
        [have_pam=no]
  )
  AC_CHECK_FUNCS(fgetpwent fgetgrent)
  AS_IF([test "x$ac_cv_func_fgetpwent" = "xyes" && \
         test "x$ac_cv_func_fgetgrent" = "xyes"],
         [have_fgetpwent_fgetgrent=yes]
         [AC_DEFINE(HAVE_FGETPWENT_FGETGRENT, 1, [Define if fgetpwent and fgetgrent functions are present])],
         [have_fgetpwent_fgetgrent=no]
     )

  AS_CASE(["$target_os"],
     [freebsd*],
     [
        AS_IF([test "x$have_pam" = "xyes" && \
               test "x$have_userprogs" = "xyes"],
              [users_promises_ok=yes],
              [users_promises_ok=no])
     ],
     [
        AS_IF([test "x$have_pam" = "xyes" && \
               test "x$have_userprogs" = "xyes"
               test "x$ac_cv_func_fgetpwent" = "xyes" && \
               test "x$ac_cv_func_fgetgrent" = "xyes"],
              [users_promises_ok=yes],
              [users_promises_ok=no])
     ])
], [
  users_promises_ok=no
])
AM_CONDITIONAL(HAVE_USERS_PROMISE_DEPS, [test "x$users_promises_ok" = "xyes"])

AC_CHECK_DECLS(getnetgrent, [], [], [[#include <netdb.h>]])
AC_CHECK_FUNCS(getnetgrent)

AC_CHECK_DECLS(setnetgrent, [], [], [[#include <netdb.h>]])
AC_CHECK_FUNCS(setnetgrent)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <netdb.h>]],
                                   [[extern int setnetgrent(const char *)]])],
  [AC_DEFINE([SETNETGRENT_RETURNS_INT], 1, [Whether setnetgrent returns int])],
  [AC_DEFINE([SETNETGRENT_RETURNS_INT], 0, [Whether setnetgrent returns int])])


AC_CHECK_DECLS(endnetgrent, [], [], [[#include <netdb.h>]])
AC_CHECK_FUNCS(endnetgrent)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <netdb.h>]],
                                   [[extern int endnetgrent(const char *)]])],
  [AC_DEFINE([ENDNETGRENT_RETURNS_INT], 1, [Whether endnetgrent returns int])],
  [AC_DEFINE([ENDNETGRENT_RETURNS_INT], 0, [Whether endnetgrent returns int])])


AC_CHECK_FUNCS(sendto)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
                                     #include <sys/socket.h>]],
                                   [[extern ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)]])],
    [AC_DEFINE([SENDTO_RETURNS_SSIZE_T], 1, [Whether sendto returns ssize_t])],
    [AC_DEFINE([SENDTO_RETURNS_SSIZE_T], 0, [Whether sendto does not returns ssize_t])])

CF3_CHECK_PROPER_FUNC([ctime],
  [],
  [[#error ctime(3) may produce different results on different OSes. Let's have our POSIX-compliant implementation all the time]],
  [#define ctime rpl_ctime])

dnl Check whether mkdir accepts two parameters
CF3_CHECK_PROPER_FUNC([mkdir],
  [[#include <sys/stat.h>
    #include <sys/types.h>]],
  [[int mkdir(const char *pathname, mode_t mode);]],
  [#define mkdir rpl_mkdir])

dnl Check for Win32 stat. We don't know to detect improper stat during
dnl compilation, so we resort to OS type detection.

CF3_CHECK_PROPER_FUNC([stat],
  [[#include <sys/types.h>
    #include <sys/stat.h>]],
  [[#if defined(__MINGW32__)
    #error stat in Windows CRT ill-behaves
    #endif]],
  [])

dnl Check for Win32 rename. We don't know how to detect improper rename (not
dnl removing target file if it exists) during cross-compilation, so we resort to
dnl OS type detection.

CF3_CHECK_PROPER_FUNC([rename],
  [],
  [[#if defined(__MINGW32__)
    #error rename in Windows CRT ill-behaves
    #endif]],
  [#define rename rpl_rename])

AC_CHECK_DECLS(mkdtemp)

AC_CHECK_DECLS(strrstr)

AC_CHECK_DECLS(stpncpy)

AC_CHECK_FUNCS(seteuid setegid setreuid setregid)
AC_CHECK_FUNCS(uname gethostname chflags)
AC_CHECK_FUNCS(mkfifo statfs statvfs door)
AC_CHECK_FUNCS(sysinfo setsid sysconf)
AC_CHECK_FUNCS(getzoneid getzonenamebyid)
AC_CHECK_FUNCS(fpathconf)

AC_CHECK_MEMBERS([struct stat.st_mtim, struct stat.st_mtimespec])
AC_CHECK_MEMBERS([struct stat.st_blocks])

AC_MSG_CHECKING([for PRIuMAX/PRIdMAX macros])
AC_EGREP_CPP([primacros_found],
  AC_INCLUDES_DEFAULT
  [#include <inttypes.h>
  #if defined(PRIuMAX) && defined(PRIdMAX)
    primacros_found
  #endif
  ],
  [AC_MSG_RESULT(yes)],
  [AC_MSG_RESULT(no)
   AC_MSG_RESULT(Unable to find out how to scan intmax_t/uintmax_t types)])

HW_FUNC_VSNPRINTF
HW_FUNC_SNPRINTF
HW_FUNC_VASPRINTF
HW_FUNC_ASPRINTF

dnl dirfd might be a function or a macro

AC_CHECK_DECLS(dirfd, [], [], [AC_INCLUDES_DEFAULT
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#endif
])

AC_CHECK_FUNCS(dirfd, [], [
  AC_MSG_CHECKING([for dirfd macro])
  AC_EGREP_CPP([dirfd_found],
    AC_INCLUDES_DEFAULT
    [#ifdef HAVE_DIRENT_H
    # include <dirent.h>
    #endif
    #ifdef dirfd
      dirfd_found
    #endif
    ],
  [AC_MSG_RESULT(yes)
  DIRFD_MACRO_FOUND=1],
  [AC_MSG_RESULT(no)])
  ])

AC_CHECK_FUNCS(jail_get)

dnl
dnl Various functions
dnl

AC_SEARCH_LIBS(setsockopt, [socket ws2_32])
AC_SEARCH_LIBS(gethostent, nsl)

AC_CHECK_FUNCS(socket)
AC_CHECK_FUNCS(setsockopt)
AC_CHECK_FUNCS(gethostent)

AC_CHECK_TYPES(struct sockaddr_storage, [], [], [[
#if HAVE_WINSOCK2_H
    #include <winsock2.h>
#endif
#if HAVE_WS2TCPIP_H
    #include <ws2tcpip.h>
#else
    #include <sys/socket.h>
    #include <netdb.h>
#endif]])

AC_CHECK_DECLS(getaddrinfo, [], [], [[
#if HAVE_WINSOCK2_H
    #include <winsock2.h>
#endif
#if HAVE_WS2TCPIP_H
    #include <ws2tcpip.h>
#else
    #include <sys/socket.h>
    #include <netdb.h>
#endif
]])

AC_CHECK_DECLS([[inet_ntop], [inet_pton]], [], [], [[
#if HAVE_WINSOCK2_H
    #include <winsock2.h>
#endif
#if HAVE_WS2TCPIP_H
    #include <ws2tcpip.h>
#else
    #include <arpa/inet.h>
#endif
]])

AC_CHECK_FUNCS(getifaddrs)

AC_CHECK_FUNCS(getprocs64)

AC_CHECK_FUNC(lchown, AC_DEFINE(HAVE_LCHOWN, 1, [Whether to use lchown(3) to change ownerships]))

AC_CHECK_DECLS(pthread_attr_setstacksize, [], [], [[#include <pthread.h>]])

AC_CHECK_DECLS(pthread_sigmask, [], [], [[#include <signal.h>]])

AC_CHECK_DECLS(sched_yield, [], [], [[#include <sched.h>]])
AC_CHECK_FUNCS(sched_yield)

AC_CHECK_DECLS([openat], [], [], [[#define _GNU_SOURCE 1
                                   #include <fcntl.h>]])
AC_CHECK_DECLS([fstatat], [], [], [[#define _GNU_SOURCE 1
                                    #include <sys/stat.h>]])
AC_CHECK_DECLS([fchownat], [], [], [[#define _GNU_SOURCE 1
                                     #include <unistd.h>]])
AC_CHECK_DECLS([fchmodat], [], [], [[#define _GNU_SOURCE 1
                                    #include <sys/stat.h>]])
AC_CHECK_DECLS([readlinkat], [], [], [[#define _GNU_SOURCE 1
                                       #include <unistd.h>]])

AC_CHECK_DECLS([log2], [], [], [[#include <math.h>]])

dnl ######################################################################
dnl Required by cf-upgrade. It cannot be implemented in libcompat because
dnl cf-upgrade does not link to any libraries except libutils and only
dnl statically.
dnl ######################################################################
AC_CHECK_FUNC(fexecve, AC_DEFINE(HAVE_FEXECVE, 1, [Whether to use fexecve(3) to execute a new process]))

dnl ######################################################################
dnl These need declarations here, but will be defined in the
dnl Enterprise Windows code.
dnl ######################################################################

AC_CHECK_DECLS(alarm)
AC_CHECK_DECLS(chmod)
AC_CHECK_DECLS(chown)
AC_CHECK_DECLS(fchmod)
AC_CHECK_FUNCS(fchmod)
AC_CHECK_DECLS(uname)
AC_CHECK_DECLS(getuid)
AC_CHECK_DECLS(getgid)
AC_CHECK_DECLS(lstat)
AC_CHECK_FUNCS(sleep)
AC_CHECK_DECLS(socketpair, [], [], [[#include <sys/socket.h>]])
AC_CHECK_DECLS(fsync)
AC_CHECK_DECLS(glob, [], [], [[#include <glob.h>]])
AC_CHECK_DECLS(gettid, [], [], [[
#define _GNU_SOURCE
#include <unistd.h>]])

dnl ######################################################################
dnl Check for sa_len in struct sockaddr
dnl ######################################################################

AC_CHECK_MEMBERS([struct sockaddr.sa_len], , , [
#include <sys/types.h>
#include <sys/socket.h>])

AC_CHECK_MEMBERS([struct ifreq.ifr_hwaddr],,, [
#include <sys/ioctl.h>
#include <net/if.h>])

dnl BSD uses sys/sysctl.h for CPU counting
AC_CHECK_HEADERS(sys/sysctl.h, [], [], [AC_INCLUDES_DEFAULT
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
])

dnl ######################################################################
dnl Requirements for GetUptimeMinutes() to set $(sys.uptime)
dnl ######################################################################
dnl Linux sysinfo() call
AC_CHECK_MEMBERS([struct sysinfo.uptime], , , [#include <sys/sysinfo.h>])

dnl BSD uses sys/sysctl.h to get time-of-boot
AC_CHECK_HEADERS(sys/sysctl.h, [], [], [AC_INCLUDES_DEFAULT
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
])

dnl Solaris uses kstat.h to get time-of-boot
AC_CHECK_HEADERS(kstat.h)
dnl SystemV way to get uptime, highly portable
AC_CHECK_HEADERS(utmp.h)
dnl POSIX way to get uptime
AC_CHECK_HEADERS(utmpx.h)


dnl ######################################################################
dnl Fancy new Linux syscalls for file/data copying (config for libntech)
dnl ######################################################################
AC_CHECK_HEADERS([linux/fs.h])
AC_CHECK_DECLS([FICLONE], [], [], [#include <linux/fs.h>])
AC_CHECK_DECLS([SEEK_DATA], [], [], [
#define _GNU_SOURCE
#include <unistd.h>
])
AC_CHECK_DECLS([FALLOC_FL_PUNCH_HOLE], [], [], [
#define _GNU_SOURCE
#include <fcntl.h>
])
AC_CHECK_HEADERS([sys/sendfile.h])
AC_CHECK_FUNCS([sendfile])
AC_CHECK_DECL([copy_file_range])


dnl #######################################################################
dnl Newer BSD systems don't have a compatible rtentry - use ortentry
dnl #######################################################################

rtry=none
AC_MSG_CHECKING(for either struct rtentry or struct ortentry)
AC_EGREP_HEADER(rtentry, net/route.h, rtry=rtentry)
if test "$rtry" = rtentry; then
 AC_DEFINE(HAVE_RTENTRY, 1, [Do we have any route entry structure?])
fi
AC_EGREP_HEADER(ortentry, net/route.h, rtry=ortentry)
if test "$rtry" = ortentry; then
 AC_DEFINE(HAVE_ORTENTRY, 1, [The old route entry structure in newer BSDs])
fi
AC_MSG_RESULT([$rtry])

dnl #######################################################################
dnl Enable extended attributes. Used for SELinux and ACLs
dnl #######################################################################

AC_CHECK_FUNCS(llistxattr, [AC_DEFINE(WITH_XATTR, 1, [Define if you have a libc that supports extended attributes])])
AC_CHECK_HEADERS([attr/xattr.h sys/xattr.h])

AC_MSG_CHECKING([whether xattr functions have extra arguments])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <sys/types.h>
                                    #include <sys/xattr.h>],
                                    [(void)llistxattr("", 0, 0, 0);
                                    (void)lgetxattr("", "", 0, 0, 0, 0);
                                    (void)lsetxattr("", "", "", 0, 0, 0);
                                    (void)lremovexattr("", "", 0);])],
                  [AC_DEFINE(WITH_XATTR_EXTRA_ARGS, 1, [Define if your xattr implementation has extra arguments])]
                  [AC_MSG_RESULT([yes])],
                  [AC_MSG_RESULT([no])])

dnl ######################################################################
dnl Give the chance to enable SELINUX
dnl ######################################################################

AC_ARG_ENABLE([selinux], [AS_HELP_STRING([--enable-selinux], [Deprecated. SELinux support is always enabled])])

dnl ######################################################################
dnl Collect all the options
dnl ######################################################################

CORE_CPPFLAGS="$LMDB_CPPFLAGS $TOKYOCABINET_CPPFLAGS $QDBM_CPPFLAGS $PCRE2_CPPFLAGS $OPENSSL_CPPFLAGS $SQLITE3_CPPFLAGS $LIBACL_CPPFLAGS $LIBCURL_CPPFLAGS $LIBRSYNC_CPPFLAGS $LIBYAML_CPPFLAGS $POSTGRESQL_CPPFLAGS $MYSQL_CPPFLAGS $LIBXML2_CPPFLAGS $CFECOMPAT_CPPFLAGS $CPPFLAGS"
CORE_CFLAGS="$LMDB_CFLAGS $TOKYOCABINET_CFLAGS $QDBM_CFLAGS $PCRE2_CFLAGS $OPENSSL_CFLAGS $SQLITE3_CFLAGS $LIBACL_CFLAGS $LIBCURL_CFLAGS $LIBRSYNC_CFLAGS $LIBYAML_CFLAGS $POSTGRESQL_CFLAGS $MYSQL_CFLAGS $LIBXML2_CFLAGS $CF3_CFLAGS $DEBUG_CFLAGS"
CORE_LDFLAGS="$LMDB_LDFLAGS $TOKYOCABINET_LDFLAGS $QDBM_LDFLAGS $PCRE2_LDFLAGS $OPENSSL_LDFLAGS $SQLITE3_LDFLAGS $LIBACL_LDFLAGS $LIBCURL_LDFLAGS $LIBRSYNC_LDFLAGS $LIBYAML_LDFLAGS $POSTGRESQL_LDFLAGS $MYSQL_LDFLAGS $LIBXML2_LDFLAGS $LDFLAGS"
CORE_LIBS="$LMDB_LIBS $TOKYOCABINET_LIBS $QDBM_LIBS $PCRE2_LIBS $OPENSSL_LIBS $SQLITE3_LIBS $LIBACL_LIBS $LIBCURL_LIBS $LIBRSYNC_LIBS $LIBYAML_LIBS $POSTGRESQL_LIBS $MYSQL_LIBS $LIBXML2_LIBS $LIBS"

dnl ######################################################################
dnl Make them available to subprojects.
dnl ######################################################################

AC_SUBST([CORE_CPPFLAGS])
AC_SUBST([CORE_CFLAGS])
AC_SUBST([CORE_LDFLAGS])
AC_SUBST([CORE_LIBS])
AC_CONFIG_FILES([configure_flags.env])

dnl ######################################################################
dnl OS specific stuff
dnl ######################################################################

case "$target_os" in

   solaris2.8|solaris2.9)
        AC_DEFINE(_XOPEN_SOURCE, 500, [UNIX 98])
        AC_DEFINE(__EXTENSIONS__, 1, [Extended UNIX 98 interfaces])
        ;;
   solaris2.10|solaris2.11)
        AC_DEFINE(_XOPEN_SOURCE, 600, [SUSv3])
        AC_DEFINE(__EXTENSIONS__, 1, [Extended UNIX 98 interfaces])
        ;;
   hpux*|hp-ux*)
        dnl pstat* functions may fail with EOVERFLOW without this.
        AC_DEFINE(_PSTAT64, 1, [Enable wide data structures everywhere])
        ;;
   aix*)
        CPPFLAGS="$CPPFLAGS -w"
        ;;
   linux*|*bsd*|*gnu*)
        AC_CHECK_LIB(nss_nis, yp_get_default_domain)
        ;;
   freebsd*|dragonfly*)
     AM_CONDITIONAL([FREEBSD], true)
        ;;
   netbsd*)
        ;;
   unicos*)
        ;;
   cray*)
        ;;
   qnx*)
        ;;
   openbsd*|obsd*)
        ;;
   sysv4.2MP|unix_sv*)
        ;;
   cygwin*)
        ;;
   mingw*)
        ;;
   sco*)
        ;;
   darwin*)
        ;;
   *)
        AC_MSG_ERROR(Unknown system type $target_os)
        ;;
esac

m4_include([m4/cf3_platforms.m4])

dnl #####################################################################
dnl Configure directories
dnl #####################################################################

AC_ARG_WITH(workdir,
    [  --with-workdir=WORKDIR  default for internal (trusted) working directory ],
    [
        if test "x$withval" != x ; then
            WORKDIR="$withval"
            LOGDIR="$withval"
            PIDDIR="$withval"
            MODULEDIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in WORKDIR

adl_RECURSIVE_EVAL("${WORKDIR}", WORKDIR)

AC_DEFINE_UNQUOTED(WORKDIR, "${WORKDIR}", [Workdir location])
AC_SUBST(workdir, "${WORKDIR}")

AC_ARG_WITH(masterdir,
    [  --with-masterdir=MASTERDIR  default for internal masterfiles directory ],
    [
        if test "x$withval" != x ; then
            MASTERDIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in MASTERDIR

adl_RECURSIVE_EVAL("${MASTERDIR}", MASTERDIR)

AC_DEFINE_UNQUOTED(MASTERDIR, "${MASTERDIR}", [Masterfiles directory location])
AC_SUBST(masterdir, "${MASTERDIR}")

AC_ARG_WITH(inputdir,
    [  --with-inputdir=INPUTDIR  default for internal inputs directory ],
    [
        if test "x$withval" != x ; then
            INPUTDIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in INPUTDIR

adl_RECURSIVE_EVAL("${INPUTDIR}", INPUTDIR)

AC_DEFINE_UNQUOTED(INPUTDIR, "${INPUTDIR}", [Inputs directory location])
AC_SUBST(inputdir, "${INPUTDIR}")

AC_ARG_WITH(datadir,
    [  --with-datadir=DATADIR  default for internal data directory ],
    [
        if test "x$withval" != x ; then
            DATADIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in DATADIR

adl_RECURSIVE_EVAL("${DATADIR}", DATADIR)

dnl There's a conflict on mingw where they have a type called DATADIR!
AC_DEFINE_UNQUOTED(CF_DATADIR, "${DATADIR}", [Datadir location])
AC_SUBST(datadir, "${DATADIR}")

AC_ARG_WITH(keydir,
    [  --with-keydir=KEYDIR  default for internal key directory ],
    [
        if test "x$withval" != x ; then
            KEYDIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in KEYDIR

adl_RECURSIVE_EVAL("${KEYDIR}", KEYDIR)

AC_DEFINE_UNQUOTED(KEYDIR, "${KEYDIR}", [Keydir location])
AC_SUBST(keydir, "${KEYDIR}")

AC_ARG_WITH(logdir,
    [  --with-logdir=LOGDIR  default for internal log directory ],
    [
        if test "x$withval" != x ; then
            LOGDIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in LOGDIR

adl_RECURSIVE_EVAL("${LOGDIR}", LOGDIR)

AC_DEFINE_UNQUOTED(LOGDIR, "${LOGDIR}", [Logdir location])
AC_SUBST(logdir, "${LOGDIR}")

AC_ARG_WITH(piddir,
    [  --with-piddir=LOGDIR  default for internal pid directory ],
    [
        if test "x$withval" != x ; then
            PIDDIR="$withval"
        fi
    ],
)

AC_ARG_WITH(moduledir,
    [  --with-moduledir=MODULEDIR  default for internal module directory ],
    [
        if test "x$withval" != x ; then
            MODULEDIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in MODULEDIR

adl_RECURSIVE_EVAL("${MODULEDIR}", MODULEDIR)

AC_DEFINE_UNQUOTED(MODULEDIR, "${MODULEDIR}", [Moduledir location])
AC_SUBST(moduledir, "${MODULEDIR}")

dnl Expand ${prefix} and whatnot in PIDDIR

adl_RECURSIVE_EVAL("${PIDDIR}", PIDDIR)

AC_DEFINE_UNQUOTED(PIDDIR, "${PIDDIR}", [piddir location])
AC_SUBST(piddir, "${PIDDIR}")

AC_ARG_WITH(statedir,
    [  --with-statedir=STATEDIR  default for internal state directory ],
    [
        if test "x$withval" != x ; then
            STATEDIR="$withval"
        fi
    ],
)

dnl Expand ${prefix} and whatnot in STATEDIR

adl_RECURSIVE_EVAL("${STATEDIR}", STATEDIR)

AC_DEFINE_UNQUOTED(STATEDIR, "${STATEDIR}", [State directory location])
AC_SUBST(statedir, "${STATEDIR}")

AC_ARG_WITH(shell, [AS_HELP_STRING([--with-shell=PATH],
                   [Specify path to POSIX-compatible shell (if not /bin/sh)])],
                   [],
                   [with_shell=/bin/sh])

dnl Expand ${prefix} and whatnot in bindir
adl_RECURSIVE_EVAL("${bindir}", bindir)

AC_DEFINE_UNQUOTED(BINDIR, "${bindir}", [binaries location])
AC_SUBST(bindir, "${bindir}")


if test "x$with_shell" = "xno"; then
   AC_MSG_ERROR([Please specify full path to POSIX-compatible shell])
fi
AC_DEFINE_UNQUOTED(SHELL_PATH, "$with_shell", [Path to the POSIX-compatible shell])

dnl ######################################################################
dnl init.d script installation
dnl ######################################################################

AC_ARG_WITH(init-script, AS_HELP_STRING([--with-init-script=PATH],
    [Install init.d script in given path. The default is no, but if specified, the default path is platform specific.]),
[
    if test -n "$withval" && test "x$withval" != "xno"; then
        if test "x$withval" = "xyes"; then
            AS_CASE(["$target_os"],
            [aix*],
            [
                INIT_D_PATH=/etc/rc.d/init.d
            ],
            [hpux*],
            [
                INIT_D_PATH=/sbin/init.d
            ],
            [
                INIT_D_PATH=/etc/init.d
            ])
        else
            INIT_D_PATH="$withval"
        fi
        AC_SUBST([INIT_D_PATH])
        AC_CONFIG_FILES([misc/init.d/cfengine3], [chmod +x misc/init.d/cfengine3])
        AC_CONFIG_FILES([misc/init.d/cf-php-fpm], [chmod +x misc/init.d/cf-php-fpm])
    fi
])
AM_CONDITIONAL([WITH_INIT_D_SCRIPT], [test -n "$INIT_D_PATH"])

dnl ######################################################################
dnl systemd unit file installation
dnl ######################################################################

SYSTEMD_SERVICE_PATH=""

AC_ARG_WITH(systemd-service, AS_HELP_STRING([--with-systemd-service=PATH],
    [Install systemd service file in given path. The default is no, but if specified, the default path is /usr/lib/systemd/system.]),
[
    if test -n "$withval" && test "x$withval" != "xno"; then
        if test "x$withval" = "xyes"; then
            SYSTEMD_SERVICE_PATH=/usr/lib/systemd/system
        else
            SYSTEMD_SERVICE_PATH="$withval"
        fi
    fi
]
,
[
    AC_PATH_PROG([systemctl], [systemctl], [no])
    AS_IF([test "x$systemctl" == "xno"],
    [AC_MSG_CHECKING([Not a systemd system])],
    [
        SYSTEMD_SERVICE_PATH=/usr/lib/systemd/system
    ])
])

AS_IF([test "x$SYSTEMD_SERVICE_PATH" = "x"], [], [
    AC_SUBST([SYSTEMD_SERVICE_PATH])
    AC_CONFIG_FILES([misc/systemd/cfengine3.service])
    AC_CONFIG_FILES([misc/systemd/cf-php-fpm.service])
    AC_CONFIG_FILES([misc/systemd/cf-apache.service])
    AC_CONFIG_FILES([misc/systemd/cf-execd.service])
    AC_CONFIG_FILES([misc/systemd/cf-hub.service])
    AC_CONFIG_FILES([misc/systemd/cf-reactor.service])
    AC_CONFIG_FILES([misc/systemd/cf-monitord.service])
    AC_CONFIG_FILES([misc/systemd/cf-postgres.service])
    AC_CONFIG_FILES([misc/systemd/cf-serverd.service])
])

AM_CONDITIONAL([WITH_SYSTEMD_SERVICE], [test -n "$SYSTEMD_SERVICE_PATH"])

AC_ARG_WITH(environment-path, AS_HELP_STRING([--with-environment-path=PATH],
    [Specifies the location of the environment files for the platform. Currently used only by systemd.]))
OS_ENVIRONMENT_PATH=
if test -z "$with_environment_path" || test "$with_environment_path" = "yes"; then
    if test -n "$SYSTEMD_SERVICE_PATH"; then
        for i in /etc/sysconfig /etc/default; do
            if test -d $i; then
                OS_ENVIRONMENT_PATH=$i
                break
            fi
        done
        if test -z "$OS_ENVIRONMENT_PATH"; then
            AC_MSG_ERROR([Unable to detect location of environment files on the platform (e.g. '/etc/sysconfig'). Please specify it using --with-environment-path, or turn off systemd support.])
        fi
    fi
else
    if test "$with_environment_path" = "no"; then
        if test -n "$SYSTEMD_SERVICE_PATH"; then
            AC_MSG_ERROR([It is not possible to both specify systemd support and not provide an environment path with --without-environment-path.])
        fi
    else
        OS_ENVIRONMENT_PATH="$with_environment_path"
    fi
fi
AC_SUBST([OS_ENVIRONMENT_PATH])

dnl systemd-socket activation

AC_ARG_WITH([systemd-socket], [AS_HELP_STRING([--with-systemd-socket[[=PATH]]], [support systemd socket activation])], [], [with_systemd_socket=check])

dnl ########################################################################
dnl systemd socket feature, only available if systemd init scripts requested
dnl ########################################################################
if test "x$with_systemd_service" != xno && test "x$with_systemd_socket" != xno
then
   CF3_WITH_LIBRARY(systemd_socket, [
      AC_CHECK_LIB(systemd, sd_listen_fds, [], [if test "x$with_systemd_socket" != xcheck; then AC_MSG_ERROR(Cannot find systemd library); fi])
      AC_CHECK_LIB(systemd, sd_notify_barrier, [AC_DEFINE([HAVE_SD_NOTIFY_BARRIER],[1],[sd_notify_barrier on recent systemd])])
      AC_CHECK_HEADERS(systemd/sd-daemon.h, [], [if test "x$with_systemd_socket" != xcheck; then AC_MSG_ERROR(Cannot find systemd headers); fi])
   ])
fi

dnl #####################################################################
dnl SELinux policy build and installation
dnl #####################################################################
AC_ARG_WITH(selinux-policy,
            AS_HELP_STRING([--with-selinux-policy],
                           [Whether to build and install SELinux policy (default: no)]),
            [], [with_selinux_policy=no])
AM_CONDITIONAL([WITH_SELINUX], [test "x$with_selinux_policy" != "xno"])

if test "x$with_selinux_policy" != "xno"; then
   platform_id=$(sed -r -e '/PLATFORM_ID/!d;s/PLATFORM_ID="platform:(@<:@^"@:>@+)"/\1/' < /etc/os-release)
   if test -f ${srcdir}/misc/selinux/cfengine-enterprise.te.$platform_id; then
     PLATFORM_SELINUX_POLICIES=cfengine-enterprise.te.$platform_id
     AC_SUBST(PLATFORM_SELINUX_POLICIES)
   fi
fi

dnl #####################################################################
dnl Hostname and Version stuff
dnl #####################################################################

AC_PATH_PROG(HOSTNAME, hostname, "", $PATH)

AC_DEFINE_UNQUOTED(AUTOCONF_HOSTNAME, "`$HOSTNAME`", [Special CFEngine symbol])
AC_DEFINE_UNQUOTED(AUTOCONF_SYSNAME, "$target_os", [Special CFEngine symbol])


dnl #####################################################################
dnl xen cpuid-based hvm detection
dnl #####################################################################

AC_MSG_CHECKING(for Xen cpuid-based HVM detection)
if test x"$GCC" = "xyes"; then
    case $host_cpu in
        i[[3456]]86*|x86_64*|amd64)
            AC_DEFINE(XEN_CPUID_SUPPORT, 1, [Define if XEN cpuid-based HVM detection is available.])
            AC_MSG_RESULT(yes)
            ;;
        *)
            AC_MSG_RESULT(no)
            ;;
    esac
else
    AC_MSG_RESULT(no)
fi

dnl
dnl Code coverage
dnl

AC_ARG_ENABLE(coverage, AS_HELP_STRING([--enable-coverage], [Enable code coverage]),
              [use_coverage=$enableval], [use_coverage=no])

if test "x$use_coverage" = "xyes"; then
  if test "$GCC" != "yes"; then
    AC_MSG_ERROR([GCC is required for --enable-coverage])
  fi

  if test "$debug" != "yes"; then
    AC_MSG_ERROR([--enable-debug is required for --enable-coverage])
  fi

  AC_CHECK_PROG(LCOV, lcov, lcov)
  AC_CHECK_PROG(LCOV_GENHTML, genhtml, genhtml)

  if test -z "$LCOV"; then
    AC_MSG_ERROR([Cannot find lcov from the LTP package])
  fi

  if test -z "$LCOV_GENHTML"; then
    AC_MSG_ERROR([Could not find genhtml from the LTP package])
  fi

  dnl Remove all optimization flags from CFLAGS
  changequote({,})
  CF3_CFLAGS=`echo "$CF3_CFLAGS" | $SED -e 's/-O[0-9]*//g'`
  DEBUG_CFLAGS=""
  changequote([,])

  dnl Add the special gcc flags
  CF3_CFLAGS="$CF3_CFLAGS -fprofile-arcs -ftest-coverage"
  LDFLAGS="$LDFLAGS -lgcov"
  # Need to set ENABLE_COVERAGE so that tests/unit/Makefile.am can adapt for one
  # test which needs gcov stubs if core not built with coverage.
  AM_CONDITIONAL([ENABLE_COVERAGE], true)
else
  AM_CONDITIONAL([ENABLE_COVERAGE], false)
fi

#
# Populate contents of config.post.h
#
AC_SUBST(post_macros)
AM_SUBST_NOTMAKE(post_macros)

dnl ######################################################################
dnl Check how file descriptor transfers are supported between proceses.
dnl ######################################################################
AC_CHECK_MEMBER([struct msghdr.msg_control],
                [AC_DEFINE([HAVE_MSGHDR_MSG_CONTROL], [1], [Define to 1 if SCM_RIGHTS supported])],
                [AC_DEFINE([HAVE_NO_MSGHDR_MSG_CONTROL], [1], [Define to 1 if SCM_RIGHTS support])],
                [[#include <sys/types.h>
                  #include <sys/socket.h>]])

AC_CHECK_MEMBER([struct msghdr.msg_accrights],
                [AC_DEFINE([HAVE_MSGHDR_ACCRIGHTS], [1], [Define to 1 if BSD .msg_accrights supported])],
                [AC_DEFINE([HAVE_NO_MSGHDR_ACCRIGHTS], [1], [Define to 1 if no BSD .msg_accrights support])],
                [[#include <sys/types.h>
                  #include <sys/socket.h>]])

dnl ######################################################################
dnl Summarize
dnl ######################################################################

AC_MSG_RESULT( )

AC_MSG_RESULT(Summary:)
AC_MSG_RESULT(> Version: $cfengine_version)

AC_MSG_RESULT([> Required libraries])

AC_MSG_RESULT([-> OpenSSL: $OPENSSL_PATH])

AC_MSG_RESULT([-> PCRE2: $PCRE2_PATH])

AC_MSG_RESULT([-> Rsync: $LIBRSYNC_PATH])

if test $WITH_TOKYO = 1; then
  AC_MSG_RESULT([-> DB: Tokyo Cabinet: $TOKYOCABINET_PATH])
elif test $WITH_QDBM = 1; then
  AC_MSG_RESULT([-> DB: QDBM: $QDBM_PATH])
fi

AC_MSG_RESULT([> Optional libraries])

if test "x$ac_cv_lib_mysqlclient_mysql_real_connect" = "xyes"; then
  AC_MSG_RESULT([-> MySQL connector: $MYSQL_PATH])
else
  AC_MSG_RESULT([-> MySQL connector: disabled])
fi

if test "x$ac_cv_lib_pq_PQconnectdb" = "xyes"; then
  AC_MSG_RESULT([-> PostgreSQL connector: $POSTGRESQL_PATH])
else
  AC_MSG_RESULT([-> PostgreSQL connector: disabled])
fi

if test $WITH_LMDB = 1; then
  AC_MSG_RESULT([-> DB: Lightning MDB: $LMDB_PATH])
elif test $WITH_TOKYO = 1; then
  AC_MSG_RESULT([-> DB: Tokyo Cabinet: $TOKYOCABINET_PATH])
elif test $WITH_QDBM = 1; then
  AC_MSG_RESULT([-> DB: QDBM: $QDBM_PATH])
fi

if test "x$ac_cv_lib_virt_virConnectOpen" = xyes; then
  AC_MSG_RESULT([-> libvirt: $LIBVIRT_PATH])
else
  AC_MSG_RESULT([-> libvirt: disabled])
fi

if test "x$ac_cv_lib_acl_acl_init" = xyes; then
  AC_MSG_RESULT([-> libacl: $LIBACL_PATH])
else
  AC_MSG_RESULT([-> libacl: disabled])
fi

if test "x$ac_cv_lib_curl_curl_global_init" = xyes; then
  AC_MSG_RESULT([-> libcurl: $LIBCURL_PATH])
else
  AC_MSG_RESULT([-> libcurl: disabled])
fi

if test "x$ac_cv_lib_yaml_yaml_parser_initialize" = xyes; then
  AC_MSG_RESULT([-> libyaml: $LIBYAML_PATH])
else
  AC_MSG_RESULT([-> libyaml: disabled])
fi

if test "x$ac_cv_lib_xml2_xmlFirstElementChild" = xyes; then
  AC_MSG_RESULT([-> libxml2: $LIBXML2_PATH])
else
  AC_MSG_RESULT([-> libxml2: disabled])
fi

if test "x$users_promises_ok" = "xyes"; then
  AC_MSG_RESULT([-> User promises: PAM/user* tools])
else
  AC_MSG_RESULT([-> User promises: disabled])
fi

if test "x$enable_builtin_extensions" = "xyes"; then
  AC_MSG_RESULT([-> Enterprise extensions: Built in])
else
  AC_MSG_RESULT([-> Enterprise extensions: Plugin or not included])
fi

if test -n "$INIT_D_PATH"; then
  AC_MSG_RESULT([-> init.d script: $INIT_D_PATH])
else
  AC_MSG_RESULT([-> init.d script: disabled])
fi

if test -n "$SYSTEMD_SERVICE_PATH"; then
  AC_MSG_RESULT([-> Systemd service: $SYSTEMD_SERVICE_PATH])
else
  AC_MSG_RESULT([-> Systemd service: disabled])
fi

if test -n "$OS_ENVIRONMENT_PATH"; then
  AC_MSG_RESULT([-> Path of platform environment files: $OS_ENVIRONMENT_PATH])
fi

if test "x$with_selinux_policy" != "xno"; then
  AC_MSG_RESULT([-> SELinux policy: enabled])
  AC_MSG_RESULT([-> SELinux platform policies: $PLATFORM_SELINUX_POLICIES])
else
  AC_MSG_RESULT([-> SELinux policy: disabled])
fi

m4_indir(incstart[]incend, [nova/config.m4])

AC_MSG_RESULT([-> Workdir: $WORKDIR])
AC_MSG_RESULT([-> Masterdir: $MASTERDIR])
AC_MSG_RESULT([-> Inputdir: $INPUTDIR])
AC_MSG_RESULT([-> Datadir: $DATADIR])
AC_MSG_RESULT([-> KeyDir: $KEYDIR])
AC_MSG_RESULT([-> Logdir: $LOGDIR])
AC_MSG_RESULT([-> Piddir: $PIDDIR])
AC_MSG_RESULT([-> Statedir: $STATEDIR])
AC_MSG_RESULT([-> bindir: $bindir])
AC_MSG_RESULT([-> Moduledir: $MODULEDIR])

AC_MSG_RESULT( )

dnl ######################################################################
dnl Now make the Makefiles
dnl ######################################################################

AC_CONFIG_FILES([Makefile
    libcfnet/Makefile
    libenv/Makefile
    libpromises/Makefile
    libcfecompat/Makefile
    cf-agent/Makefile
    cf-check/Makefile
    cf-promises/Makefile
    cf-execd/Makefile
    cf-key/Makefile
    cf-monitord/Makefile
    cf-upgrade/Makefile
    cf-runagent/Makefile
    cf-serverd/Makefile
    cf-testd/Makefile
    cf-net/Makefile
    cf-secret/Makefile
    cf-watchd/Makefile
    cf-reactor/Makefile
    config.post.h
    contrib/vagrant-ci/centos-9s-x64/Makefile
    misc/Makefile
    misc/selinux/Makefile
    python/Makefile
    ext/Makefile
    examples/Makefile
    tests/Makefile
    tests/acceptance/Makefile
    tests/acceptance/25_cf-execd/Makefile
    tests/unit/Makefile
    tests/load/Makefile
    tests/static-check/Makefile
    tests/valgrind-check/Makefile])

# Run autoconf/configure in libutils, generating necessary makefiles:
AC_CONFIG_SUBDIRS([libntech])

AC_OUTPUT

AC_MSG_RESULT(DONE: Configuration done. Run make/gmake to build CFEngine Community.)
