dnl Process this file with autoconf to produce a configure script.
dnl OK, autoconf stuff first
AC_INIT(modules/loader.c)
dnl AC_INIT(dancer-ircd, 1.1.0.1)
dnl AC_CONFIG_SRCDIR(src/wall.c)
AC_CONFIG_AUX_DIR(autoconf)
AC_CANONICAL_SYSTEM

dnl Now the automake init bit
AM_INIT_AUTOMAKE(dancer-ircd, 1.1.0.1)
AM_CONFIG_HEADER(include/setup.h)

dnl Set these here and they'll get picked up by AC_PROG_CC
dnl Yes, these are draconian. You're not supposed to do a release build with them
CFLAGS=${CFLAGS:-"-g3 -O0 -Wall -W -Wcast-qual -Wno-unused -Wstrict-prototypes -Wmissing-prototypes"}

dnl Checks for programs.
AC_PROG_CC
dnl We're writing ANSI C here
dnl AC_PROG_CC_STDC
AC_ISC_POSIX
AC_PROG_INSTALL
dnl AC_PROG_RANLIB
AC_LIBTOOL_DLOPEN
AM_PROG_LIBTOOL

dnl Some features which are nice if they are available
dnl This lets us use them at will
AC_C_INLINE
AC_C_CONST

dnl Checks for libraries.
dnl Enough carting an old zlib around, already! Build system should have it.
AC_CHECK_LIB(z, inflate)
dnl Replace `main' with a function in -lnsl:
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_FUNC(res_mkquery,, AC_CHECK_LIB(resolv, res_mkquery))
AC_CHECK_LIB(socket, socket)
dnl AC_CHECK_FUNC(crypt,, AC_CHECK_LIB(descrypt, crypt,,AC_CHECK_LIB(crypt, crypt,,)))

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/param.h unistd.h errno.h string.h stdlib.h stddef.h getopt.h) 

dnl Check for stdarg.h - if we can't find it, halt configure
AC_CHECK_HEADER(stdarg.h, , AC_MSG_ERROR(** stdarg.h could not be found - ircd-hybrid will not compile without it **))

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL

dnl check for poll() call
AC_CHECK_FUNC(poll, AC_DEFINE(USE_POLL),)

dnl check for u_int32_t
AC_CHECK_TYPE(u_int32_t, unsigned long)

dnl taken from ircd's Config script
AC_MSG_CHECKING(for non-blocking socket implementation)
AC_TRY_RUN(
[
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <signal.h>
#include <stdlib.h>
alarmed()
{
	exit(1);
}
int main()
{
#if defined(O_NONBLOCK)
	char b[12], x[32];
	int f, l = sizeof(x);
	f = socket(AF_INET, SOCK_DGRAM, 0);
	if (f >= 0 && !(fcntl(f, F_SETFL, O_NONBLOCK))) {
		signal(SIGALRM, alarmed);
		alarm(3);
		recvfrom(f, b, 12, 0, (struct sockaddr *)x, &l);
		alarm(0);
		exit(0);
	}
#endif
	exit(1);
}
], AC_DEFINE(NBLOCK_POSIX) AC_MSG_RESULT( O_NONBLOCK ),, AC_MSG_RESULT( cross compiling ))

AC_FUNC_MMAP

AC_TRY_RUN(
[
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <signal.h>
#include <stdlib.h>
alarmed()
{
	exit(1);
}
int main()
{
#if defined( O_NDELAY ) && !defined( NBLOCK_POSIX )
	char b[12], x[32];
	int f, l = sizeof(x);
	f = socket(AF_INET, SOCK_DGRAM, 0);
	if (f >= 0 && !(fcntl(f, F_SETFL, O_NDELAY))) {
		signal(SIGALRM, alarmed);
		alarm(3);
		recvfrom(f, b, 12, 0, (struct sockaddr *)x, &l);
		alarm(0);
		exit(0);
	}
#endif
	exit(1);
}
], AC_DEFINE(NBLOCK_BSD) AC_MSG_RESULT( O_NDELAY ),, AC_MSG_RESULT(unable to test))

AC_TRY_RUN(
[
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <signal.h>
#include <stdlib.h>
alarmed()
{
	exit(1);
}
int main()
{
#if !defined(NBLOCK_BSD) && !defined(NBLOCK_POSIX) && defined(FIONBIO)
	char b[12], x[32];
	int f, l = sizeof(x);
	f = socket(AF_INET, SOCK_DGRAM, 0);
	if (f >= 0 && !(fcntl(f, F_SETFL, FIONBIO))) {
		signal(SIGALRM, alarmed);
		alarm(3);
		recvfrom(f, b, 12, 0, (struct sockaddr *)x, &l);
		alarm(0);
		exit(0);
	}
#endif /* !NBLOCK_POSIX && !NBLOCK_BSD && FIONBIO */
	exit(1);
}
], AC_DEFINE(NBLOCK_SYSV) AC_MSG_RESULT(FIONBIO),, AC_MSG_RESULT(unable to test))

dnl taken from ircd's Config script
AC_MSG_CHECKING(signal implementation)
AC_TRY_RUN(
[
#include <stdlib.h>
int main()
{
#if defined(HAVE_SIGACTION)
	exit(0);
#else
	exit(1);
#endif
}
], AC_DEFINE(POSIX_SIGNALS) AC_MSG_RESULT(posix),
    AC_TRY_RUN(
[
#include <signal.h>
#include <stdlib.h>
int calls = 0;
void handler()
{
	if (calls) return;
 	calls++;
	kill(getpid(), SIGTERM);
	sleep(1);
}
int main()
{
	signal(SIGTERM, handler);
	kill(getpid(), SIGTERM);
	exit (0);
}
], AC_DEFINE(BSD_RELIABLE_SIGNALS) AC_MSG_RESULT(bsd),
    AC_DEFINE(SYSV_UNRELIABLE_SIGNALS) AC_MSG_RESULT(sysv), AC_MSG_RESULT(uknown)),
AC_MSG_RESULT(unknown))

dnl don't add more of these unless it's critical.  Trying to get rid of them.
AC_MSG_CHECKING(operating system)
case "$target" in
*-solaris2*) AC_DEFINE(OS_SOLARIS2) AC_MSG_RESULT(Solaris 2.x) ;;
*) AC_MSG_RESULT(nothing special required) ;;
esac

dnl shared-modules
AC_ARG_ENABLE(shared-modules,
[  --enable-shared-modules      Enable shared modules.
  --disable-shared-modules     Disable shared modules. ],

[ 
  case "$enableval" in
  yes)
    build_shared_modules=yes
    ;;
  no)
    build_shared_modules=no
    ;;
  *)
    build_shared_modules=yes
    ;;
  esac
],
  build_shared_modules=$enable_shared
)

if test "$build_shared_modules" = "no"; then
	AC_DEFINE(STATIC_MODULES,1,Link the modules in statically)
else
	AC_CHECK_FUNC(dlopen,, AC_CHECK_LIB(dl, dlopen,,AC_MSG_ERROR(**
	dlopen could not be found - try running configure with --disable-shared-modules)))
fi

AM_CONDITIONAL(SHARED_MODULES, test x$build_shared_modules = xyes)

dnl These bits are used to store build-time information in version.c for
dnl  display in m_info()
BUILD_UNAME=`uname -a`
BUILD_TIME=`date`
AC_SUBST(BUILD_UNAME)
AC_SUBST(BUILD_TIME)

AC_OUTPUT(
Makefile
src/Makefile
tools/Makefile
include/Makefile
src/version.c
modules/Makefile
modules/support/Makefile
modules/server/Makefile
modules/user/Makefile
modules/database/Makefile
modules/query/Makefile
modules/admin/Makefile
modules/extra/Makefile
)

echo "

Configuration:

  Detected OS:      ${host_os}
  Dancer version:   ${VERSION}
  Install prefix:   ${prefix}
  Dynamic modules:  ${build_shared_modules}
"
