dnl ##########################################################################
dnl #
dnl # Process this file with autoconf then autoheader
dnl # to build a configure script
dnl #
dnl ##########################################################################

AC_PREREQ(2.13)

AC_INIT(src/cfengine.c)
AC_CANONICAL_SYSTEM

dnl
dnl The version in the next line is the only one to set
dnl

AM_INIT_AUTOMAKE(cfengine, 1.6.4) dnl remember to set version
AM_CONFIG_HEADER(src/conf.h)

#
# Add to the default list of places in LDFLAGS to compensate for
# ... the configure default value of LIBS on some systems
#
for x in /usr/local/BerkeleyDB/lib /usr/local/gnu/lib \
	/usr/local/gnulib /usr/local/lib /opt/dce/lib
do
  if test -d "$x"; then
    y=`expr " $LDFLAGS " : ".* -L$x "`
    if test $y -eq 0; then
      LDFLAGS="$LDFLAGS -L$x"
    fi
  fi
done

#
# Add to the default list of places in CPPFLAGS to match LDFLAGS above
# 
for x in /usr/local/BerkeleyDB/include \
	/usr/local/include /usr/local/gnu/include /opt/dce/include
do 
  if test -d "$x"; then
    y=`expr " $CPPFLAGS " : ".* -I$x "`
    if test $y -eq 0; then
      CPPFLAGS="$CPPFLAGS -I$x"
    fi
  fi
done


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

AC_PROG_CC

AM_PROG_LEX

AC_PROG_YACC

AC_PROG_RANLIB
AC_PROG_INSTALL

AC_PATH_PROG(LATEX, latex, false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(TEXI2DVI, texi2dvi, false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(DVIPS, dvips, false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(MAKEINFO, makeinfo, false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)


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

AC_CHECK_LIB(m,main)
AC_CHECK_FUNC(door_create,door)
AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt))
AC_CHECK_FUNC(gethostent, , AC_CHECK_LIB(nsl, gethostent))
AC_CHECK_FUNC(yp_get_default_domain, , AC_CHECK_LIB(nsl, yp_get_default_domain))
dnl AC_CHECK_FUNC(db_create, AC_DEFINE(HAVE_DB_CREATE), AC_CHECK_LIB(db, db_create))
AC_CHECK_LIB(db, db_create)

AC_CHECK_LIB(wrap, main)   dnl TCP wrappers library
AC_CHECK_LIB(nsl, main)    dnl Debian -lwrap needs nsl
AC_CHECK_LIB(rx, main)     dnl GNU regex lib if you have regcomp in libc you shouldn't have this
AC_CHECK_LIB(crypto,main)


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

AC_CHECK_HEADERS(unistd.h stdlib.h)
AC_CHECK_HEADERS(sys/mount.h)
AC_CHECK_HEADERS(utime.h)
AC_CHECK_HEADERS(time.h)
AC_CHECK_HEADERS(sys/time.h)
AC_CHECK_HEADERS(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(sys/param.h)
AC_CHECK_HEADERS(fcntl.h)
AC_CHECK_HEADERS(sys/filesys.h)
AC_CHECK_HEADERS(dustat.h)
AC_CHECK_HEADERS(tcpd.h)
AC_CHECK_HEADERS(db.h regex.h rxposix.h)
AC_CHECK_HEADERS(sys/systeminfo.h)
AC_CHECK_HEADERS(sys/acl.h)
AC_CHECK_HEADERS(openssl/des.h)

AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_SYS_WAIT
AC_HEADER_DIRENT


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

AC_TYPE_MODE_T
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_TYPE_PID_T


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

AC_C_CONST

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

AC_CHECK_FUNCS(getcwd getnetgrent waitpid seteuid setegid setreuid setregid)
AC_CHECK_FUNCS(uname gethostname realpath regcomp chflags)
AC_CHECK_FUNCS(strstr putenv shmget)
AC_CHECK_FUNCS(bcopy mkfifo statfs statvfs)
AC_CHECK_FUNCS(sysinfo setsid strdup strrchr strerror)

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, AC_DEFINE(HAVE_RTENTRY) rtry=rtentry)
AC_EGREP_HEADER(ortentry, net/route.h, AC_DEFINE(HAVE_ORTENTRY) rtry=ortentry)
AC_MSG_RESULT($rtry)

dnl #######################################################################
dnl Handle DCE + threading stuff
dnl #######################################################################

AC_CHECK_LIB(pthread,main)         dnl POSIX threads

if test "$ac_cv_lib_pthread_main" = "yes"; then
	AC_CHECK_HEADERS(pthread.h)
	AC_CHECK_HEADERS(sched.h)
fi

AC_CHECK_FUNC(lchown, AC_DEFINE(HAVE_LCHOWN))

AC_CHECK_FUNC(pthread_attr_setstacksize, AC_DEFINE(HAVE_PTHREAD_ATTR_SETSTACKSIZE), AC_CHECK_LIB(pthread, pthread_create))
AC_CHECK_FUNC(pthread_sigmask, AC_DEFINE(HAVE_PTHREAD_SIGMASK), AC_CHECK_LIB(pthread, pthread_create))

AC_ARG_ENABLE(DCE,[  --enable-DCE            support for DCE and ACLs],[
  case "$enableval" in
    no) 
        ;;
    *)
	AC_CHECK_LIB(thread, main) 
	AC_CHECK_LIB(dce, main)            dnl DFS
	AC_CHECK_HEADERS(dce/daclif.h)
	AC_CHECK_HEADERS(pthread.h)
	AC_CHECK_HEADERS(sched.h)
	;;
  esac
])


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

dnl To ensure conf.h is picked up via VPATH
if test "$srcdir" != "."; then
  CPPFLAGS="$CPPFLAGS -I`pwd`/src"
fi

case "$target_os" in

   sunos3*)
	AC_DEFINE(SUN3)
        ;;
   sunos4*)
	AC_DEFINE(SUN4)
        ;;
   solaris2.4*)
	AC_DEFINE(SOLARIS)
        AC_DEFINE(HAVE_SYS_ACL_H,)
        CPPFLAGS="-w $CPPFLAGS"
        LIBS="$LIBS -lelf"
        ;;
   solaris2.5*)
	AC_DEFINE(SOLARIS)
        AC_DEFINE(_POSIX_C_SOURCE)
        AC_DEFINE(__EXTENSIONS__)
        LIBS="$LIBS -lelf -lsec"
        ;;
   solaris2*)
	AC_DEFINE(SOLARIS)
        AC_DEFINE(__BIT_TYPES_DEFINED__) # avoid conflict with db.h
        AC_DEFINE(_POSIX_C_SOURCE)
        AC_DEFINE(__EXTENSIONS__)
        LIBS="$LIBS -lelf -lsec"
        ;;
   ultrix*)
	AC_DEFINE(ULTRIX)
        ;;
   hpux*|hp-ux*) 
	AC_DEFINE(HPuUX)
        if test "$GCC" != "yes"; then
	  AC_DEFINE(REGEX_MALLOC)
        fi
        LIBS="$LIBS -lc -lPW"
        ;;
   aix*)
	AC_DEFINE(AIX)
        CPPFLAGS="$CPPFLAGS -w"
	AC_CHECK_LIB(pthreads, main)
        AC_CHECK_HEADER(pthreads.h)
        ;;
   osf*)
	AC_DEFINE(OSF)
        ;;
   irix6*)
        AC_DEFINE(HAVE_ORTENTRY) # Have to hack this for 6.* owing to bug
	AC_DEFINE(IRIX)
        CFLAGS="$CFLAGS -w"
        ;;
   irix4*)
	AC_DEFINE(IRIX)
        CFLAGS="$CFLAGS -w"
        LIBS="$LIBS -lsun"
        ;;
   irix*)
	AC_DEFINE(IRIX)
        CFLAGS="$CFLAGS -w"
        ;;
   linux*)
	AC_DEFINE(LINUX)
	AC_CHECK_LIB(nss_nis, yp_get_default_domain)
        ;;
   freebsd*)
	AC_DEFINE(FREEBSD)
         CFLAGS="$CFLAGS -pthread"
        AC_CHECK_HEADERS(pthread.h)
        ;;
   netbsd*)
	AC_DEFINE(NETBSD)
        ;;
   newsos*)
	AC_DEFINE(NEWS_OS)
        ;;
   bsd/os*)
	AC_DEFINE(BSDOS)
        ;;
   bsd*)
	AC_DEFINE(BSD43)
        ;;
   aos*)
	AC_DEFINE(AOS)
        ;;
   nextstep*)
	AC_DEFINE(NEXTSTEP)
        ;;

   unicos*)
	AC_DEFINE(CFCRAY)
        ;;

   cray*)
	AC_DEFINE(CFCRAY)
        ;;

   openbsd*|obsd*)
	AC_DEFINE(OPENBSD)
        ;;

   gnu*)
	AC_DEFINE(CFGNU)
        ;;

   sysv4.2MP|unix_sv*)
	AC_DEFINE(UNIXWARE)
        AC_CHECK_LIB(thread,main)         dnl POSIX threads, weird setup
        if test "$ac_cv_lib_thread_main" = "yes"; then
	   AC_CHECK_HEADERS(thread.h)
        fi
        ;;

   cygwin*)
        AC_DEFINE(NT)
        ;;

   sco*)
        AC_DEFINE(SCO)
        ;;

   *)
	AC_MSG_ERROR(Unknown system type $target_os)
        ;;
esac

dns LIBS="$LIBS -L../pub -lpub"

dnl #####################################################################
dnl Configure LOCKDIR and LOGDIR
dnl #####################################################################

AC_ARG_WITH(LOCKDIR,
	[  --with-lockdir=LOCKDIR  directory where lockfiles should go ],
	[
		if test x$withval != x ; then
			LOCKDIR=$withval
		else
			if test -d /var/run ; then
				LOCKDIR=/var/run/cfengine
			else
				LOCKDIR=/etc/cfengine
			fi
		fi
		AC_DEFINE_UNQUOTED(LOCKFILEDIR, "${LOCKDIR}")
	],
	[
		if test -d /var/run ; then
			LOCKDIR=/var/run/cfengine
		else
			LOCKDIR=/etc/cfengine
		fi
		AC_DEFINE_UNQUOTED(LOCKFILEDIR, "${LOCKDIR}")
	]
)

AC_ARG_WITH(LOGDIR,
	[  --with-logdir=LOGDIR  directory where logfiles should go ],
	[
		if test x$withval != x ; then
			LOGDIR=$withval
		else
			if test -d /var/run; then
				LOGDIR=/var/run/cfengine
			else
				LOGDIR=/etc/cfengine
			fi
		fi
		AC_DEFINE_UNQUOTED(LOGFILEDIR, "${LOGDIR}")
	],
	[
		if test -d /var/run; then
			LOGDIR=/var/run/cfengine
		else
			LOGDIR=/etc/cfengine
		fi
		AC_DEFINE_UNQUOTED(LOGFILEDIR, "${LOGDIR}")
	]
)
  

dnl #####################################################################
dnl Fix for lex/flex
dnl #####################################################################

AC_MSG_CHECKING("8-bit support in Flex")

if test "$LEX" = "flex"; then
   EIGHTBIT="\200-\377"
   AC_SUBST(EIGHTBIT)
   AC_MSG_RESULT("8-bit support added")
   NOWRAP="%option noyywrap"
else
   AC_MSG_RESULT("no 8-bit support")
   NOWRAP=""
fi

AC_SUBST(NOWRAP)

LIBS="$LIBS -L../pub -lpub"


dnl #####################################################################
dnl Hostname and Version stuff
dnl #####################################################################
  
AC_PATH_PROG(HOSTNAME, hostname, "", $PATH)

AC_DEFINE_UNQUOTED(AUTOCONF_HOSTNAME, "`$HOSTNAME`")
AC_DEFINE_UNQUOTED(AUTOCONF_SYSNAME, "$target_os")


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

AC_OUTPUT(pub/Makefile doc/Makefile src/Makefile bin/Makefile
	contrib/Makefile inputs/Makefile Makefile doc/cfengine.8
	bin/cfdaily src/cflex.l contrib/cfcron contrib/vicf)

chmod 0755 bin/cfmail bin/cfdaily bin/cfwrap contrib/cfdoc \
	contrib/cfcron contrib/vicf

AC_MSG_WARN("=====================================================")
AC_MSG_WARN(" ")
AC_MSG_WARN(" \* DFS users should use --enable-DCE")
AC_MSG_WARN(" ")
AC_MSG_WARN(" \* If compiling on older pre-POSIX or NT systems")
AC_MSG_WARN("   you might need librx compatibility lib from GNU")
AC_MSG_WARN(" ")
AC_MSG_WARN(" \* This version requires Berkeley db version 3 ")
AC_MSG_WARN("   in /usr/local/BerkeleyDB from www.sleepycat.com.")
AC_MSG_WARN(" ")
AC_MSG_WARN("=====================================================")


AC_MSG_RESULT("Configuration done. Run make/gmake to build cfengine.")

