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$

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

AC_PROG_CC_C99

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

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 check for /dev/null so we can use it to hold evil fd's
AC_MSG_CHECKING([for /dev/null])
if test -c /dev/null ; then
        AC_DEFINE(PATH_DEVNULL, "/dev/null", [Path to /dev/null])
        AC_MSG_RESULT(yes)
else
        AC_DEFINE(PATH_DEVNULL, "devnull.log", [Path to /dev/null])
        AC_MSG_RESULT(no - using devnull.log)
fi

dnl **
dnl **  SSL Library checks (OpenSSL)
dnl **
AC_MSG_CHECKING([whether to enable OpenSSL support])
AC_ARG_ENABLE([ssl],
[  --disable-ssl           Disable Secure Sockets Layer support],
[unet_cv_enable_ssl=$enable_ssl],
[AC_CACHE_VAL(unet_cv_enable_ssl,
[unet_cv_enable_ssl=yes])])
AC_MSG_RESULT([$unet_cv_enable_ssl])

if test x"$unet_cv_enable_ssl" = xyes; then
  AC_MSG_CHECKING([for OpenSSL includes])
  AC_ARG_WITH([openssl-includes],
  AS_HELP_STRING([--with-openssl-includes=dir], [Specify location of OpenSSL header files (default: /usr/include)]),
  [base_ssl_inc=$withval],
  [base_ssl_inc=/usr/include])
  [unet_cv_with_openssl_inc_prefix=$base_ssl_inc]
  AC_MSG_RESULT([$unet_cv_with_openssl_inc_prefix])
  AC_DEFINE_UNQUOTED(SSL_INCLUDES_PATH, "$base_ssl_inc", [Path name used as a base for the ssl include files.])

  AC_MSG_CHECKING([for OpenSSL libraries])
  AC_ARG_WITH([openssl-libs],
  AS_HELP_STRING([--with-openssl-libs=dir], [Specify location of OpenSSL libs (default: /usr/lib)]),
  [unet_cv_with_openssl_prefix=$withval],
  [unet_cv_with_openssl_prefix=/usr/lib])
  AC_MSG_RESULT([$unet_cv_with_openssl_prefix])
  AC_DEFINE_UNQUOTED(SSL_LIBS_PATH, "$unet_cv_with_openssl_prefix", [Path name used as a base for the ssl lib files.])

  save_CFLAGS=$CFLAGS
  save_LIBS=$LIBS

  CFLAGS="-I$unet_cv_with_openssl_inc_prefix -lcrypto"
  LIBS="-Wl,-rpath=$unet_cv_with_openssl_prefix -L$unet_cv_with_openssl_prefix -lssl -lcrypto"

  AC_CHECK_LIB(ssl, SSL_read, [
    AC_CHECK_LIB(crypto, X509_new, [
     AC_CHECK_LIB(crypto, EVP_sha256, [
        AC_CHECK_HEADERS($base_ssl_inc/openssl/ssl.h $base_ssl_inc/openssl/err.h, [
          enable_ssl="yes";
          OPENSSL_LDFLAGS="-lssl -lcrypto"
        ])
      ])
    ])
  ])

  LIBS=$save_LIBS
  CFLAGS=$save_CFLAGS

  if test "x$enable_ssl" = xyes; then
    AC_DEFINE([USE_SSL], , [Define if you are using OpenSSL])
    LIBS="$LIBS -Wl,-rpath=$unet_cv_with_openssl_prefix -L$unet_cv_with_openssl_prefix $OPENSSL_LDFLAGS"
    CFLAGS="$CFLAGS -I$unet_cv_with_openssl_inc_prefix"
  else
    AC_MSG_ERROR([Unable to find OpenSSL, Maybe you need to install the openssl and libssl-dev package, or use --with-openssl-includes and --with-openssl-libs if you have openssl installed in an odd location])
  fi
fi

dnl **
dnl **  GeoIP checks (GeoIP)
dnl **
AC_MSG_CHECKING([whether to enable GeoIP support])
AC_ARG_ENABLE([geoip],
[  --disable-geoip         Disable GeoIP support],
[unet_cv_enable_geoip=$enable_geoip],
[AC_CACHE_VAL(unet_cv_enable_geoip,
[unet_cv_enable_geoip=yes])])
AC_MSG_RESULT([$unet_cv_enable_geoip])

if test x"$unet_cv_enable_geoip" = xyes; then
  AC_ARG_WITH([geoip],
  AS_HELP_STRING([--with-geoip=dir], [Specify the installation prefix of GeoIP (default: /usr/local)]),
  [base_geoip_prefix=$withval],
  [base_geoip_prefix=/usr/local])

  AC_MSG_CHECKING([for GeoIP includes])
  AC_ARG_WITH([geoip-includes],
  AS_HELP_STRING([--with-geoip-includes=dir], [Specify location of GeoIP header files (default: /usr/local/include)]),
  [base_geoip_inc=$withval],
  [base_geoip_inc=$base_geoip_prefix/include])
  [unet_cv_with_geoip_inc_prefix=$base_geoip_inc]
  AC_MSG_RESULT([$unet_cv_with_geoip_inc_prefix])
  AC_DEFINE_UNQUOTED(GEOIP_INCLUDES_PATH, "$base_geoip_inc", [Path name used as a base for the GeoIP include files.])

  AC_MSG_CHECKING([for GeoIP libraries])
  AC_ARG_WITH([geoip-libs],
  AS_HELP_STRING([--with-geoip-libs=dir], [Specify location of GeoIP libs (default: /usr/local/lib)]),
  [base_geoip_lib=$withval],
  [base_geoip_lib=$base_geoip_prefix/lib])
  [unet_cv_with_geoip_prefix=$base_geoip_lib]
  AC_MSG_RESULT([$unet_cv_with_geoip_prefix])
  AC_DEFINE_UNQUOTED(GEOIP_LIBS_PATH, "$unet_cv_with_geoip_prefix", [Path name used as a base for the GeoIP lib files.])

  save_CFLAGS=$CFLAGS
  save_LIBS=$LIBS

  CFLAGS="-I$unet_cv_with_geoip_inc_prefix"
  LIBS="-L$unet_cv_with_geoip_prefix"

  AC_CHECK_HEADERS($unet_cv_with_geoip_inc_prefix/GeoIP.h, [
    AC_CHECK_LIB(GeoIP, GeoIP_id_by_addr_gl, [
      AC_CHECK_LIB(GeoIP, GeoIP_id_by_addr_v6_gl, [
        enable_geoip="yes";
        enable_geoip_gl="yes";
        GEOIP_LDFLAGS="-lGeoIP"
      ])
    ], [
      AC_CHECK_LIB(GeoIP, GeoIP_id_by_addr, [
        AC_CHECK_LIB(GeoIP, GeoIP_id_by_addr_v6, [
          enable_geoip="yes";
          GEOIP_LDFLAGS="-lGeoIP"
        ])
      ])
    ])
  ])

  LIBS=$save_LIBS
  CFLAGS=$save_CFLAGS

  if test "x$enable_geoip" = xyes; then
    AC_DEFINE([USE_GEOIP], , [Define if you are using GeoIP])
    if test "x$enable_geoip_gl" = xyes; then
      AC_DEFINE([USE_GEOIP_GL], , [Define if you are using GeoIP with *_gl() functions])
    fi
    LIBS="$LIBS -L$unet_cv_with_geoip_prefix $GEOIP_LDFLAGS"
    CFLAGS="$CFLAGS -I$unet_cv_with_geoip_inc_prefix"
  else
     AC_MSG_WARN([Unable to find GeoIP, GeoIP features will not work without libGeoIP. Disabling GeoIP support.])
  fi
fi

dnl **
dnl **  MaxMindDB checks (GeoIP)
dnl **
AC_MSG_CHECKING([whether to enable MaxMindDB support])
AC_ARG_ENABLE([mmdb],
[  --disable-mmdb         Disable MaxMindDB support],
[unet_cv_enable_mmdb=$enable_mmdb],
[AC_CACHE_VAL(unet_cv_enable_mmdb,
[unet_cv_enable_mmdb=yes])])
AC_MSG_RESULT([$unet_cv_enable_mmdb])

if test x"$unet_cv_enable_mmdb" = xyes; then
  AC_ARG_WITH([mmdb],
  AS_HELP_STRING([--with-mmdb=dir], [Specify the installation prefix of MaxMindDB (default: /usr/local)]),
  [base_mmdb_prefix=$withval],
  [base_mmdb_prefix=/usr/local])

  AC_MSG_CHECKING([for MaxMindDB includes])
  AC_ARG_WITH([mmdb-includes],
  AS_HELP_STRING([--with-mmdb-includes=dir], [Specify location of MaxMindDB header files (default: /usr/local/include)]),
  [base_mmdb_inc=$withval],
  [base_mmdb_inc=$base_mmdb_prefix/include])
  [unet_cv_with_mmdb_inc_prefix=$base_mmdb_inc]
  AC_MSG_RESULT([$unet_cv_with_mmdb_inc_prefix])
  AC_DEFINE_UNQUOTED(MMDB_INCLUDES_PATH, "$base_mmdb_inc", [Path name used as a base for the MaxMindDB include files.])

  AC_MSG_CHECKING([for MaxMindDB libraries])
  AC_ARG_WITH([mmdb-libs],
  AS_HELP_STRING([--with-mmdb-libs=dir], [Specify location of MaxMindDB libs (default: /usr/local/lib)]),
  [base_mmdb_lib=$withval],
  [base_mmdb_lib=$base_mmdb_prefix/lib])
  [unet_cv_with_mmdb_prefix=$base_mmdb_lib]
  AC_MSG_RESULT([$unet_cv_with_mmdb_prefix])
  AC_DEFINE_UNQUOTED(MMDB_LIBS_PATH, "$unet_cv_with_mmdb_prefix", [Path name used as a base for the MaxMindDB lib files.])

  save_CFLAGS=$CFLAGS
  save_LIBS=$LIBS

  CFLAGS="-I$unet_cv_with_mmdb_inc_prefix"
  LIBS="-L$unet_cv_with_mmdb_prefix"

  AC_CHECK_HEADERS($unet_cv_with_mmdb_inc_prefix/maxminddb.h, [
    AC_CHECK_LIB(maxminddb, MMDB_lookup_string, [
      AC_CHECK_LIB(maxminddb, MMDB_get_value, [
        enable_mmdb="yes";
        MMDB_LDFLAGS="-lmaxminddb"
      ])
    ])
  ])

  LIBS=$save_LIBS
  CFLAGS=$save_CFLAGS

  if test "x$enable_mmdb" = xyes; then
    AC_DEFINE([USE_MMDB], , [Define if you are using MaxMindDB])
    LIBS="$LIBS -L$unet_cv_with_mmdb_prefix $MMDB_LDFLAGS"
    CFLAGS="$CFLAGS -I$unet_cv_with_mmdb_inc_prefix"
  else
     AC_MSG_WARN([Unable to find MaxMindDB, MaxMindDB features will not work without libmaxminddb. Disabling MaxMindDB support.])
  fi
fi

dnl --with-maxcon allows us to set the maximum connections
unet_maxcon=`ulimit -Sn`
if test x"$unet_maxcon" = xunlimited; then
    unet_maxcon=1024
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
    if test "$unet_maxcon" -lt 32; then
      AC_MSG_ERROR([Maximum connections (number of open files minus 4) must be at least 32.])
    fi
    unet_cv_with_maxcon=$unet_maxcon
elif test "$unet_cv_with_maxcon" -lt 32; then
    AC_MSG_ERROR([Maximum connections (--with-maxcon) must be at least 32.])
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, [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
"])
