dnl Prefered emacs editing mode: -*- shell-script -*-
dnl
dnl Process this file with autoconf to produce a configure script.
dnl
dnl Copyright (c) 1997, by Carlo Wood <carlo@runaway.xs4all.nl>
dnl Copyright (C) 2001  Kevin L. Mitchell <klmitch@mit.edu>
dnl
dnl This program is free software; you can redistribute it and/or modify
dnl it under the terms of the GNU General Public License as published by
dnl the Free Software Foundation; either version 1, or (at your option)
dnl any later version.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
dnl
dnl $Id: configure.in,v 1.34.2.1 2005/12/14 03:01:38 entrope Exp $

dnl Make sure we are in the correct directory (someone could have run
dnl 'configure' with a wrong '--srcdir').
AC_INIT(ircd/ircd.c)

dnl Set the default prefix
AC_PREFIX_DEFAULT([$HOME])
AC_MSG_CHECKING([for installation prefix])
AC_CACHE_VAL(unet_cv_prefix, [unet_cv_prefix=$HOME])
if test x"$prefix" != xNONE; then
    unet_cv_prefix=$prefix
fi
AC_MSG_RESULT([$unet_cv_prefix])
dnl HACK WARNING: We are referencing an autoconf internal variable.  This is
dnl the only way to force the prefix to be retrieved from the config.cache
dnl file!
ac_default_prefix=$unet_cv_prefix

dnl Define the input and output configuration header file.
AC_CONFIG_HEADER([config.h])

dnl Demand at least version 2.59 of autoconf (for AS_HELP_STRING)
AC_PREREQ(2.59)

dnl Find out what type of system we are
AC_CANONICAL_HOST

dnl This should be done early.
AC_PROG_CC

dnl ANSIfy the C compiler whenever possible.
AM_PROG_CC_STDC

dnl Checks for libraries.

dnl Locate the library containing crypt
AC_SEARCH_LIBS(crypt, descrypt crypt, ,
[AC_MSG_ERROR([Unable to find library containing crypt()])])

dnl Do all the checks necessary to figure out -lnsl / -lsocket stuff
AC_LIBRARY_NET

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(crypt.h poll.h inttypes.h stdint.h sys/devpoll.h sys/epoll.h sys/event.h sys/param.h sys/resource.h sys/socket.h)

dnl Checks for typedefs, structures, and compiler characteristics
dnl AC_C_CONST
AC_C_BIGENDIAN
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_TYPE_UID_T
unet_CHECK_TYPE_SIZES
AC_CHECK_TYPE(struct sockaddr_in6, [unet_have_sockaddr_in6="yes"], [unet_have_sockaddr_in6="no"], [#include <sys/types.h>
#include <netinet/in.h>])

dnl Check for socklen_t.  In traditional BSD this is an int, but some
dnl OSes use a different type.  Test until we find something that will
dnl work properly.  Test borrowed from a patch submitted for Python.
AC_CHECK_TYPE([socklen_t], ,[
  AC_MSG_CHECKING([for socklen_t equivalent])
  AC_CACHE_VAL([curl_cv_socklen_t_equiv],
  [
dnl Systems have either "struct sockaddr*" or "void*" as second
dnl arg to getpeername.
    curl_cv_socklen_t_equiv=
    for arg2 in "struct sockaddr" void ; do
      for t in int size_t unsigned long "unsigned long" ; do
        AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>
int getpeername (int $arg2 *, $t *);],[$t len;
  getpeername(0, 0, &len);], [curl_cv_socklen_t_equiv="$t"
  break])
      done
    done
  ])
  AC_MSG_RESULT($curl_cv_socklen_t_equiv)
  AC_DEFINE_UNQUOTED(socklen_t, $curl_cv_socklen_t_equiv,
      [type to use in place of socklen_t if not defined])],
  [#include <sys/types.h>
#include<sys/socket.h>])

dnl Checks for library functions.
AC_CHECK_FUNCS([kqueue setrlimit getrusage times])

dnl Do we have restarting syscalls ?
AC_SYS_RESTARTABLE_SYSCALLS

dnl Check for required features for admins?
AC_MSG_CHECKING([for donuts])
AC_MSG_RESULT([yes])

dnl Test for programs
AC_PROG_AWK
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PATH_PROGS(RMPROG, rm, /bin/rm)
AC_PATH_PROGS(SHPROG, sh, /bin/sh)

dnl (F)LEX - needed for the new conf file parser
AC_PROG_LEX
dnl The autoconf docs say $LEX defaults to 'lex'.  They lie.
if test "$LEX" = ":" ; then
  AC_MSG_ERROR([Cannot find flex.])
elif echo "" | $LEX -V -v --version > /dev/null 2>&1 ; then
  :
else
  AC_MSG_ERROR([Cannot use $LEX as flex.])
fi

if test -z "$LEXLIB" ; then
  AC_MSG_FAILURE([Cannot find a library with yywrap() in, but flex was found.
  It's possible the compiler you're using ($CC) is incompatible with the
  installed library.])
fi

LIBS="$LEXLIB $LIBS"

dnl YACC - ditto
AC_PROG_YACC
dnl The autoconf docs say $YACC defaults to 'yacc'.  This seems to be true,
dnl but judging from AC_PROG_LEX, it may not stay true.
if test "$YACC" = ":" ; then
  AC_MSG_ERROR([Cannot find yacc.])
elif echo "" | $YACC -V -v --version > /dev/null 2>&1 ; then
  :
else
dnl byacc does not seem to have any way to test for workingness, so only warn.
  AC_MSG_WARN([$YACC may not work as yacc.])
fi

unet_NONBLOCKING
unet_SIGNALS

dnl Check OS for os_dep files.
AC_MSG_CHECKING(for OS-dependent information)
case "$host" in
    *-linux*)
	AC_MSG_RESULT([Linux ($host) found.])
	unet_poll_syscall=yes
	;;

    *-solaris*)
	AC_MSG_RESULT([Solaris ($host) found.])
	if test x"$ac_cv_header_poll_h" = xyes; then
	    unet_poll_syscall=yes
	else
	    unet_poll_syscall=no
	fi
        AC_DEFINE([IRCU_SOLARIS], 1, [Define if building on Solaris])
	;;

    *-sunos*)
	AC_MSG_RESULT([Solaris ($host) found.])
	unet_poll_syscall=no
	;;

    *-openbsd*)
	AC_MSG_RESULT([OpenBSD ($host) found.])
	if test x"$ac_cv_header_poll_h" = xyes; then
	    unet_poll_syscall=yes
	else
	    unet_poll_syscall=no
	fi
	;;

    *-*bsd*)
	AC_MSG_RESULT([Generic BSD ($host) found.])
	if test x"$ac_cv_header_poll_h" = xyes; then
	    unet_poll_syscall=yes
	else
	    unet_poll_syscall=no
	fi
	;;

    *-darwin*)
	AC_MSG_RESULT([Darwin (Mac OS X) ($host) found.])
	unet_poll_syscall=no
	;;

    *)
	AC_MSG_RESULT([Unknown system type $host found.])
	AC_MSG_WARN([Unknown OS type; using generic routines.])
	unet_poll_syscall=no
	;;
esac

dnl Check user configuration options
dnl Start with --enable-poll
AC_MSG_CHECKING([whether to enable use of poll()])
AC_ARG_ENABLE([poll],
[  --enable-poll           Force poll to be used regardless of whether or not
                          it is a system call],
[unet_cv_enable_poll=$enable_poll],
[AC_CACHE_VAL(unet_cv_enable_poll,
[unet_cv_enable_poll=$unet_poll_syscall])])

# Force poll to be disabled if there is no poll.h
if test x"$ac_cv_header_poll_h" != xyes; then
    unet_cv_enable_poll=no
fi

AC_MSG_RESULT([$unet_cv_enable_poll])

if test x"$unet_cv_enable_poll" = xyes; then
    AC_DEFINE([USE_POLL], 1, [Specify whether or not to use poll()])
    ENGINE_C=engine_poll.c
else
    ENGINE_C=engine_select.c
fi
AC_SUBST(ENGINE_C)

dnl Now look for --enable-debug
AC_MSG_CHECKING([whether to enable debug mode])
AC_ARG_ENABLE([debug],
[  --enable-debug          Turn on debugging mode],
[unet_cv_enable_debug=$enable_debug],
[AC_CACHE_VAL(unet_cv_enable_debug,
[unet_cv_enable_debug=no])])
AC_MSG_RESULT([$unet_cv_enable_debug])

if test x"$unet_cv_enable_debug" = xyes; then
    AC_DEFINE([DEBUGMODE], 1, [Enable debugging code])
fi

dnl Now look for --enable-leak-detect
AC_MSG_CHECKING([whether to enable leak detection])
AC_ARG_WITH([leak-detect],
[  --with-leak-detect          Turn on the leak detector(requires patched boehm)],
[unet_cv_with_leak_detect=$with_leak_detect],
[AC_CACHE_VAL(unet_cv_with_leak_detect,
[unet_cv_with_leak_detect=no])])
AC_MSG_RESULT([$unet_cv_enable_leak_detect])

if test x"$unet_cv_with_leak_detect" != xno; then
    LIBS="-lgc $LIBS"
    CFLAGS="-DMDEBUG $CFLAGS"
    if test x"$unet_cv_with_leak_detect" != xyes; then
       LIBS="-L$unet_cv_with_leak_detect $LIBS"
    fi
fi

AC_ARG_WITH([ipv6],
    AS_HELP_STRING([--without-ipv6], [disable IPv6 support (default is autodetect)]),
    [ac_cv_use_ipv6=$withval],
    [ac_cv_use_ipv6=$unet_have_sockaddr_in6])
AC_CACHE_CHECK([whether to use IPv6], [ac_cv_use_ipv6], [ac_cv_use_ipv6=no])
if test x"$ac_cv_use_ipv6" != "xno" ; then
    AC_DEFINE([IPV6], 1, [Enable IPv6 support])
fi

dnl And now for --disable-asserts
AC_MSG_CHECKING([whether to enable asserts])
AC_ARG_ENABLE([asserts],
[  --disable-asserts       Disable assertion checking],
[unet_cv_enable_asserts=$enable_asserts],
[AC_CACHE_VAL(unet_cv_enable_asserts,
[unet_cv_enable_asserts=yes])])
AC_MSG_RESULT([$unet_cv_enable_asserts])

if test x"$unet_cv_enable_asserts" = xno; then
    AC_DEFINE([NDEBUG], 1, [Disable assertions])
fi

dnl Now check for --enable-profile
AC_MSG_CHECKING([whether to enable profiling support (gprof)])
AC_ARG_ENABLE([profile],
[  --enable-profile        Enable profiling support (add -pg to CFLAGS and LDFLAGS)],
[unet_cv_enable_profile=$enable_profile],
[AC_CACHE_VAL(unet_cv_enable_profile,
[unet_cv_enable_profile=no])])
AC_MSG_RESULT([$unet_cv_enable_profile])

if test x"$unet_cv_enable_profile" = xyes; then
    CFLAGS="-pg $CFLAGS"
    LDFLAGS="-pg $LDFLAGS"
fi

dnl Now check for --enable-pedantic
AC_MSG_CHECKING([whether to enable pedantic compiler warnings])
AC_ARG_ENABLE([pedantic],
[  --enable-pedantic       Enable pedantic warnings (add -pedantic to CFLAGS)],
[unet_cv_enable_pedantic=$enable_pedantic],
[AC_CACHE_VAL(unet_cv_enable_pedantic,
[unet_cv_enable_pedantic=no])])
AC_MSG_RESULT([$unet_cv_enable_pedantic])

if test x"$unet_cv_enable_pedantic" = xyes; then
    CFLAGS="-pedantic $CFLAGS"
fi

dnl Now check for --enable-warnings
AC_MSG_CHECKING([whether to enable compiler warnings])
AC_ARG_ENABLE([warnings],
[  --enable-warnings       Enable warnings (add -Wall to CFLAGS)],
[unet_cv_enable_warnings=$enable_warnings],
[AC_CACHE_VAL(unet_cv_enable_warnings,
[unet_cv_enable_warnings=no])])
AC_MSG_RESULT([$unet_cv_enable_warnings])

if test x"$unet_cv_enable_warnings" = xyes; then
    CFLAGS="-Wall $CFLAGS"
fi

dnl --disable-inlines check...
AC_MSG_CHECKING([whether to enable inlining for a few critical functions])
AC_ARG_ENABLE([inlines],
[  --disable-inlines       Disable inlining for a few critical functions],
[unet_cv_enable_inlines=$enable_inlines],
[AC_CACHE_VAL(unet_cv_enable_inlines,
[unet_cv_enable_inlines=yes])])
AC_MSG_RESULT([$unet_cv_enable_inlines])

if test x"$unet_cv_enable_inlines" = xyes; then
    AC_DEFINE([FORCEINLINE], 1, [Force inlining for a few critical functions])
fi

dnl --disable-devpoll check...
AC_MSG_CHECKING([whether to enable the /dev/poll event engine])
AC_ARG_ENABLE([devpoll],
[  --disable-devpoll       Disable the /dev/poll-based engine],
[unet_cv_enable_devpoll=$enable_devpoll],
[AC_CACHE_VAL(unet_cv_enable_devpoll,
[unet_cv_enable_devpoll=yes])])

if test x"$ac_cv_header_sys_devpoll_h" = xno; then
    unet_cv_enable_devpoll=no
fi

AC_MSG_RESULT([$unet_cv_enable_devpoll])

if test x"$unet_cv_enable_devpoll" != xno; then
    AC_DEFINE([USE_DEVPOLL], 1, [Define to enable the /dev/poll engine])
    ENGINE_C="engine_devpoll.c $ENGINE_C"
fi

dnl --disable-kqueue check...
AC_MSG_CHECKING([whether to enable the kqueue event engine])
AC_ARG_ENABLE([kqueue],
[  --disable-kqueue        Disable the kqueue-based engine],
[unet_cv_enable_kqueue=$enable_kqueue],
[AC_CACHE_VAL(unet_cv_enable_kqueue,
[unet_cv_enable_kqueue=yes])])

if test x"$ac_cv_header_sys_event_h" = xno -o x"$ac_cv_func_kqueue" = xno; then
    unet_cv_enable_kqueue=no
fi

AC_MSG_RESULT([$unet_cv_enable_kqueue])

if test x"$unet_cv_enable_kqueue" != xno; then
    AC_DEFINE([USE_KQUEUE], 1, [Define to enable the kqueue engine])
    ENGINE_C="engine_kqueue.c $ENGINE_C"
fi

dnl --disable-epoll check
AC_MSG_CHECKING([whether to enable the epoll event engine])
AC_ARG_ENABLE([epoll],
[  --disable-epoll         Disable the epoll-based engine],
[unet_cv_enable_epoll=$enable_epoll],
[AC_CACHE_VAL(unet_cv_enable_epoll,
[unet_cv_enable_epoll=yes])])

if test x"$ac_cv_header_sys_epoll_h" = xno -o x"$ac_cv_func_epoll" = xno; then
    unet_cv_enable_epoll=no
fi

AC_MSG_RESULT([$unet_cv_enable_epoll])

dnl If we have the header and user has not refused epoll, we still need
dnl to check whether the functions are properly defined.
if test x"$unet_cv_enable_epoll" != xno; then
    AC_MSG_CHECKING([whether epoll functions are properly defined])
    AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <sys/epoll.h>], [epoll_create(10);])],
        [AC_MSG_RESULT([yes])],
        [AC_MSG_RESULT([no])
         AC_DEFINE([EPOLL_NEED_BODY], 1, [Define to implement epoll system calls])])
    AC_DEFINE([USE_EPOLL], 1, [Define to enable the epoll engine])
    ENGINE_C="engine_epoll.c $ENGINE_C"
fi

dnl How to copy one va_list to another?
AC_CACHE_CHECK([for va_copy], unet_cv_c_va_copy, [AC_LINK_IFELSE(
  [AC_LANG_PROGRAM([#include <stdarg.h>], [va_list ap1, ap2; va_copy(ap1, ap2);])],
  [unet_cv_c_va_copy="yes"],
  [unet_cv_c_va_copy="no"]
)])
if test "$unet_cv_c_va_copy" = "yes" ; then
  AC_DEFINE(HAVE_VA_COPY, 1, [Define if we have va_copy])
fi

AC_CACHE_CHECK([for __va_copy], unet_cv_c___va_copy, [AC_LINK_IFELSE(
  [AC_LANG_PROGRAM([#include <stdarg.h>], [va_list ap1, ap2; __va_copy(ap1, ap2);])],
  [unet_cv_c___va_copy="yes"],
  [unet_cv_c___va_copy="no"]
)])
if test "$unet_cv_c___va_copy" = "yes" ; then
  AC_DEFINE(HAVE___VA_COPY, 1, [Define if we have __va_copy])
fi

dnl --with-symlink lets us set the name of the symlink; defaults to "ircd"
AC_MSG_CHECKING([what name to give the symlink])
AC_ARG_WITH([symlink],
[  --with-symlink=name     Name to give the symlink; if name is "no," no
                          symlink will be created.],
[unet_cv_with_symlink=$with_symlink],
[AC_CACHE_VAL(unet_cv_with_symlink,
[unet_cv_with_symlink="ircd"])])

if test x"$unet_cv_with_symlink" = xyes; then
    unet_cv_with_symlink="ircd"
fi

AC_MSG_RESULT([$unet_cv_with_symlink])

if test x"$unet_cv_with_symlink" = xno; then
    INSTALL_RULE=install-no-symlink
    SYMLINK=
else
    INSTALL_RULE=install-with-symlink
    SYMLINK=$unet_cv_with_symlink
fi
AC_SUBST(INSTALL_RULE)
AC_SUBST(SYMLINK)

dnl --with-mode lets us set the permissions on the binary
AC_MSG_CHECKING([what permissions to set on the installed binary])
AC_ARG_WITH([mode],
[  --with-mode=mode        Permissions (in octal) to give the binary],
[unet_cv_with_mode=$with_mode],
[AC_CACHE_VAL(unet_cv_with_mode,
[unet_cv_with_mode=711])])

if test x"$unet_cv_with_mode" = xyes -o x"$unet_cv_with_mode" = xno; then
    unet_cv_with_mode=711
fi

AC_MSG_RESULT([$unet_cv_with_mode])

IRCDMODE=$unet_cv_with_mode
AC_SUBST(IRCDMODE)

dnl --with-owner lets us set the owner of the binary
changequote(,)dnl
unet_uid=`id | sed -e 's/.*uid=[0-9]*(//' -e 's/).*//' 2> /dev/null`
changequote([,])dnl
AC_MSG_CHECKING([which user should own the installed binary])
AC_ARG_WITH([owner],
[  --with-owner=owner      Specify owner of the installed binary],
[unet_cv_with_owner=$with_owner],
[AC_CACHE_VAL(unet_cv_with_owner,
[unet_cv_with_owner=$unet_uid])])

if test x"$unet_cv_with_owner" = xyes -o x"$unet_cv_with_owner" = xno; then
    unet_cv_with_owner=$unet_uid
fi

AC_MSG_RESULT([$unet_cv_with_owner])

IRCDOWN=$unet_cv_with_owner
AC_SUBST(IRCDOWN)

dnl --with-group lets us set the group owner of the binary
changequote(,)dnl
unet_gid=`id | sed -e 's/.*gid=[0-9]*(//' -e 's/).*//' 2> /dev/null`
changequote([,])dnl
AC_MSG_CHECKING([which group should own the installed binary])
AC_ARG_WITH([group],
[  --with-group=group      Specify group owner of the installed binary],
[unet_cv_with_group=$with_group],
[AC_CACHE_VAL(unet_cv_with_group,
[unet_cv_with_group=$unet_gid])])

if test x"$unet_cv_with_group" = xyes -o x"$unet_cv_with_group" = xno; then
    unet_cv_with_group=$unet_gid
fi

AC_MSG_RESULT([$unet_cv_with_group])

IRCDGRP=$unet_cv_with_group
AC_SUBST(IRCDGRP)

dnl --with-domain lets us set the domain name for some statistics-gathering
unet_domain=
if test -f /etc/resolv.conf; then
    unet_domain=`awk '/^domain/ { print $2; exit }' /etc/resolv.conf`
    if test x"$unet_domain" = x; then
	unet_domain=`awk '/^search/ { print $2; exit }' /etc/resolv.conf`
    fi
fi
AC_MSG_CHECKING([for site domain name])
AC_ARG_WITH([domain],
[  --with-domain=domain    Domain name to use in local statistics gathering],
[unet_cv_with_domain=$with_domain],
[AC_CACHE_VAL(unet_cv_with_domain,
[unet_cv_with_domain=$unet_domain])])

if test x"$unet_cv_with_domain" = xyes -o x"$unet_cv_with_domain" = xno; then
    unet_cv_with_domain=$unet_domain
fi
if test x"$unet_cv_with_domain" = xno; then
    AC_MSG_ERROR([Unable to determine server DNS domain; use --with-domain to set it])
fi

AC_MSG_RESULT([$unet_cv_with_domain])

AC_DEFINE_UNQUOTED(DOMAINNAME, "*$unet_cv_with_domain",
[Domain name to be used for some statistics gathering])

dnl --with-chroot lets us define a directory that we are going to be using
dnl as the root of our filesystem
AC_MSG_CHECKING([if chroot operation is desired])
AC_ARG_WITH([chroot],
[  --with-chroot=dir       Specify that the server will be operated under
                          a different root directory given by dir.  See
                          doc/readme.chroot for more information.],
[unet_cv_with_chroot=$with_chroot],
[AC_CACHE_VAL(unet_cv_with_chroot,
[unet_cv_with_chroot=no])])

if test x"$unet_cv_with_chroot" = xyes; then
    AC_MSG_ERROR([--with-chroot given with no directory.  See doc/readme.chroot.])
fi

# Ensure there are no trailing /'s to mess us up
unet_cv_with_chroot=`echo "$unet_cv_with_chroot" | sed 's%/*$%%'`

AC_MSG_RESULT([$unet_cv_with_chroot])

dnl Determine some default directory names
dnl
dnl HACK WARNING: We are referencing an autoconf internal variable.  This is
dnl the only way to figure out what value $prefix will have when we go to do
dnl the install--and the only way we can stick that value in our definitions
dnl of SPATH, etc.
# Deal with the annoying value "NONE" here
unet_save_prefix=$prefix
if test x"$prefix" = xNONE; then
    prefix=$ac_default_prefix
else
    prefix=$prefix
fi

unet_save_exec_prefix=$exec_prefix
if test x"$exec_prefix" = xNONE; then
    exec_prefix=$prefix
else
    exec_prefix=$exec_prefix
fi

# Obtain the actual interesting directories
unet_bindir=`eval echo "$bindir"`
unet_libdir=`eval echo "$libdir"`

# Restore the original settings of $prefix and $exec_prefix
prefix=$unet_save_prefix
exec_prefix=$unet_save_exec_prefix

dnl Now compute the name of the binary and verify that it will work under
dnl chroot operation
AC_MSG_CHECKING([where the binary will be for /restart])
if test x"$unet_cv_with_symlink" = xno; then
    unet_spath="$unet_bindir/ircd"
else
    unet_spath="$unet_bindir/$unet_cv_with_symlink"
fi
AC_MSG_RESULT([$unet_spath])

if test x"$unet_cv_with_chroot" != xno; then
    if echo "$unet_spath" | grep "^$unet_cv_with_chroot" > /dev/null 2>&1; then
	unet_spath=`echo "$unet_spath" | sed "s%^$unet_cv_with_chroot%%"`
    else
	AC_MSG_WARN([Binary $unet_spath not relative to root directory $unet_cv_with_chroot; restarts will probably fail])
    fi
fi

AC_DEFINE_UNQUOTED(SPATH, "$unet_spath", [Path to executable for restarts])

dnl --with-dpath sets the all-important DPATH
AC_MSG_CHECKING([what the data directory should be])
AC_ARG_WITH([dpath],
[  --with-dpath=dir        Directory for all server data files],
[unet_cv_with_dpath=$with_dpath],
[AC_CACHE_VAL(unet_cv_with_dpath,
[unet_cv_with_dpath=$unet_libdir])])

if test x"$unet_cv_with_dpath" = xyes -o x"$unet_cv_with_dpath" = xno; then
    unet_cv_with_dpath=$unet_libdir
fi

# Ensure there are no trailing /'s to mess us up
unet_cv_with_dpath=`echo "$unet_cv_with_dpath" | sed 's%/*$%%'`

AC_MSG_RESULT([$unet_cv_with_dpath])

if test x"$unet_cv_with_chroot" != xno; then
    if echo "$unet_cv_with_dpath" | grep "^$unet_cv_with_chroot" > /dev/null 2>&1; then
	unet_dpath=`echo "$unet_cv_with_dpath" | sed "s%^$unet_cv_with_chroot%%"`
    else
	AC_MSG_ERROR([Data directory $unet_cv_with_dpath not relative to root directory $unet_cv_with_chroot])
    fi
else
    unet_dpath=$unet_cv_with_dpath
fi

AC_DEFINE_UNQUOTED(DPATH, "$unet_dpath", [Path to data directory])

DPATH=$unet_cv_with_dpath
AC_SUBST(DPATH)

dnl --with-cpath allows us to specify the configuration file
AC_MSG_CHECKING([where the default configuration file resides])
AC_ARG_WITH([cpath],
[  --with-cpath=file       Set server configuration file],
[unet_cv_with_cpath=$with_cpath],
[AC_CACHE_VAL(unet_cv_with_cpath,
[unet_cv_with_cpath="ircd.conf"])])

if test x"$unet_cv_with_cpath" = xyes -o x"$unet_cv_with_cpath" = xno; then
    unet_cv_with_cpath="ircd.conf"
fi

AC_MSG_RESULT([$unet_cv_with_cpath])

if echo "$unet_cv_with_cpath" | grep '^/' > /dev/null 2>&1; then
    # Absolute path; check against chroot stuff
    if test x"$unet_cv_with_chroot" != xno; then
	if echo "$unet_cv_with_cpath" | grep "^$unet_cv_with_chroot" > /dev/null 2>&1; then
	    unet_cpath=`echo "$unet_cv_with_cpath" | sed "s%^$unet_cv_with_chroot%%"`
	else
	    AC_MSG_ERROR([Configuration file $unet_cv_with_cpath not relative to root directory $unet_cv_with_chroot])
	fi
    else
	unet_cpath=$unet_cv_with_cpath
    fi
else
    unet_cpath=$unet_cv_with_cpath
fi

AC_DEFINE_UNQUOTED(CPATH, "$unet_cpath", [Configuration file name])

dnl --with-lpath allows us to specify the default debugging log file
AC_MSG_CHECKING([where to put the debugging log if debugging enabled])
AC_ARG_WITH([lpath],
[  --with-lpath=file       Set the debugging log file],
[unet_cv_with_lpath=$with_lpath],
[AC_CACHE_VAL(unet_cv_with_lpath,
[unet_cv_with_lpath="ircd.log"])])

if test x"$unet_cv_with_lpath" = xyes -o x"$unet_cv_with_lpath" = xno; then
    unet_cv_with_lpath="ircd.log"
fi

AC_MSG_RESULT([$unet_cv_with_lpath])

if echo "$unet_cv_with_lpath" | grep '^/' > /dev/null 2>&1; then
    # Absolute path; check against chroot stuff
    if test x"$unet_cv_with_chroot" != xno; then
	if echo "$unet_cv_with_lpath" | grep "^$unet_cv_with_chroot" > /dev/null 2>&1; then
	    unet_lpath=`echo "$unet_cv_with_lpath" | sed "s%^$unet_cv_with_chroot%%"`
	else
	    AC_MSG_WARN([Log file $unet_cv_with_lpath not relative to root directory $unet_cv_with_chroot; using default ircd.log instead])
	    unet_cv_with_lpath="ircd.log"
	    unet_lpath="ircd.log"
	fi
    else
	unet_lpath=$unet_cv_with_lpath
    fi
else
    unet_lpath=$unet_cv_with_lpath
fi

AC_DEFINE_UNQUOTED(LPATH, "$unet_lpath", [Path to debugging log file])

dnl --with-maxcon allows us to set the maximum connections
unet_maxcon=`ulimit -Hn`
if test x"$unet_maxcon" = xunlimited; then
    unet_maxcon=`ulimit -Sn`
fi
unet_maxcon=`expr $unet_maxcon - 4`
AC_MSG_CHECKING([max connections])
AC_ARG_WITH([maxcon],
[  --with-maxcon=maxcon    Maximum number of connections server will accept],
[unet_cv_with_maxcon=$with_maxcon],
[AC_CACHE_VAL(unet_cv_with_maxcon,
[unet_cv_with_maxcon=$unet_maxcon])])

if test x"$unet_cv_with_maxcon" = xyes -o x"$unet_cv_with_maxcon" = xno; then
    unet_cv_with_maxcon=$unet_maxcon
fi

AC_MSG_RESULT([$unet_cv_with_maxcon])

AC_DEFINE_UNQUOTED(MAXCONNECTIONS, $unet_cv_with_maxcon,
[Maximum number of network connections])

dnl Finally really generate all output files:
AC_OUTPUT(Makefile ircd/Makefile ircd/test/Makefile doc/Makefile, [echo timestamp > stamp-h])

dnl Report configuration
AC_OUTPUT_COMMANDS([echo "
ircu is now hopefully configured for your system.

  Host system:         $host_os
  Prefix:              $prefix
  Asserts:             $unet_cv_enable_asserts
  Warnings:            $unet_cv_enable_warnings
  Debug:               $unet_cv_enable_debug
  Profile:             $unet_cv_enable_profile
  Owner/mode:          $unet_cv_with_owner.$unet_cv_with_group ($unet_cv_with_mode)
  Chroot:              $unet_cv_with_chroot

  Domain:              $unet_cv_with_domain
  DPath:               $unet_cv_with_dpath
  CPath:               $unet_cv_with_cpath
  LPath:               $unet_cv_with_lpath
  Maximum connections: $unet_cv_with_maxcon

  poll() engine:       $unet_cv_enable_poll
  kqueue() engine:     $unet_cv_enable_kqueue
  /dev/poll engine:    $unet_cv_enable_devpoll
  epoll() engine:      $unet_cv_enable_epoll
"])
