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

AC_INIT(src/worker.cc)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(worker, 2.4.0)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_RANLIB

dnl Checks for header files.
AC_PATH_X
AC_PATH_XTRA

if test "$no_x"; then
	AC_MSG_ERROR([X Window libraries and headers not found!
*********************************************************************
* Check your X installation (many distributions have these files in *
* the ...devel-packages (e.g. xdevel)).                             *
*********************************************************************])
fi

AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h sys/time.h unistd.h)

dnl Checks for libraries.
dnl AC_CHECK_LIB(X11,XCreateWindow)
AC_CHECK_LIB(m,pow)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_UID_T
AC_TYPE_MODE_T
AC_TYPE_SIZE_T
AC_TYPE_OFF_T
AC_HEADER_TIME
AC_STRUCT_TM

AC_CHECK_TYPE(loff_t,off_t)

dnl Checks for library functions.
AC_FUNC_FNMATCH
AC_FUNC_STRFTIME
AC_CHECK_FUNCS(mkdir)
dnl AC_CHECK_FUNCS(nanosleep,,AC_CHECK_LIB(posix4,nanosleep))
AC_CHECK_FUNCS(nanosleep,,AC_CHECK_LIB(posix4, nanosleep, 
	LIBS="$LIBS -lposix4"
	AC_DEFINE(HAVE_NANOSLEEP),
	AC_CHECK_LIB(rt, nanosleep, 
		LIBS="$LIBS -lrt"
		AC_DEFINE(HAVE_NANOSLEEP),
		no_nanosleep="yes"
	)))
AC_CHECK_FUNCS(usleep,, no_usleep="yes" )

if test "$no_nanosleep" -a "$no_usleep"; then
	AC_MSG_ERROR([Neither nanosleep nor usleep was found!
*********************************************************************
* Worker need at least on of this. Please contact the author at     *
*         www.boomerangsworld.de/worker/wfeedback.php3              *
* and I will try to help!                                           *
*********************************************************************])  
fi

AC_CHECK_FUNCS(sysinfo)
AC_CHECK_FUNCS(getcwd gettimeofday mkdir select strstr)

dnl how to get fileinformations
AC_CHECK_FUNCS(statvfs)
AC_CHECK_HEADERS(sys/vfs.h sys/statvfs.h)

AC_CHECK_HEADERS(sys/sysinfo.h)
AC_CHECK_HEADERS(linux/kernel.h)

AC_ARG_ENABLE(zlib,
	[  --enable-zlib           use zlib (default)
  --disable-zlib          do not use zlib],
	use_zlib=$enableval,
        use_zlib=yes)

if test "$use_zlib" = yes; then
  AC_CHECK_HEADERS(zlib.h,AC_CHECK_LIB(z,gzopen,,use_zlib=no))
fi

dnl check for pthread library
AC_ARG_ENABLE(pthread,
	[  --enable-pthread        use threads (default)
  --disable-pthread       do not use threads],
	use_pthread=$enableval,
        use_pthread=yes)

if test "$use_pthread" = "yes"; then
  AC_CHECK_LIB(pthread,pthread_create,
               AC_DEFINE(HAVE_LIBPTHREAD)
               LIBS="$LIBS -lpthread"
               AC_DEFINE(WANT_THREADS),
               use_pthread=no )
fi

AC_ARG_ENABLE(debug,
	[  --enable-debug	  compile some debug informations],
	AC_DEFINE(DEBUG))

dnl LIBS=" -lX11 $LIBS"

WORKER_LDADD="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"

dnl Checks for header files.
AC_SUBST(X_CFLAGS)
AC_SUBST(X_PRE_LIBS)
AC_SUBST(X_EXTRA_LIBS)
AC_SUBST(x_libraries)
AC_SUBST(WORKER_LDADD)
CPPFLAGS="$CPPFLAGS -Wall"
CXXFLAGS="$CXXFLAGS -fcheck-new"

dnl Check for compile error when adding X_CFLAGS to CPPFLAGS
dnl as I know this is a problem on Solaris
AC_LANG_CPLUSPLUS
AC_CACHE_CHECK(for needed X include dir,
		dneedxf,[AC_TRY_COMPILE([#include <X11/Xlib.h>],
		[int x; x=0;], dneedxf=no, dneedxf=yes)])
if test x$dneedxf = xyes; then
	CPPFLAGS="$CPPFLAGS $X_CFLAGS"
fi

if test "$prefix" = NONE; then
  AC_DEFINE_UNQUOTED(PREFIX, "$ac_default_prefix")
else
  AC_DEFINE_UNQUOTED(PREFIX, "$prefix")
fi

AC_OUTPUT(Makefile
src/Makefile
src/aguix/Makefile
catalogs/Makefile
Icons/Makefile
examples/Makefile
man/Makefile)

echo ""
echo "Configuration finished:"
echo "  ZLib-Usage: $use_zlib"
echo "  Thread-Usage: $use_pthread"
echo ""
echo " To compile do"
echo ""
echo "   make"
echo ""
