dnl Process this -*-m4-*- file with autoconf to produce a configure script.

dnl Disable caching
define([AC_CACHE_LOAD],)
define([AC_CACHE_SAVE],)

AC_INIT(base/nagios.c)
AC_CONFIG_HEADER(include/config.h include/snprintf.h include/nagios.h include/cgiutils.h)
AC_PREFIX_DEFAULT(/usr/local/nagios)

PKG_NAME=nagios
PKG_VERSION="2.6"
PKG_HOME_URL="http://www.nagios.org/"
PKG_REL_DATE="11-27-2006"

dnl Figure out how to invoke "install" and what install options to use.
AC_PROG_INSTALL
AC_SUBST(INSTALL)

dnl What OS are we running?
AC_CANONICAL_HOST

dnl Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET
AC_PATH_PROG([STRIP],[strip],[true])

dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(arpa/inet.h ctype.h dirent.h errno.h fcntl.h getopt.h grp.h limits.h math.h netdb.h netinet/in.h pthread.h pthreads.h pwd.h regex.h signal.h socket.h string.h strings.h sys/mman.h sys/types.h sys/time.h sys/resource.h sys/wait.h sys/socket.h sys/stat.h sys/timeb.h sys/un.h sys/ipc.h sys/msg.h sys/poll.h syslog.h uio.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_STRUCT_TM
AC_STRUCT_TIMEZONE
AC_TYPE_MODE_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_SIGNAL
AC_TYPE_GETGROUPS

dnl Checks for library functions.
dnl AC_CHECK_LIB(nsl,main,SOCKETLIBS="$SOCKETLIBS -lnsl")
dnl AC_CHECK_LIB(socket,socket,SOCKETLIBS="$SOCKETLIBS -lsocket")
AC_SUBST(SOCKETLIBS)
AC_CHECK_FUNCS(initgroups setenv strdup strstr strtoul unsetenv)

AC_MSG_CHECKING(for type of socket size)
AC_TRY_COMPILE([#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
],
[int a = send(1, (const void *) 0, (size_t) 0, (int) 0);],
[AC_DEFINE(SOCKET_SIZE_TYPE, size_t) AC_MSG_RESULT(size_t)],
[AC_DEFINE(SOCKET_SIZE_TYPE, int) AC_MSG_RESULT(int)])


dnl Test for pthreads support - taken from ICU FreeBSD Port configure script
THREADLIBS=""
have_pthreads="no"

dnl FreeBSD: Try ports/linuxthreads first - Mammad Zadeh <mammad@yahoo-inc.com>
dnl FreeBSD -pthread check - Jonathan McDowell <noodles@earth.li>
AC_DEFUN(AC_PTHREAD_FREEBSD,[
	AC_CHECK_LIB(lthread,pthread_create,[
		CFLAGS="-D_THREAD_SAFE -I/usr/local/include/pthread/linuxthreads -I/usr/include $CFLAGS"
		THREADLIBS="-L/usr/local/lib -llthread -llgcc_r"
		],[
		AC_MSG_CHECKING([if we need -pthread for threads])
		AC_CACHE_VAL(ac_ldflag_pthread,[
			ac_save_LDFLAGS="$LDFLAGS"
			LDFLAGS="-pthread $LDFLAGS"
			AC_TRY_LINK([
				char pthread_create();
				],
				pthread_create();,
				eval "ac_ldflag_pthread=yes",
				eval "ac_ldflag_pthread=no"
				),
			THREADLIBS="$ac_save_LDFLAGS"
			])
		if eval "test \"`echo $ac_ldflag_pthread`\" = yes"; then
			AC_MSG_RESULT(yes)
		else
			AC_MSG_RESULT(no)
		fi
		],-L/usr/local/lib)
	])

dnl Test for HPUX cma threads first..
AC_CHECK_LIB(cma,pthread_create,THREADLIBS="$THREADLIBS -lpthread")
if test $ac_cv_lib_cma_pthread_create = yes; then
	have_pthreads="yes"
fi

dnl special pthread handling 
dnl AIX uses pthreads instead of pthread, and HP/UX uses cma
dnl FreeBSD users -pthread
AC_CHECK_LIB(pthread,pthread_create,THREADLIBS="$THREADLIBS -lpthread")
if test $ac_cv_lib_pthread_pthread_create = yes; then
	have_pthreads="yes"
else
	dnl For HP 11
	AC_CHECK_LIB(pthread,pthread_mutex_init,THREADLIBS="$THREADLIBS -lpthread")
	if test $ac_cv_lib_pthread_pthread_mutex_init = yes; then
		have_pthreads="yes"
	fi
fi

dnl AIX uses pthreads instead of pthread
if test $have_pthreads = "no"; then
	AC_CHECK_LIB(pthreads,pthread_create,THREADLIBS="$THREADLIBS -lpthreads")
	if test $ac_cv_lib_pthreads_pthread_create = yes; then
		have_pthreads="yes"
	fi
fi

dnl all other thread tests fail, try BSD's -pthread
if test $have_pthreads = "no"; then
	AC_PTHREAD_FREEBSD
fi

AC_SUBST(THREADLIBS)

dnl Solaris needs rt or posix4 libraries for nanosleep()
AC_SEARCH_LIBS(nanosleep,[rt posix4],,[
	echo "Error: nanosleep() needed for timing operations."
	exit 1
	])


AC_ARG_WITH(nagios_user,--with-nagios-user=<user> sets user name to run nagios,nagios_user=$withval,nagios_user=nagios)
AC_ARG_WITH(nagios_group,--with-nagios-group=<grp> sets group name to run nagios,nagios_grp=$withval,nagios_grp=nagios)
AC_SUBST(nagios_user)
AC_SUBST(nagios_grp)
AC_DEFINE_UNQUOTED(DEFAULT_NAGIOS_USER,"$nagios_user")
AC_DEFINE_UNQUOTED(DEFAULT_NAGIOS_GROUP,"$nagios_grp")
INSTALL_OPTS="-o $nagios_user -g $nagios_grp"
AC_SUBST(INSTALL_OPTS)

AC_ARG_WITH(command_user,--with-command-user=<user> sets user name for command access,command_user=$withval,command_user=$nagios_user)
AC_ARG_WITH(command_group,--with-command-group=<grp> sets group name for command access,command_grp=$withval,command_grp=$nagios_grp)
AC_SUBST(command_user)
AC_SUBST(command_grp)
COMMAND_OPTS="-o $command_user -g $command_grp"
AC_SUBST(COMMAND_OPTS)

dnl Check for location of mail program
MAIL_PROG=no
AC_ARG_WITH(mail,--with-mail=<path_to_mail> sets path to equivalent program to mail,MAIL_PROG=$withval,MAIL_PROG=no) 
if test MAIL_PROG=no; then
	AC_PATH_PROG(MAIL_PROG,mail) 
fi 
AC_SUBST(MAIL_PROG)

dnl Check for location of init scripts
init_dir=/etc/rc.d/init.d
if test -d /etc/rc.d/init.d; then
	init_dir="/etc/rc.d/init.d"
elif test -d /usr/local/etc/rc.d; then
	init_dir="/usr/local/etc/rc.d"
elif test -d /etc/rc.d; then
	init_dir="/etc/rc.d"
elif test -d /etc/init.d; then
	init_dir="/etc/init.d"
elif test -d /sbin/init.d; then
	init_dir="/sbin/init.d"
fi

dnl User can override init script location
AC_ARG_WITH(init_dir,--with-init-dir=<path> sets directory to place init script into,init_dir=$withval)
AC_SUBST(init_dir)

echo "Init script directory: " $init_dir

AC_ARG_WITH(lockfile,--with-lockfile=<path> sets path and file name for lock file,lockfile=$withval,lockfile=$localstatedir/nagios.lock)
AC_SUBST(lockfile)



dnl Default xdata routines...
XSDTYPE=default
XCDTYPE=default
XRDTYPE=default
XODTYPE=template
XPDTYPE=default
XDDTYPE=default

XSDCOMMENT=
XCDCOMMENT=
XRDCOMMENT=
XODCOMMENT=
XPDCOMMENT=
XDDCOMMENT=

USE_MYSQL=no
USE_PGSQL=no

dnl Status data
AC_DEFINE_UNQUOTED(USE_XSDDEFAULT)
XSDC="xsddefault.c"
XSDH="xsddefault.h"
XSDCOMMENT="Default (text file)"
echo "We'll use default routines (in xdata/xsddefault.*) for status data I/O..."
AC_SUBST(XSDC)
AC_SUBST(XSDH)


dnl Comment data
AC_DEFINE_UNQUOTED(USE_XCDDEFAULT)
XCDC="xcddefault.c"
XCDH="xcddefault.h"
XCDCOMMENT="Default (text file)"
echo "We'll use default routines (in xdata/xcddefault.*) for comment data I/O..."
AC_SUBST(XCDC)
AC_SUBST(XCDH)


dnl Retention data
AC_DEFINE_UNQUOTED(USE_XRDDEFAULT)
XRDC="xrddefault.c"
XRDH="xrddefault.h"
XRDCOMMENT="Default (text file)"
echo "We'll use default routines (in xdata/xrddefault.*) for retention data I/O..."
AC_SUBST(XRDC)
AC_SUBST(XRDH)


dnl Object data
AC_DEFINE_UNQUOTED(USE_XODTEMPLATE)
XODC="xodtemplate.c"
XODH="xodtemplate.h"
XODCOMMENT="Template-based (text file)"
echo "We'll use template-based routines (in xdata/xodtemplate.*) for object data I/O..."
AC_SUBST(XODC)
AC_SUBST(XODH)



dnl Performance data
AC_DEFINE_UNQUOTED(USE_XPDDEFAULT)
XPDC="xpddefault.c"
XPDH="xpddefault.h"
XPDCOMMENT="Default (external commands)"
echo "We'll use default routines (in xdata/xpddefault.*) for performance data I/O..."
AC_SUBST(XPDC)
AC_SUBST(XPDH)


dnl Downtime data
AC_DEFINE_UNQUOTED(USE_XDDDEFAULT)
XDDC="xdddefault.c"
XDDH="xdddefault.h"
XDDCOMMENT="Default (text file)"
echo "We'll use default routines (in xdata/xdddefault.*) for scheduled downtime data I/O..."
AC_SUBST(XDDC)
AC_SUBST(XDDH)


dnl Option GD library and include paths
AC_ARG_WITH(gd-lib,--with-gd-lib=DIR sets location of the gd library,[
	LDFLAGS="${LDFLAGS} -L${withval}"
	LD_RUN_PATH="${withval}${LD_RUN_PATH:+:}${LD_RUN_PATH}"
	])
AC_ARG_WITH(gd-inc,--with-gd-inc=DIR sets location of the gd include files,[
	CFLAGS="${CFLAGS} -I${withval}"
	])


TRYGD=yep

dnl statusmap CGI enabled by default, unless users chooses not to use it
TRYSTATUSMAP=yep
AC_ARG_ENABLE(statusmap,--disable-statusmap=disables compilation of statusmap CGI,TRYSTATUSMAP=nope)


dnl statuswrl CGI enabled by default, unless users chooses not to use it
TRYSTATUSWRL=yep
AC_ARG_ENABLE(statuswrl,--disable-statuswrl=disables compilation of statuswrl (VRML) CGI,TRYSTATUSWRL=nope)

if test x$TRYSTATUSWRL = xyep; then
	AC_DEFINE_UNQUOTED(USE_STATUSWRL)
	CGIEXTRAS="$CGIEXTRAS statuswrl.cgi"
fi


dnl JMD_CHECK_LIB_ORDER(LIBRARY, FUNCTION, ORDER [, ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND
dnl              [, OTHER-LIBRARIES]]])
	AC_DEFUN(JMD_CHECK_LIB_ORDER,
	[AC_MSG_CHECKING([for $2 in -l$1 (order $3)])
	dnl Use a cache variable name containing both the library and function name,
	dnl because the test really is for library $1 defining function $2, not
	dnl just for library $1.  Separate tests with the same $1 and different $2s
	dnl may have different results.
	ac_lib_var=`echo $1['_']$2['_']$3 | sed 'y%./+-%__p_%'`
	AC_CACHE_VAL(ac_cv_lib_$ac_lib_var,
	[ac_save_LIBS="$LIBS"
	LIBS="-l$1 $6 $LIBS"
	AC_TRY_LINK(dnl
	ifelse([AC_LANG], [FORTRAN77], ,
	ifelse([$2], [main], , dnl Avoid conflicting decl of main.
[/* Override any gcc2 internal prototype to avoid an error.  */
]ifelse([AC_LANG], CPLUSPLUS, [#ifdef __cplusplus
extern "C"
#endif
])dnl
[/* We use char because int might match the return type of a gcc2
    builtin and then its argument prototype would still apply.  */
char $2();
])),
	            [$2()],
	            eval "ac_cv_lib_$ac_lib_var=yes",
	            eval "ac_cv_lib_$ac_lib_var=no")
	LIBS="$ac_save_LIBS"
	])dnl
	if eval "test \"`echo '$ac_cv_lib_'$ac_lib_var`\" = yes"; then
	  AC_MSG_RESULT(yes)
	  ifelse([$4], ,
	[changequote(, )dnl
	  ac_tr_lib=HAVE_LIB`echo $1 | sed -e 's/[^a-zA-Z0-9_]/_/g' \
	    -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'`
	changequote([, ])dnl
	  AC_DEFINE_UNQUOTED($ac_tr_lib)
	  LIBS="-l$1 $LIBS"
	], [$4])
	else
	  AC_MSG_RESULT(no)
	ifelse([$5], , , [$5
	])dnl
	fi
	])



dnl Should we try and detect the GD libs?
if test x$TRYGD = xyep; then

	dnl libiconv is required on some systems - tack it on if found
	AC_CHECK_LIB(iconv,main,ICONV=-liconv,)

	dnl See if the GD lib is available and supports PNG images...

	dnl GD > 1.8.3 requires the TrueType library to be present as well, so test for that first...
	JMD_CHECK_LIB_ORDER(gd,gdImagePng,1,[
			GDLIBFOUND=yep
			GDLIBS="-lgd -lttf -lpng -ljpeg -lz -lm"
			],:,[-lttf -lpng -ljpeg -lz -lm])
 
	dnl GD > 1.8.1 requires the jpeg library to be present as well, so test for that...
	if test x$GDLIBFOUND = x; then
		JMD_CHECK_LIB_ORDER(gd,gdImagePng,2,[
			GDLIBFOUND=yep
			GDLIBS="-lgd $ICONV -lpng -ljpeg -lz -lm"
        		],:,[$ICONV -lpng -ljpeg -lz -lm])
	fi

	dnl If we failed the first test, try without jpeg library
	if test x$GDLIBFOUND = x; then
		JMD_CHECK_LIB_ORDER(gd,gdImagePng,3,[
			GDLIBFOUND=yep
			GDLIBS="-lgd $ICONV -lz -lm -lpng"
		        ],:,[$ICONV -lz -lm -lpng])
	fi

	dnl We failed again, so try a different library ordering (without jpeg libs)
	if test x$GDLIBFOUND = x; then
		JMD_CHECK_LIB_ORDER(gd,gdImagePng,4,[
			GDLIBFOUND=yep
			GDLIBS="-lgd $ICONV -lpng -lz -lm"
	        	],:,[$ICONV -lpng -lz -lm])
	fi

	dnl Did we find the necessary GD libraries?
	if test x$GDLIBFOUND = x; then
		echo ""
		echo ""
		echo "*** GD, PNG, and/or JPEG libraries could not be located... *********"
		echo ""
		echo "Boutell's GD library is required to compile the statusmap, trends"
		echo "and histogram CGIs.  Get it from http://www.boutell.com/gd/, compile"
		echo "it, and use the --with-gd-lib and --with-gd-inc arguments to specify"
		echo "the locations of the GD library and include files."
		echo ""
		echo "NOTE: In addition to the gd-devel library, you'll also need to make"
		echo "      sure you have the png-devel and jpeg-devel libraries installed"
		echo "      on your system."
		echo ""
		echo "NOTE: After you install the necessary libraries on your system:"
		echo "      1. Make sure /etc/ld.so.conf has an entry for the directory in"
		echo "         which the GD, PNG, and JPEG libraries are installed."
		echo "      2. Run 'ldconfig' to update the run-time linker options."
		echo "      3. Run 'make clean' in the Nagios distribution to clean out"
		echo "         any old references to your previous compile."
		echo "      4. Rerun the configure script."
		echo ""
		echo "NOTE: If you can't get the configure script to recognize the GD libs"
		echo "      on your system, get over it and move on to other things.  The"
		echo "      CGIs that use the GD libs are just a small part of the entire"
		echo "      Nagios package.  Get everything else working first and then"
		echo "      revisit the problem.  Make sure to check the nagios-users"
		echo "      mailing list archives for possible solutions to GD library"
		echo "      problems when you resume your troubleshooting."
		echo ""
		echo "********************************************************************"
		echo ""
		echo ""

	dnl We found the GD lib!
	else
		echo "GD library was found!"
		if test x$TRYSTATUSMAP = xyep; then
			AC_DEFINE_UNQUOTED(USE_STATUSMAP)
			CGIEXTRAS="$CGIEXTRAS statusmap.cgi"
		fi

		dnl compile trends CGI
		AC_DEFINE_UNQUOTED(USE_TRENDS)
		CGIEXTRAS="$CGIEXTRAS trends.cgi"
	
		dnl compile histogram CGI
		AC_DEFINE_UNQUOTED(USE_HISTOGRAM)
	        CGIEXTRAS="$CGIEXTRAS histogram.cgi"
	fi
fi


AC_ARG_WITH(cgiurl,--with-cgiurl=<local-url> sets URL for cgi programs (do not use a trailing slash),cgiurl=$withval,cgiurl=/nagios/cgi-bin)
AC_ARG_WITH(htmurl,--with-htmurl=<local-url> sets URL for public html,htmurl=$withval,htmurl=/nagios)
AC_SUBST(htmurl)
AC_SUBST(cgiurl)

AC_ARG_ENABLE(DEBUG0,--enable-DEBUG0 shows function entry and exit,AC_DEFINE_UNQUOTED(DEBUG0))
AC_ARG_ENABLE(DEBUG1,--enable-DEBUG1 shows general info messages,AC_DEFINE_UNQUOTED(DEBUG1))
AC_ARG_ENABLE(DEBUG2,--enable-DEBUG2 shows warning messages,AC_DEFINE_UNQUOTED(DEBUG2))
AC_ARG_ENABLE(DEBUG3,--enable-DEBUG3 shows scheduled events (service and host checks... etc),AC_DEFINE_UNQUOTED(DEBUG3))
AC_ARG_ENABLE(DEBUG4,--enable-DEBUG4 shows service and host notifications,AC_DEFINE_UNQUOTED(DEBUG4))
AC_ARG_ENABLE(DEBUG5,--enable-DEBUG5 shows SQL queries,AC_DEFINE_UNQUOTED(DEBUG5))
AC_ARG_ENABLE(DEBUGALL,--enable-DEBUGALL shows all debugging messages,[
	AC_DEFINE_UNQUOTED(DEBUG0)
	AC_DEFINE_UNQUOTED(DEBUG1)
	AC_DEFINE_UNQUOTED(DEBUG2)
	AC_DEFINE_UNQUOTED(DEBUG3)
	AC_DEFINE_UNQUOTED(DEBUG4)
	AC_DEFINE_UNQUOTED(DEBUG5)
	])


USE_NANOSLEEP=yes
AC_ARG_ENABLE(nanosleep,--enable-nanosleep enables use of nanosleep (instead sleep) in event timing,USE_NANOSLEEP=$enableval,USE_NANOSLEEP=yes)
if test x$USE_NANOSLEEP = xyes; then
	AC_DEFINE_UNQUOTED(USE_NANOSLEEP)
fi

USE_EVENTBROKER=yes
AC_ARG_ENABLE(event-broker,--enable-event-broker enables integration of event broker routines,USE_EVENTBROKER=$enableval,USE_EVENTBROKER=yes)

BROKER_LDFLAGS=""
BROKERLIBS="";
some_dl_found="no";
if test x$USE_EVENTBROKER = xyes; then

	dnl Which loader library should we use? libtdl or dl?
	dnl Hopefully this will be portable and not give us headaches...
	AC_CHECK_HEADER(ltdl.h,[
		AC_CHECK_LIB(ltdl,lt_dlinit,[
			AC_DEFINE(HAVE_LTDL_H)
			some_dl_found="yes"
			BROKERLIBS="$BROKERLIBS -lltdl"
			])
	        ])
	if test "x$some_dl_found" != xyes; then
	AC_CHECK_HEADER(dlfcn.h,[
		AC_CHECK_LIB(dl,dlopen,[
			AC_DEFINE(HAVE_DLFCN_H)
			some_dl_found="yes"
			BROKERLIBS="$BROKERLIBS -ldl"
			])
	        ])
	fi

	dnl - Modified from www.erlang.org
	# Check how to export functions from the broker executable, needed
	# when dynamically loaded drivers are loaded (so that they can find
	# broker functions).
	# OS'es with ELF executables using the GNU linker (Linux and recent *BSD,
	# in rare cases Solaris) typically need '-Wl,-export-dynamic' (i.e. pass
	# -export-dynamic to the linker - also known as -rdynamic and some other
	# variants); some sysVr4 system(s) instead need(s) '-Wl,-Bexport'.
	# AIX 4.x (perhaps only for x>=2) wants -Wl,-bexpall,-brtl and doesn't
	# reliably return an error for others, thus we separate it out.
	# Otherwise we assume that if the linker accepts the flag, it is needed.
	AC_MSG_CHECKING(for extra flags needed to export symbols)
	case $host_os in
		aix4*|aix5*)
			BROKER_LDFLAGS="$BROKER_LDFLAGS -Wl,-bexpall,-brtl"
		;;
		bsdi*)
    			BROKER_LDFLAGS="$BROKER_LDFLAGS -rdynamic" 
	    	;;
		*)
			save_ldflags="$LDFLAGS"
			LDFLAGS=-Wl,-export-dynamic
			AC_TRY_LINK(,,[BROKER_LDFLAGS="$BROKER_LDFLAGS -Wl,-export-dynamic"], [
				LDFLAGS=-Wl,-Bexport
				AC_TRY_LINK(,,[BROKER_LDFLAGS="$BROKER_LDFLAGS -Wl,-Bexport"],
					AC_MSG_RESULT(none))])
			LDFLAGS="$save_ldflags"
		;;
	esac
	AC_SUBST(BROKER_LDFLAGS)
	AC_SUBST(BROKERLIBS)
	test "x$BROKER_LDFLAGS" != x && AC_MSG_RESULT([$BROKER_LDFLAGS])


	dnl - Modified version from www.erlang.org
	dnl - Some 12/15/05 mods made after reading http://xaxxon.slackworks.com/phuku/dl.html
	AC_MSG_CHECKING(for linker flags for loadable modules)
	case $host_os in
		solaris2*|sysv4*)
			MOD_LDFLAGS="-G"
		;;
		aix4*|aix5*)
			#MOD_LDFLAGS="-G -bnoentry -bexpall"
			MOD_LDFLAGS="-G -bM:SRE -bnoentry -bexpall"
		;;
		freebsd2*)
			# Non-ELF GNU linker
			MOD_LDFLAGS="-Bshareable"
		;;
		darwin*)
			# Mach-O linker, a shared lib and a loadable
			# object file is not the same thing.
			MOD_LDFLAGS="-bundle -flat_namespace -undefined suppress"
			MOD_CFLAGS="$MOD_CFLAGS -fno-common"
		;;
		linux*)
			# assume GNU linker and ELF
			MOD_LDFLAGS="-shared"
			MOD_CFLAGS="-fPIC"
		;;
		*)
			# assume GNU linker and ELF
			MOD_LDFLAGS="-shared"
		;;
	esac
	AC_MSG_RESULT([$MOD_LDFLAGS])
	AC_SUBST(MOD_CFLAGS)
	AC_SUBST(MOD_LDFLAGS)


	AC_DEFINE_UNQUOTED(USE_EVENT_BROKER)
	BROKER_O="broker.o nebmods.o"
	AC_SUBST(BROKER_O)
	BROKER_H="../include/broker.h ../include/nebmods.h ../include/nebmodules.h ../include/nebcallbacks.h ../include/neberrors.h"
	AC_SUBST(BROKER_H)
fi


USEPERL=no;
INSTALLPERLSTUFF=no;
AC_ARG_ENABLE(embedded-perl,--enable-embedded-perl will enable embedded Perl interpreter,[
	AC_DEFINE_UNQUOTED(EMBEDDEDPERL)
	PERLLIBS="`perl -MExtUtils::Embed -e ldopts`" 
	PERLDIR="`perl -MConfig -e 'print $Config{installsitearch}'`" 
	CFLAGS="${CFLAGS} `perl -MExtUtils::Embed -e ccopts`" 
	USEPERL=yes
	INSTALLPERLSTUFF=yes;
	PERLXSI_O=perlxsi.o
	OBJS="${OBJS} ${PERLXSI_O}"
	echo "creating base/perlxsi.c"
	perl -MExtUtils::Embed -e xsinit -- -o base/perlxsi.c
	])

PERLCACHE=no;
AC_ARG_WITH(perlcache,--with-perlcache turns on cacheing of internally compiled Perl scripts,[
	AC_DEFINE(DO_CLEAN,"0")
	PERLCACHE=yes;
	]
	,[
	AC_DEFINE(DO_CLEAN,"1")
	PERLCACHE=no;
	])

dnl Is embedded Perl being compiled in?
if test x$USEPERL = xyes; then
	echo "Embedded Perl interpreter will be compiled in..."
	if test x$PERLCACHE = xyes; then
		echo "Internally compiled Perl scripts will be cached..."
	else
		echo "Internally compiled Perl scripts will NOT be cached..."
	fi
fi

dnl Test if we're using threaded Perl (patch by Chip Ach)
if test x$USEPERL = xyes; then
	if (perl -e 'use Config;exit -1 unless ($Config{'usethreads'});'); then
		echo "Using threaded perl"
		AC_DEFINE_UNQUOTED(THREADEDPERL)
	fi
fi


dnl Option for compiling under CYGWIN
nagios_name=nagios
nagiostats_name=nagiostats
AC_ARG_ENABLE(cygwin,--enable-cygwin enables building under the CYGWIN environment,[
	CFLAGS="${CFLAGS} -DCYGWIN" 
	nagios_name=nagios.exe;
	nagiostats_name=nagiostats.exe;
	])
AC_SUBST(nagios_name)
AC_SUBST(nagiostats_name)


dnl Should predictive failure routines be compiled in?
dnl AC_ARG_ENABLE(failure-prediction,--enable-failure-prediction will enable integration with failure prediction module (NOT HERE YET!),[
dnl	AC_DEFINE_UNQUOTED(PREDICT_FAILURES)
dnl	BASEEXTRALIBS="$BASEEXTRALIBS \$(FDATALIBS)"
dnl	echo "Failure prediction routines (incomplete!) will be compiled in..."
dnl	])

dnl Find traceroute
AC_PATH_PROG(PATH_TO_TRACEROUTE,traceroute)
AC_DEFINE_UNQUOTED(TRACEROUTE_COMMAND,"$PATH_TO_TRACEROUTE")



dnl Package directory for Solaris pkgmk (and other OSs, eventually)
dnl VERSION=`grep 1.0 include/common.h | cut -d ' ' -f 3 | sed 's/"//g'`
VERSION=$PKG_VERSION
PACKDIR=`pwd`/pkg
AC_SUBST(PACKDIR)
AC_SUBST(VERSION)
 
AC_CHECK_FUNC(snprintf,AC_DEFINE(HAVE_SNPRINTF),SNPRINTF_O=../common/snprintf.o)
AC_SUBST(SNPRINTF_O)

AC_MSG_CHECKING(for type va_list)
AC_TRY_COMPILE([#ifdef __STDC__
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#else
#include <sys/types.h>
#include <stdio.h>
#include <varargs.h>
#endif],
[va_list args;],
[AC_MSG_RESULT(yes)],
[AC_DEFINE(NEED_VA_LIST) AC_MSG_RESULT(no)])

AC_SUBST(CGIEXTRAS)
AC_SUBST(GDLIBS)
AC_SUBST(PERLLIBS)
AC_SUBST(PERLDIR)
AC_SUBST(PERLXSI_O)
AC_SUBST(BASEEXTRALIBS)
AC_SUBST(INITDIR)
AC_SUBST(INSTALLPERLSTUFF)

AC_PATH_PROG(PERL,perl)
AC_OUTPUT(Makefile subst pkginfo base/Makefile common/Makefile contrib/Makefile cgi/Makefile html/Makefile module/Makefile include/Makefile xdata/Makefile daemon-init html/index.html html/side.html)


perl subst include/locations.h
perl subst include/nagios.h

echo ""
echo "Creating sample config files in sample-config/ ..."

perl subst sample-config/nagios.cfg
perl subst sample-config/cgi.cfg
perl subst sample-config/resource.cfg
perl subst sample-config/httpd.conf

perl subst sample-config/template-object/localhost.cfg
perl subst sample-config/template-object/commands.cfg



dnl Review options
echo ""
echo ""
AC_MSG_RESULT([*** Configuration summary for $PKG_NAME $PKG_VERSION $PKG_REL_DATE ***:])

echo ""
echo " General Options:"
echo " -------------------------"

AC_MSG_RESULT([        Nagios executable:  $nagios_name])
AC_MSG_RESULT([        Nagios user/group:  $nagios_user,$nagios_grp])
AC_MSG_RESULT([       Command user/group:  $command_user,$command_grp])
if test x$USEPERL = xyes; then
if test x$PERLCACHE = xyes; then
AC_MSG_RESULT([            Embedded Perl:  yes, with caching])
else
AC_MSG_RESULT([            Embedded Perl:  yes, without caching])
fi
else
AC_MSG_RESULT([            Embedded Perl:  no])
fi
if test x$USE_EVENTBROKER = xyes; then
AC_MSG_RESULT([             Event Broker:  yes])
else
AC_MSG_RESULT([             Event Broker:  no])
fi
AC_MSG_RESULT([        Install \${prefix}:  $prefix])
AC_MSG_RESULT([                Lock file:  $lockfile])
AC_MSG_RESULT([           Init directory:  $init_dir])
AC_MSG_RESULT([                  Host OS:  $host_os])

echo ""
echo " Web Interface Options:"
echo " ------------------------"

AC_MSG_RESULT([                 HTML URL:  http://localhost$htmurl/])
AC_MSG_RESULT([                  CGI URL:  http://localhost$cgiurl/])
AC_MSG_RESULT([ Traceroute (used by WAP):  $PATH_TO_TRACEROUTE])

dnl echo ""
dnl echo " External Data Routines:"
dnl echo " ------------------------"

dnl AC_MSG_RESULT([              Status data:  $XSDCOMMENT])
dnl AC_MSG_RESULT([             Comment data:  $XCDCOMMENT])
dnl AC_MSG_RESULT([            Downtime data:  $XDDCOMMENT])
dnl AC_MSG_RESULT([          Peformance data:  $XPDCOMMENT])


echo ""
echo ""
echo "Review the options above for accuracy.  If they look okay,"
echo "type 'make all' to compile the main program and CGIs."
echo ""

