# configure.in for ircii
dnl
dnl Copyright (c) 1993-2003 Matthew R. Green.
dnl All rights reserved.
dnl
dnl Redistribution and use in source and binary forms, with or without
dnl modification, are permitted provided that the following conditions
dnl are met:
dnl 1. Redistributions of source code must retain the above copyright
dnl    notice, this list of conditions and the following disclaimer.
dnl 2. Redistributions in binary form must reproduce the above copyright
dnl    notice, this list of conditions and the following disclaimer in the
dnl    documentation and/or other materials provided with the distribution.
dnl 3. The name of the author may not be used to endorse or promote products
dnl    derived from this software without specific prior written permission.
dnl
dnl THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
dnl IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
dnl OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
dnl IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
dnl INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
dnl BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
dnl LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
dnl AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
dnl OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
dnl OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
dnl SUCH DAMAGE.
dnl
dnl certain portions have other attributions, see below.
dnl
dnl thanks to the screen 3.3 configure.in for giving me examples to
dnl work from and steal ;)
dnl
dnl @(#)$eterna: configure.in,v 1.186 2006/07/25 11:11:05 mrg Exp $

AC_REVISION($Revision: 1.186 $)dnl
AC_INIT(${srcdir}/source/irc.c)
AC_CONFIG_HEADER(${objdir}/defs.h:include/defs.h.in)

# So many systems seem to need this that it is better do it here automatically.
if test "x$prefix" != xNONE; then
  if test -d "$prefix/lib"; then
    LIBS="-L${prefix}/lib $LIBS"
  fi
else
  if test -d "$ac_default_prefix/lib"; then
    LIBS="-L${ac_default_prefix}/lib $LIBS"
  fi
fi

dnl
dnl grok the version number - from source/irc.c
dnl

VERSION=`sed -n -e 's/"[[^"]]*$//' -e '/#define[ 	]*IRCII_VERSION[ 	]*"/s///p' -e '/#define IRCII_VERSION/q' < ${srcdir}/source/irc.c` 
echo this is ircii version $VERSION
echo
AC_SUBST(VERSION)

CATEGORIES='build change-request doc pending runtime'
AC_SUBST(CATEGORIES)

dnl so that the generated configure has an id.
echo '$eterna: configure.in,v 1.186 2006/07/25 11:11:05 mrg Exp $' > /dev/null

dnl
dnl initial stuff
dnl

AC_PROG_CC
AC_PROG_CPP
AC_ISC_POSIX

dnl
dnl setup variables
dnl
srcdir=`(cd $srcdir; pwd)`
AC_SUBST(srcdir)
objdir=`pwd`
AC_SUBST(objdir)

dnl
dnl check for NeXT.. and -bsd switch
dnl

AC_MSG_CHECKING(for NeXT)
AC_EGREP_CPP(yes,
[#if defined(NeXT) || defined(__NeXT) || defined(__NeXT__)
  yes
#endif
],nextstep=1)

if test -n "$nextstep"; then
  AC_MSG_RESULT(yes)
else
  AC_MSG_RESULT(no)
fi

dnl AC_AIX is broken.  blah.

AC_MSG_CHECKING(for AIX)
AC_EGREP_CPP(yes,
[
#ifdef _AIX
  yes
#endif
],AC_MSG_RESULT(yes)
  aix=1
  AC_DEFINE([_ALL_SOURCE]),
  AC_MSG_RESULT(no))

AC_PROG_GCC_TRADITIONAL
if test $ac_cv_prog_gcc = yes; then
  CFLAGS="-pipe $CFLAGS"
fi
dnl i think this is for (old) irix
dnl AC_CHECK_LIB(sun, getpwnam)

AC_MSG_CHECKING(for qnx)
case "`uname -s`" in
dnl bleck - wish i could ditch this qnx test, but we need to bump the stack
  QNX*)
    AC_MSG_RESULT(yes)
    if test -z "$GCC"; then
      LDFLAGS="-N256k -M"
      CFLAGS="-w4 -Otax -zc -Wc,-fr= -Wc,-wcd=121"
    fi
    ;;
  *)
    AC_MSG_RESULT(no)
esac
AC_MSG_CHECKING(for solaris)
if /bin/sun 2> /dev/null
then
  uname=`uname -r`
  case "$uname" in
    4.*)
      AC_MSG_RESULT(no) 
      sunos4=1
      ;;
    5.*)
      AC_MSG_RESULT(yes)
      svr4=1
      ;;
  esac
else
  AC_MSG_RESULT(no)
  AC_MSG_CHECKING(for SVR4)
  AC_EGREP_CPP(yes,
[#if defined(SVR4) || defined(__svr4__) || defined(__SVR4)
  yes
#endif
],AC_MSG_RESULT(yes)
  svr4=1)
fi

if test -n "$svr4"
then
  LIBS="-L/usr/ccs/lib -lnsl -lsocket $LIBS"
else
  if test ! -n "$sunos4"
  then
    AC_MSG_RESULT(no)
  fi
fi

dnl
dnl testing for svr3/wollengong tcp's /usr/netinclude
dnl

AC_MSG_CHECKING(for -I/usr/netinclude)
if test -d /usr/netinclude; then
  AC_MSG_RESULT(yes)
  CFLAGS="$CFLAGS -I/usr/netinclude"
else
  AC_MSG_RESULT(no)
fi

dnl
dnl check for hpux, and hpux version
dnl

AC_MSG_CHECKING(for hpux version)
if test -f /hp-ux -o -f /stand/vmunix; then
  AC_MSG_RESULT(yes)
  hpux=1
  uname=`uname -r` 2> /dev/null
  case $uname in
	A.09* | A.9* | a.9* | a.09* | 9* | 09* | B.09*)
		;;
	A.08* | A.8* | a.8* | a.08* | 8* | 08*)
		AC_DEFINE([HPUX8])
		;;
	A.07* | A.B7* | A.7* | a.7* | a.07* | 7* | 07*)
		AC_DEFINE([HPUX7])
		;;
	*)
		AC_DEFINE([HPUXUNKNOWN])
		;;
  esac
else
  AC_MSG_RESULT(no)
fi

AC_MSG_CHECKING(for ultrix)
AC_EGREP_CPP(yes,
[#if defined(ultrix) || defined(__ultrix) || defined(__ultrix)
  yes
#endif
], ultrix=1)
if test -n "$ultrix"; then
  AC_MSG_RESULT(yes)
  test -z "$GCC" && CC="$CC -YBSD"
else
  AC_MSG_RESULT(no)
fi

dnl
dnl linux ncurses
dnl
case "`uname -s`" in
  Linux*)
    if test -d /usr/include/ncurses; then
      CFLAGS="-I/usr/include/ncurses $CFLAGS"
    fi
    ;;
esac

AC_MSG_CHECKING(for sequent/ptx)
AC_EGREP_CPP(yes,
[#ifdef _SEQUENT_
  yes
#endif
], LIBS="$LIBS -lseq" seqptx=1 AC_MSG_RESULT(yes), AC_MSG_RESULT(no))

AC_CHECK_HEADERS(sys/un.h sys/select.h sys/fcntl.h sys/ioctl.h sys/file.h sys/time.h sys/wait.h sys/twg_config.h sys/ptem.h sys/uio.h)dnl sys/ ones
AC_CHECK_HEADERS(unistd.h fcntl.h string.h memory.h netdb.h limits.h crypt.h)dnl non sys/ ones
AC_CHECK_HEADERS(process.h termcap.h iconv.h)dnl others
if test -n "$ultrix" -o -n "$nextstep"; then
  # POSIX termios is broken on Ultrix so don't look for it.
  # NEXTSTEP POSIX is broken so don't look for it.
  AC_CHECK_HEADERS(termio.h sgtty.h, break)dnl sigh.
else
  AC_CHECK_HEADERS(termios.h termio.h sgtty.h, break)dnl sigh.
fi

AC_CHECK_FUNC(fchmod,, AC_DEFINE([NEED_FCHMOD]))
AC_CHECK_FUNC(getcwd,, AC_DEFINE([NEED_GETCWD]))
AC_CHECK_FUNC(getpgid, AC_DEFINE([HAVE_GETPGID]),)
AC_CHECK_FUNC(getsid, AC_DEFINE([HAVE_GETSID]),)
AC_CHECK_FUNC(memmove, AC_DEFINE([HAVE_MEMMOVE]),)
AC_CHECK_FUNC(scandir, AC_DEFINE([HAVE_SCANDIR]),)
AC_CHECK_FUNC(setsid, AC_DEFINE([HAVE_SETSID]),)
AC_CHECK_FUNC(strerror,, AC_DEFINE([NEED_STRERROR]))
AC_CHECK_FUNC(strftime, AC_DEFINE([HAVE_STRFTIME]),)
AC_CHECK_FUNC(uname, AC_DEFINE([HAVE_UNAME]),)
AC_CHECK_FUNC(writev, AC_DEFINE([HAVE_WRITEV]),)
AC_CHECK_FUNC(vasprintf, AC_DEFINE([HAVE_VASPRINTF]),)
AC_CHECK_FUNC(snprintf, AC_DEFINE([HAVE_SNPRINTF]),)
AC_CHECK_FUNC(vsnprintf, AC_DEFINE([HAVE_VSNPRINTF]),)
AC_CHECK_FUNC(fputc, AC_DEFINE([HAVE_FPUTC]),)
AC_CHECK_FUNC(fwrite, AC_DEFINE([HAVE_FWRITE]),)
AC_CHECK_FUNC(setenv, AC_DEFINE([HAVE_SETENV]),)
AC_CHECK_FUNC(unsetenv, AC_DEFINE([HAVE_UNSETENV]),)
AC_CHECK_FUNC(gethostbyname2, AC_DEFINE([HAVE_GETHOSTBYNAME2]),)
AC_CHECK_FUNC(getaddrinfo, AC_DEFINE([HAVE_GETADDRINFO]),)
AC_CHECK_FUNC(getnameinfo, AC_DEFINE([HAVE_GETNAMEINFO]),)
AC_CHECK_FUNC(inet_pton, AC_DEFINE([HAVE_INET_PTON]),)
AC_CHECK_FUNC(inet_ntop, AC_DEFINE([HAVE_INET_NTOP]),)
AC_CHECK_FUNC(tzset, AC_DEFINE([HAVE_TZSET]),)
AC_CHECK_FUNC(iconv_open, AC_DEFINE([HAVE_ICONV_OPEN]),)

AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_DIRENT

AC_TYPE_SIGNAL
AC_TYPE_MODE_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_CHECK_TYPE(ssize_t,int)

if test x$ac_cv_header_sys_twg_config_h = xyes
then
  AC_DEFINE([WINS])
fi

AC_MSG_TRY_COMPILE(for sockaddr_in.sin_len, ircii_cv_HAVE_SOCKADDR_SA_LEN, [
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> ],
  [ struct sockaddr_in sin;
    int X = sin.sin_len ],
  [AC_DEFINE([HAVE_SOCKADDR_SA_LEN])])

AC_MSG_TRY_COMPILE(for struct addrinfo, ircii_cv_HAVE_ADDRINFO, [
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h> ],
  [ struct addrinfo X ],
  [AC_DEFINE([HAVE_ADDRINFO])])

AC_MSG_TRY_COMPILE(for socklen_t, ircii_cv_HAVE_SOCKLEN_T, [
#include <sys/types.h>
#include <sys/socket.h> ], [ socklen_t X ], [AC_DEFINE([HAVE_SOCKLEN_T])])

dnl
dnl check for struct linger
dnl

AC_MSG_CHECKING(for struct linger)
AC_EGREP_HEADER([struct( |	)*linger], sys/socket.h, AC_MSG_RESULT(yes), AC_DEFINE([NO_STRUCT_LINGER]) AC_MSG_RESULT(no, ugh.))

dnl
dnl grok process groups
dnl

if test x$ac_cv_func_getsid != xyes && test x$ac_cv_func_getpgid != xyes
then
  AC_MSG_CHECKING(if getpgrp takes an argument)
  AC_TRY_RUN([
main()
{
  int pid;
  int pgid;

  pid = fork();

  if (0 == pid)
  {
#ifdef HAVE_SETSID
    setsid();
#else
    setpgrp(0, getpid());
#endif /* HAVE_SETSID */
    sleep(10);
  }
  else
  {
    sleep(2);
    pgid = getpgrp(pid);
    kill(pid,9);
    if (pid != pgid)
      exit(1);
  }
  exit(0);
}
],
  AC_MSG_RESULT(yes),
  AC_MSG_RESULT(no your getpgrp is painful)
  AC_DEFINE([BROKEN_GETPGRP]), :)
fi

dnl
dnl work out wait type..
dnl

AC_CHECK_FUNC(waitpid,,
  AC_MSG_RESULT(no)
  AC_MSG_CHECKING(for bsd wait)
  AC_TRY_LINK([
#include <sys/types.h>
#include <sys/wait.h>
],[
  union wait x;
  int y;
#ifdef WEXITSTATUS
  y = WEXITSTATUS(x);
#endif
],
    AC_DEFINE([BSDWAIT])
    bsdwait=1
    AC_MSG_RESULT(yes),
    AC_MSG_RESULT(no)
    AC_DEFINE([NEED_WAITPID])))

dnl
dnl checking for select()
dnl

olibs="$LIBS"
AC_CHECK_FUNC(select, ,
  AC_CHECK_LIB(socket, select, LIBS="$LIBS -lsocket",
    AC_CHECK_LIB(nsl, select, LIBS="$LIBS -lnsl",
      AC_CHECK_LIB(inet, select, LIBS="$LIBS -linet",
        AC_CHECK_LIB(cposix, select, LIBS="$LIBS -lcposix",
          AC_CHECK_LIB(net, select, LIBS="$LIBS -lnet",
            AC_MSG_WARN(i can not find select.  you might need to help me)))))))

dnl
dnl termcap or curses
dnl if termcap, check for tputs declaration
dnl
if test x$aix = x1
then
  LIBS="$LIBS -lcurses"
  AC_DEFINE([INCLUDE_CURSES_H])
else
  olibs="$LIBS"
  AC_CHECK_LIB(termcap, tgetent, LIBS="-ltermcap $olibs",
    AC_CHECK_LIB(tinfo, tgetent, LIBS="-ltinfo $olibs",
      AC_CHECK_LIB(termlib, tgetent, LIBS="$olibs -ltermlib",
        AC_CHECK_LIB(curses, tgetent, LIBS="$olibs -lcurses",
          AC_CHECK_LIB(xtermcap, tgetent, LIBS="$olibs -lxtermcap",
            AC_CHECK_LIB(ncurses, tgetent, LIBS="$olibs -lncurses",
              AC_MSG_WARN(i can not find tgetent.  you might need to help me)))))))
  AC_MSG_CHECKING(for tputs in curses.h)
  AC_EGREP_CPP([tputs( |	|\()],[#include <curses.h>
], AC_DEFINE([INCLUDE_CURSES_H]) AC_MSG_RESULT(yes), blahhack=1; AC_MSG_RESULT(no))
fi

dnl <term.h> is broken on most systems; ignoreit.

dnl if test x$blahhack = x1
dnl then
dnl   AC_MSG_CHECKING(for tputs in term.h)
dnl   AC_EGREP_CPP([tputs( |	|\()],[#include <term.h>
dnl ], AC_DEFINE([INCLUDE_TERM_H]) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
dnl fi

dnl
dnl signal handling
dnl

AC_CHECKING(signal implementation)
AC_CHECK_FUNC(sigaction, AC_DEFINE([USE_SIGACTION])
  echo using posix signals
  ,
  AC_CHECK_FUNC(sigset, AC_DEFINE([USE_SIGSET])
  echo using ok system v signals
  ,

echo checking for reliable signals
AC_TRY_RUN([
#include <sys/types.h>
#include <signal.h>

#ifndef SIGCHLD
# define SIGCHLD SIGCLD
#endif
#ifdef USE_SIGSET
# define signal sigset
#endif

int got;

#ifdef SIGVOID
void
#endif
hand()
{
  got++;
}

main()
{
  (void)signal(SIGCHLD, hand);
  kill(getpid(), SIGCHLD);
  kill(getpid(), SIGCHLD);
  if (got < 2)
    exit(1);
  exit(0);
}
], 
echo using bsd signals
, AC_DEFINE([SYSVSIGNALS])
echo using system v signals, you lose.
)))

if test x$bsdwait = x1
then
  AC_MSG_CHECKING(for wait3 declaration)
  AC_EGREP_CPP([wait3( |	|\()],[#include <sys/types.h> dnl )
#include <signal.h>
#include <sys/wait.h>
], AC_DEFINE([WAIT3_DECLARED]) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
else
  AC_MSG_CHECKING(for waitpid declaration)
  AC_EGREP_CPP([waitpid( |	|\()],[#include <sys/types.h> dnl )
#include <signal.h>
#include <sys/wait.h>
], AC_DEFINE([WAITPID_DECLARED]) AC_MSG_RESULT(yes), AC_MSG_RESULT(no))
fi

dnl
dnl check for errno declaration
dnl

AC_MSG_CHECKING(for errno declaration)
AC_TRY_RUN([
#include <sys/types.h>
#include <errno.h>
main()
{
  int i = errno;
  exit(0);
}
],
  AC_MSG_RESULT(yes)
  AC_DEFINE([ERRNO_DECLARED]),
  AC_MSG_RESULT(no), :)

dnl
dnl check for sys_errlist declaration
dnl

AC_MSG_CHECKING(for sys_errlist declaration)
AC_TRY_RUN([
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
main()
{
  char *s = sys_errlist[0];
  exit(0);
}
],
  AC_MSG_RESULT(yes)
  AC_DEFINE([SYS_ERRLIST_DECLARED]),
  AC_MSG_RESULT(no), :)


dnl
dnl check the type of iconv(3)'s 2nd argument
dnl

AC_MSG_CHECKING(for const iconv 2nd argument)
AC_EGREP_HEADER([\\<iconv\\>.*\\(.*const], iconv.h,
  AC_MSG_RESULT(yes)
  AC_DEFINE([ICONV_CONST_ARG2]),
  AC_MSG_RESULT(no))


dnl
dnl need this for dcc stuff
dnl

AC_MSG_CHECKING(for a 32 bit integer)
AC_TRY_RUN([
main()
{
  if (4 == sizeof(unsigned long))
    exit(0);
  exit(1);
}
],
  AC_MSG_RESULT(unsigned long)
  AC_DEFINE([UNSIGNED_LONG32]),
  AC_TRY_RUN([
main()
{
  if (4 == sizeof(unsigned int))
    exit(0);
  exit(1);
}
],
  AC_MSG_RESULT(unsigned int)
  AC_DEFINE([UNSIGNED_INT32]),
  AC_MSG_RESULT(none, using unsigned long)
  AC_DEFINE([UNKNOWN_32INT]), :),
:)

dnl
dnl libraries
dnl

dnl i think this was only needed for solaris with -lucb.
dnl AC_HAVE_LIBRARY(elf, LIBS="$LIBS -lelf",)
dnl AC_HAVE_LIBRARY(nls, libnls=1;AC_DEFINE([HAVE_LIB_NLS]),)
dnl i think this is redudant - we check for -lsocket above
dnl if test ! -n "$libsocket"; then
dnl 	AC_CHECK_LIB(socket, socket, LIBS="$LIBS -lsocket",)
dnl fi
dnl this is for isc.  need the nsl_s library as well.
if test ! -n "$libinet"; then
	AC_CHECK_LIB(inet, socket, libnsl=1; LIBS="$LIBS -linet -lnsl_s",)
fi
AC_CHECK_FUNC(gethostname, ,
       AC_CHECK_LIB(nsl, gethostname, LIBS="$LIBS -lnsl",) )
AC_CHECK_LIB(dgc, inet_addr, LIBS="$LIBS -ldgc",)

dnl
dnl check for non-blocking fd style available..
dnl

find_non_blocking=
AC_MSG_CHECKING(for non-blocking)
AC_ARG_WITH(non-blocking,
[  --with-non-blocking=[TYPE] Use TYPE non-blocking, from posix, sysv,
                        bsd, or none],
[ case "$withval" in
  no|none)
    AC_MSG_RESULT(no)
    ;;		
  posix)
    AC_MSG_RESULT(posix)
    AC_DEFINE([NBLOCK_POSIX])
    ;;
  bsd)
    AC_MSG_RESULT(bsd)
    AC_DEFINE([NBLOCK_BSD])
    ;;
  sysv)
    AC_MSG_RESULT(sysv)
    AC_DEFINE([NBLOCK_SYSV])
    ;;
  *)
    find_non_blocking=1
    ;;
  esac ],find_non_blocking=1)

if test -n "$find_non_blocking"; then
  changequote(<<, >>)dnl
<<
precode='#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <signal.h>

alarmed()
{
	exit(1);
}

main()
{
	char b[12], x[32];
	int f, l = sizeof(x);
	f = socket(AF_INET, SOCK_DGRAM, 0);
	if (f >= 0 && (fcntl(f, F_SETFL,'
postcode=') != -1)) {
		signal(SIGALRM, alarmed);
		alarm(3);
		recvfrom(f, b, 12, 0, (struct sockaddr *)x, /*(socklen_t)*/&l);
		alarm(0);
		exit(0);
	}
	exit(1);
}'
>>
  changequote([, ])dnl

  code="$precode O_NONBLOCK $postcode"

  AC_TRY_RUN($code,
    AC_DEFINE([NBLOCK_POSIX])
    AC_MSG_RESULT(posix),
    code="$precode O_NDELAY $postcode"
    AC_TRY_RUN($code,
      AC_DEFINE([NBLOCK_BSD])
      AC_MSG_RESULT(bsd),
      code="$precode FIONBIO $postcode"
      AC_TRY_RUN($code,
        AC_DEFINE([NBLOCK_SYSV])
        AC_MSG_RESULT(system v),
        AC_MSG_WARN(i can't find a working non blocking system),
      :), :), :)
fi

dnl work out UNIX mail dir..

AC_MSG_CHECKING(for unix mail directory)

for foo in /var/spool/mail /usr/spool/mail /var/mail /usr/mail
do
  if test -d $foo; then
    mdir=$foo
    break
  fi
done

if test -n "$mdir"; then
  UNIX_MAIL=\"$mdir\"
  AC_MSG_RESULT($mdir)
  AC_DEFINE_UNQUOTED([UNIX_MAIL], ["$mdir"])
else
  AC_MSG_RESULT(none)
  AC_MSG_CHECKING(for AMS mail)
  if test "x$HOME" != "/" -o -z "x$HOME" && test -d "$HOME/Mailbox"; then
    AC_MSG_RESULT(using Mailbox)
    AC_DEFINE_UNQUOTED([AMS_MAIL], ["Mailbox"])
  else
    AC_MSG_RESULT(no)
  fi
fi


if test -z "$CFLAGS"; then CFLAGS=-O ; fi
if test -z "$LDFLAGS"; then LDFLAGS= ; fi
RM="rm -f"
LN="ln -s"

if test ! -n "$bindir";
then
	bindir=\${exec_prefix}/bin
fi
AC_SUBST(bindir)

if test ! -n "$IRCLIB";
then
	IRCLIB=\${datadir}/irc
fi
AC_SUBST(IRCLIB)

if test "x$exec_prefix" = "xNONE"; then
  if test "x$prefix" = "xNONE"; then
    libdir="$ac_default_prefix/lib"
  else
    libdir="$prefix/lib"
  fi
else
  libdir="$exec_prefix/lib"
fi

AC_MSG_CHECKING(for ElectricFence library)
AC_ARG_WITH(efence,
[  --with-efence[=PATH]    Compile with the ElectricFence debugging library.],
[ case "$withval" in
  no)
    AC_MSG_RESULT(no)
    ;;		
  *)
    AC_MSG_RESULT(yes)
    if test "x$withval" = xyes; then
      withval="-lefence"
    else
      if test -d "$withval"; then
        if test -d "$withval/lib"; then
	  withval="-L$withval/lib -lefence"
	else
	  withval="-L$withval -lefence"
	fi
      fi
    fi
    LIBS="$withval $LIBS"
    ;;
  esac ],
  AC_MSG_RESULT(no)
)

AC_MSG_CHECKING(for emacs meta keys)
AC_ARG_WITH(emacs-meta-keys,
[  --with-emacs-meta-keys  Enable Emacs meta key support.],
[ case "$withval" in
  no)
    AC_MSG_RESULT(no)
    ;;		
  *)
    AC_MSG_RESULT(yes)
    AC_DEFINE([WITH_EMACS_META_KEYS])
    ;;
  esac ],
  AC_MSG_RESULT(no)
)

AC_MSG_CHECKING(paranoia status)
AC_ARG_WITH(paranoid,
[  --with-paranoid         Sets the compile-time paranoid flag.],
[ AC_MSG_RESULT(yes)
  AC_DEFINE_UNQUOTED([PARANOID]) ],
  AC_MSG_RESULT(none)
)

AC_MSG_CHECKING(for /dev/urandom)
if test -c /dev/urandom; then
  AC_MSG_RESULT(yes)
  AC_DEFINE_UNQUOTED([HAVE_DEV_RANDOM])
  AC_DEFINE_UNQUOTED([DEV_RANDOM_PATH], ["/dev/urandom"])
else
  AC_MSG_RESULT(no)
  AC_MSG_CHECKING(for /dev/random)
  if test -c /dev/random; then
    AC_MSG_RESULT(yes)
    AC_DEFINE_UNQUOTED([HAVE_DEV_RANDOM])
    AC_DEFINE_UNQUOTED([DEV_RANDOM_PATH], ["/dev/random"])
  else
    AC_MSG_RESULT(no)
  fi
fi

AC_MSG_CHECKING(for default server)
AC_ARG_WITH(default-server,
[  --with-default-server=SERVER[:PORT]     Connect to SERVER by default.],
[ if test "$withval"; then
    AC_MSG_RESULT($withval)
    AC_DEFINE_UNQUOTED([DEFAULT_SERVER], ["$withval"])
  else
    AC_MSG_RESULT(none)
  fi ],
  AC_MSG_RESULT(none)
)

dnl
dnl check for -lcrypt
dnl

AC_CHECK_LIB(c, crypt,
  [AC_DEFINE([HAVE_CRYPT])],
  AC_CHECK_LIB(crypt, crypt,
    [AC_DEFINE([HAVE_CRYPT])
     LIBS="$LIBS -lcrypt"],
    AC_CHECK_LIB(des, des,
      [AC_DEFINE([HAVE_CRYPT])
       LIBS="$LIBS -ldes"])))

dnl
dnl check for SOCKS
dnl

AC_MSG_CHECKING(whether to support SOCKS)
AC_ARG_WITH(socks,
[  --with-socks            Compile with SOCKS firewall traversal support.],
[ case "$withval" in
  no)
    AC_MSG_RESULT(no)
    ;;
  yes)
    AC_MSG_RESULT(yes)
    AC_CHECK_LIB(socks5, SOCKSconnect, [
	    socks=5
	    LIBS="$LIBS -lsocks5"], [
	AC_CHECK_LIB(socks, Rconnect, [
	    socks=4
	    LIBS="$LIBS -lsocks"], [
		AC_MSG_ERROR(Could not find socks library.  You must first install socks.) ] ) ] )
    ;;
  esac ],
  AC_MSG_RESULT(no)
)

if test "x$socks" = "x"; then
	AC_MSG_CHECKING(whether to support SOCKS5)
	AC_ARG_WITH(socks5,
	[  --with-socks5[=PATH]    Compile with SOCKS5 firewall traversal support.],
	[ case "$withval" in
	  no)
	    AC_MSG_RESULT(no)
	    ;;
	  *)
	    AC_MSG_RESULT(yes)
	    socks=5
	    if test "x$withval" = "xyes"; then
	      withval="-lsocks5"
	    else
	      if test -d "$withval"; then
		if test -d "$withval/include"; then
		  CFLAGS="$CFLAGS -I$withval/include"
		else
		  CFLAGS="$CFLAGS -I$withval"
		fi
	        if test -d "$withval/lib"; then
		  withval="-L$withval/lib -lsocks5"
		else
		  withval="-L$withval -lsocks5"
		fi
	      fi
	    fi
	    LIBS="$withval $LIBS"
	    # If Socks was compiled with Kerberos support, we will need
	    # to link against kerberos libraries.  Temporarily append
	    # to LIBS.  This is harmless if there is no kerberos support.
	    TMPLIBS="$LIBS"
	    LIBS="$LIBS $KERBEROS_LIBS"
	    AC_TRY_LINK([],
	                [ SOCKSconnect(); ],
			[],
			[ AC_MSG_ERROR(Could not find the $withval library.  You must first install socks5.) ])
	    LIBS="$TMPLIBS"
	    ;;
	  esac ],
	  AC_MSG_RESULT(no)
	)
fi

if test "x$socks" = "x"; then
	AC_MSG_CHECKING(whether to support SOCKS4)
	AC_ARG_WITH(socks4,
	[  --with-socks4[=PATH]    Compile with SOCKS4 firewall traversal support.],
	[ case "$withval" in
	  no)
	    AC_MSG_RESULT(no)
	    ;;
	  *)
	    AC_MSG_RESULT(yes)
	    socks=4
	    if test "x$withval" = "xyes"; then
	      withval="-lsocks"
	    else
	      if test -d "$withval"; then
	        withval="-L$withval -lsocks"
	      fi
	    fi
	    LIBS="$withval $LIBS"
	    AC_TRY_LINK([],
	                [ Rconnect(); ],
			[],
			[ AC_MSG_ERROR(Could not find the $withval library.  You must first install socks.) ])
	    ;;
	  esac ],
	  AC_MSG_RESULT(no)
	)
fi



if test "x$socks" = "x4"; then
    AC_DEFINE([SOCKS])
    AC_DEFINE([SOCKS4])
    AC_DEFINE([connect], [Rconnect])
    AC_DEFINE([getsockname], [Rgetsockname])
    AC_DEFINE([bind], [Rbind])
    AC_DEFINE([accept], [Raccept])
    AC_DEFINE([listen], [Rlisten])
    AC_DEFINE([select], [Rselect])
fi

if test "x$socks" = "x5"; then
    AC_DEFINE([SOCKS])
    AC_DEFINE([SOCKS5])
    AC_DEFINE([connect], [SOCKSconnect])
    AC_DEFINE([getsockname], [SOCKSgetsockname])
    AC_DEFINE([getpeername], [SOCKSgetpeername])
    AC_DEFINE([bind], [SOCKSbind])
    AC_DEFINE([accept], [SOCKSaccept])
    AC_DEFINE([listen], [SOCKSlisten])
    AC_DEFINE([select], [SOCKSselect])
    AC_DEFINE([recvfrom], [SOCKSrecvfrom])
    AC_DEFINE([sendto], [SOCKSsendto])
    AC_DEFINE([recv], [SOCKSrecv])
    AC_DEFINE([send], [SOCKSsend])
    AC_DEFINE([read], [SOCKSread])
    AC_DEFINE([write], [SOCKSwrite])
    AC_DEFINE([rresvport], [SOCKSrresvport])
    AC_DEFINE([shutdown], [SOCKSshutdown])
    AC_DEFINE([close], [SOCKSclose])
    AC_DEFINE([dup], [SOCKSdup])
    AC_DEFINE([dup2], [SOCKSdup2])
    AC_DEFINE([fclose], [SOCKSfclose])
    AC_DEFINE([gethostbyname], [SOCKSgethostbyname])
fi

have_rfc2553_netdb=no
if test $ac_cv_func_getaddrinfo = yes -a ! -n "$socks"; then
	AC_MSG_TRY_COMPILE(for AI_NUMERICHOST,
	    ircii_cv_have_ai_numerichost, [
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h> ],
	    [ int X = AI_NUMERICHOST ], [ have_rfc2553_netdb=yes ])
fi

AC_MSG_CHECKING([whether to enable ipv6])
if test "x$socks" = "x4" -o "x$socks" = "x5"; then
AC_MSG_RESULT(no, bacause you enabled SOCKS$socks support)
else
  AC_ARG_ENABLE(ipv6,
[  --enable-ipv6           Enable ipv6 (with ipv4) support
  --disable-ipv6          Disable ipv6 support],
[ case "$enableval" in
  no)
       AC_MSG_RESULT(no)
       ipv6=no
       ;;
  *)   AC_MSG_RESULT(yes)
       AC_DEFINE([INET6])
       ipv6=yes
       ;;
  esac ],

  AC_TRY_RUN([ /* AF_INET6 avalable check */
#include <sys/types.h>
#include <sys/socket.h>
int
main()
{
 if (socket(AF_INET6, SOCK_STREAM, 0) < 0)
   exit(1);
 else
   exit(0);
}],
    AC_MSG_RESULT(yes)
    AC_DEFINE([INET6])
    ipv6=yes,
    AC_MSG_RESULT(no)
    ipv6=no,
    AC_MSG_RESULT(no)
    ipv6=no))

  ipv6type=unknown
  ipv6lib=none
  ipv6trylibc=no

  if test "x$ipv6" = "xyes"; then
        AC_MSG_CHECKING([ipv6 stack type])
        for i in inria kame linux-glibc linux-libinet6 toshiba v6d zeta; do
                case $i in
                inria)
                        dnl http://www.kame.net/
                        AC_EGREP_CPP(yes, [dnl
#include <netinet/in.h>
#ifdef IPV6_INRIA_VERSION
yes
#endif],
                                [ipv6type=$i;
				AC_DEFINE([INET6])])
                        ;;
                kame)
                        dnl http://www.kame.net/
                        AC_EGREP_CPP(yes, [dnl
#include <netinet/in.h>
#ifdef __KAME__
yes
#endif],
                                [ipv6type=$i;
                                ipv6lib=inet6;
                                ipv6libdir=/usr/local/v6/lib;
                                ipv6trylibc=yes;
				AC_DEFINE([INET6])])
                        ;;
                linux-glibc)
                        dnl http://www.v6.linux.or.jp/
                        AC_EGREP_CPP(yes, [dnl
#include <features.h>
#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
yes
#endif],
                                [ipv6type=$i;
				AC_DEFINE([INET6])])
                        ;;
                linux-libinet6)
                        dnl http://www.v6.linux.or.jp/
                        if test -d /usr/inet6 -a -f /usr/include/netinet/ip6.h; then
                                ipv6type=$i
                                ipv6lib=inet6
                                ipv6libdir=/usr/inet6/lib
                                ipv6trylibc=yes;
                                CFLAGS="-I/usr/inet6/include $CFLAGS"
				AC_DEFINE([INET6])
                        fi
                        ;;
                toshiba)
                        AC_EGREP_CPP(yes, [dnl
#include <sys/param.h>
#ifdef _TOSHIBA_INET6
yes
#endif],
                                [ipv6type=$i;
                                ipv6lib=inet6;
                                ipv6libdir=/usr/local/v6/lib;
				AC_DEFINE([INET6])])
                        ;;
                v6d)
                        AC_EGREP_CPP(yes, [dnl
#include </usr/local/v6/include/sys/v6config.h>
#ifdef __V6D__
yes
#endif],
                                [ipv6type=$i;
                                ipv6lib=v6;
                                ipv6libdir=/usr/local/v6/lib;
                                CFLAGS="-I/usr/local/v6/include $CFLAGS"])
                        ;;
                zeta)
                        AC_EGREP_CPP(yes, [dnl
#include <sys/param.h>
#ifdef _ZETA_MINAMI_INET6
yes
#endif],
                                [ipv6type=$i;
                                ipv6lib=inet6;
                                ipv6libdir=/usr/local/v6/lib;
				AC_DEFINE([INET6])])
                        ;;
                esac
                if test "x$ipv6type" != "xunknown"; then
                        break
                fi
        done
        AC_MSG_RESULT($ipv6type)
  fi
  if test "x$ipv6" = "xyes" -a "x$ipv6lib" != "xnone"; then
        if test -d "$ipv6libdir" -a -f "$ipv6libdir/lib$ipv6lib.a"; then
                LIBS="$LIBS -L$ipv6libdir -l$ipv6lib"
                AC_MSG_RESULT(You have $ipv6lib library, using it)
        else
                if test "x$ipv6trylibc" = "xyes"; then
                        AC_MSG_RESULT([You do not have (need) $ipv6lib library, using libc])
                else
                        AC_MSG_ERROR(Fatal: no $ipv6lib library found.  cannot continue.
You need to fetch lib$ipv6lib.a from appropriate
ipv6 kit and compile beforehand.)
                fi
        fi
  fi

fi # socks = x[45]

AC_MSG_CHECKING(for working getaddrinfo())
if test $have_rfc2553_netdb = yes; then
	AC_DEFINE([HAVE_RFC2553_NETDB])
  AC_TRY_RUN([
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>

main()
{
  int passive, gaierr, inet4 = 0, inet6 = 0;
  struct addrinfo hints, *ai, *aitop;
  char straddr[INET6_ADDRSTRLEN], strport[16];

  for (passive = 0; passive <= 1; passive++) {
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_flags = passive ? AI_PASSIVE : 0;
    hints.ai_socktype = SOCK_STREAM;
    if ((gaierr = getaddrinfo(NULL, "54321", &hints, &aitop)) != 0) {
      (void)gai_strerror(gaierr);
      goto bad;
    }
    for (ai = aitop; ai; ai = ai->ai_next) {
      if (ai->ai_addr == NULL ||
          ai->ai_addrlen == 0 ||
          getnameinfo(ai->ai_addr, ai->ai_addrlen,
                      straddr, sizeof(straddr), strport, sizeof(strport),
                      NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
        goto bad;
      }
      switch (ai->ai_family) {
      case AF_INET:
        if (strcmp(strport, "54321") != 0) {
          goto bad;
        }
        if (passive) {
          if (strcmp(straddr, "0.0.0.0") != 0) {
            goto bad;
          }
        } else {
          if (strcmp(straddr, "127.0.0.1") != 0) {
            goto bad;
          }
        }
        inet4++;
        break;
      case AF_INET6:
        if (strcmp(strport, "54321") != 0) {
          goto bad;
        }
        if (passive) {
          if (strcmp(straddr, "::") != 0) {
            goto bad;
          }
        } else {
          if (strcmp(straddr, "::1") != 0) {
            goto bad;
          }
        }
        inet6++;
        break;
      case AF_UNSPEC:
        goto bad;
        break;
      default:
        /* another family support? */
        break;
      }
    }
  }

  if (inet4 != 0 && inet4 != 2)
    goto bad;
  if (inet6 != 0 && inet6 != 2)
    goto bad;

  if (aitop)
    freeaddrinfo(aitop);
  exit(0);

 bad:
  if (aitop)
    freeaddrinfo(aitop);
  exit(1);
}],
  AC_MSG_RESULT(good)
  buggygetaddrinfo=no,
  AC_MSG_RESULT(buggy)
  buggygetaddrinfo=yes,
  AC_MSG_RESULT(buggy)
  buggygetaddrinfo=yes)

  if test "x$buggygetaddrinfo" = "xyes"; then
	echo 'Using local version of getaddrinfo()'
	ac_cv_func_getaddrinfo=no
  fi

else
  if test $ac_cv_func_getaddrinfo = yes; then
	AC_MSG_RESULT(no - using local version)
  else
	AC_MSG_RESULT(using local version)
  fi
  ac_cv_func_getaddrinfo=no
fi # have_rfc2553_netdb = yes

if test -n "$hpux"
then
	# HP-UX's pax is broken
	AC_CHECK_PROGS(COPY_DIRECTORY, tar cpio)
else
	AC_CHECK_PROGS(COPY_DIRECTORY, pax tar cpio)
fi
if test "x$COPY_DIRECTORY" = "xpax";
then
    INSTALL_HELP_CMD='cd $(topdir)/help; pax -r -w -s ";.*/CVS$$;;" -s ";.*/\.CVS$$;;" -s ";.*/CVS/.*;;" -s ";.*/\.CVS/.*;;" . $(HELP_DIR)'
else
    if test "x$COPY_DIRECTORY" = "xtar";
    then
	INSTALL_HELP_CMD='cd $(topdir)/help; tar -cf - . | ( cd $(HELP_DIR); tar -xf - )'
    else
	if test "x$COPY_DIRECTORY" = "xcpio";
	then
	    INSTALL_HELP_CMD='cd $(topdir)/help; find . -print | cpio -pdu $(HELP_DIR)'
	else
	    INSTALL_HELP_CMD="@echo \"I couldn't figure out a way to install the help files.\""
	fi
    fi
fi
AC_SUBST(INSTALL_HELP_CMD)

AC_CHECK_PROGS(ZCAT, gzcat zcat gunzip gzip uncompress uncompress)
case "x$ZCAT" in
  xgzcat)
    ZSUFFIX=".gz"
    ZARGS=
    ;;
  xzcat)
    ZSUFFIX=".Z"
    ZARGS=
    ;;
  xgunzip)
    ZSUFFIX=".gz"
    ZARGS="-c"
    ;;
  xgzip)
    ZSUFFIX=".gz"
    ZARGS="-dc"
    ;;
  xuncompress)
    ZSUFFIX=".Z"
    ZARGS="-c"
    ;;
  xcompress)
    ZSUFFIX=".Z"
    ZARGS="-dc"
    ;;
esac
AC_DEFINE_UNQUOTED([ZCAT],["$ZCAT"])
AC_DEFINE_UNQUOTED([ZSUFFIX],["$ZSUFFIX"])
if test x"$ZARGS" != x; then
  AC_DEFINE_UNQUOTED([ZARGS],["$ZARGS"])
fi

dnl
dnl check if we need snprintf.c, getaddrinfo.c or getnameinfo.c
dnl
EXTRA_OBJECTS=
EXTRA_SOURCES=
EXTRA_IRCIO_OBJECTS=
EXTRA_IRCIO_SOURCES=
if test x$ac_cv_func_snprintf != xyes || test x$ac_cv_func_vsnprintf != xyes
then
  EXTRA_OBJECTS="$EXTRA_OBJECTS snprintf.o"
  EXTRA_SOURCES="$EXTRA_SOURCES snprintf.c"
  EXTRA_IRCIO_OBJECTS="$EXTRA_IRCIO_OBJECTS snprintf.o"
  EXTRA_IRCIO_SOURCES="$EXTRA_IRCIO_SOURCES snprintf.c"
fi
if test x$ac_cv_func_getaddrinfo != xyes
then
  EXTRA_OBJECTS="$EXTRA_OBJECTS getaddrinfo.o"
  EXTRA_SOURCES="$EXTRA_SOURCES getaddrinfo.c"
  EXTRA_IRCIO_OBJECTS="$EXTRA_IRCIO_OBJECTS getaddrinfo.o"
  EXTRA_IRCIO_SOURCES="$EXTRA_IRCIO_SOURCES getaddrinfo.c"
  if test x$ac_cv_func_inet_pton != xyes
  then
    EXTRA_OBJECTS="$EXTRA_OBJECTS inet_pton.o"
    EXTRA_SOURCES="$EXTRA_SOURCES inet_pton.c"
    EXTRA_IRCIO_OBJECTS="$EXTRA_IRCIO_OBJECTS inet_pton.o"
    EXTRA_IRCIO_SOURCES="$EXTRA_IRCIO_SOURCES inet_pton.c"
  fi
fi
if test x$ac_cv_func_getnameinfo != xyes
then
  EXTRA_OBJECTS="$EXTRA_OBJECTS getnameinfo.o"
  EXTRA_SOURCES="$EXTRA_SOURCES getnameinfo.c"
  if test x$ac_cv_func_inet_ntop != xyes
  then
    EXTRA_OBJECTS="$EXTRA_OBJECTS inet_ntop.o"
    EXTRA_SOURCES="$EXTRA_SOURCES inet_ntop.c"
  fi
fi
AC_SUBST(EXTRA_OBJECTS)
AC_SUBST(EXTRA_SOURCES)
AC_SUBST(EXTRA_IRCIO_OBJECTS)
AC_SUBST(EXTRA_IRCIO_SOURCES)

AC_FIND_PROGRAM(SENDMAIL,sendmail,/usr/sbin:/usr/lib:/usr/ucblib)

AC_SUBST(RM)
AC_SUBST(LN)
AC_OUTPUT(Makefile ircbug)

if test ! -f config.h; then
  cp ${srcdir}/include/config.h.dist config.h
fi

echo
echo well there we are, you now might want to look in config.h and 
echo see if there is anything you might want to tune, else you can
echo just run a make here..  good luck!
echo

