dnl configure.in for ircii, 2.3.  copyright (c) matthew green, 1993,
dnl except those bits attributed otherwise.
dnl thanks to the screen 3.3 configure.in for giving me examples to
dnl work from and steal ;)
dnl
dnl @(#)$Id: configure.in,v 1.40 1994/10/16 00:30:09 mrg Stab $

AC_REVISION($Revision: 1.40 $)dnl
AC_INIT(source/irc.c)
AC_CONFIG_HEADER(include/defs.h)

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' < source/irc.c` 
echo this is ircii version $VERSION
echo
AC_SUBST(VERSION)

dnl
dnl initial stuff
dnl

AC_PROG_CC
AC_PROG_CPP
AC_ISC_POSIX
AC_AIX
AC_PROG_LEX
AC_GCC_TRADITIONAL
AC_IRIX_SUN

dnl use this later for checking curses/termcap
case "$DEFS" in
  *-D_ALL_SOURCE*)
    aix=1
    ;;
esac

echo checking for qnx
case "$(uname -s)" in
    QNX*) LDFLAGS=-N64k                  dnl 64k of stack
          CFLAGS="-w4 -O -g -fi=unix.h"  dnl better compile options
      ;;
esac

echo checking for solaris
if /bin/sun 2> /dev/null
then
  uname=`uname -r`
  case "$uname" in
    4.*)
      ;;
    5.*) AC_DEFINE(__solaris__)
	 AC_DEFINE(SVR4)
      ;;
  esac
fi

case "$DEFS" in
  *-DSVR4*)			dnl might be defined above...
   ;;
  *)

echo checking for SVR4
AC_PROGRAM_EGREP(yes,
[#if defined(SVR4) || defined(__svr4__)
  yes
#endif
],svr4=1)
    ;;
esac

if test -n "$svr4"
then
  LIBS="-L/usr/ccs/lib $LIBS"
  AC_DEFINE(SVR4)
fi

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

echo checking for -I/usr/netinclude
if test -d /usr/netinclude; then
  CFLAGS="$CFLAGS -I/usr/netinclude"
fi
  
AC_HAVE_HEADERS(sys/un.h sys/select.h sys/fcntl.h sys/file.h sys/time.h sys/wait.h sys/twg_config.h sys/ptem.h)dnl sys/ ones
AC_HAVE_HEADERS(stdarg.h unistd.h fcntl.h string.h memory.h netdb.h limits.h)dnl non sys/ ones
AC_FUNC_CHECK(setsid, AC_DEFINE(HAVE_SETSID),)
AC_FUNC_CHECK(getpgid, AC_DEFINE(HAVE_GETPGID),)
AC_FUNC_CHECK(getsid, AC_DEFINE(HAVE_GETSID),)
AC_FUNC_CHECK(scandir, AC_DEFINE(HAVE_SCANDIR),)
AC_FUNC_CHECK(memmove, AC_DEFINE(HAVE_MEMMOVE),)
AC_FUNC_CHECK(uname, AC_DEFINE(HAVE_UNAME),
  AC_HAVE_LIBRARY(posix,
  oLIBS=$LIBS
  LIBS="$LIBS -lposix"
  AC_FUNC_CHECK(uname, AC_DEFINE(HAVE_UNAME), LIBS=$oLIBS)))
AC_FUNC_CHECK(getcwd,, AC_DEFINE(NEED_GETCWD))
AC_FUNC_CHECK(fchmod,, AC_DEFINE(NEED_FCHMOD))
AC_FUNC_CHECK(strerror,, AC_DEFINE(NEED_STRERROR))
AC_STDC_HEADERS
AC_TIME_WITH_SYS_TIME
AC_DIR_HEADER
AC_RETSIGTYPE
AC_MODE_T
AC_PID_T
AC_SIZE_T
AC_UID_T

case $DEFS in
  *HAVE_SYS_TWG_CONFIG*)  AC_DEFINE(WINS)
    ;;
esac

dnl
dnl check for hpux, and hpux version
dnl

echo checking for hpux version
if test -f /hp-ux; then
  hpux=1
  uname=`uname -r` 2> /dev/null
  case $uname in
	A.09* | A.9* | a.9* | a.09* | 9* | 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
fi

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

echo checking for NeXT
AC_PROGRAM_EGREP(yes,
[#if defined(NeXT) || defined(__NeXT) || defined(__NeXT__)
  yes
#endif
],nextstep=1)

if test -n "$nextstep"; then
  if test "$CC" != "gcc"; then
    CC="cc -bsd"
  fi
fi

dnl
dnl do ultrix/posix stuff
dnl

echo checking for Ultrix
AC_PROGRAM_EGREP(yes,
[#if defined(ultrix) || defined(__ultrix) || defined(__ultrix)
  yes
#endif
], ULTRIX=1)

if test -n "$ULTRIX"; then
  test -z "$GCC" && CC="$CC -YBSD"
else
  # POSIX termios is broken on Ultrix so don't look for it.
echo checking for POSIX.1
AC_PROGRAM_EGREP(yes,
[#include <sys/types.h>
#include <unistd.h>
main () {
#ifdef _POSIX_VERSION
  yes
#endif
}
],AC_DEFINE(POSIX) posix=1)
fi

dnl
dnl check for ptx
dnl

echo checking for sequent/ptx
AC_PROGRAM_EGREP(yes,
[#ifdef _SEQUENT_
  yes
#endif
], LIBS="$LIBS -lseq";seqptx=1)

dnl
dnl check for struct linger
dnl

echo checking for struct linger
AC_HEADER_EGREP([struct( |	)*linger], sys/socket.h,, AC_DEFINE(NO_STRUCT_LINGER))

dnl
dnl grok process groups
dnl

case "$DEFS" in
  *-DHAVE_GETSID* | *-DHAVE_GETPGID* )		dnl have a working way to grok process groups
    ;;
  *)						dnl have to test if getpgrp() really does work as posix says so
    echo checking if getpgrp\(\) takes an argument
    AC_TEST_PROGRAM([
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);
}
],,echo your getpgrp\(\) is painful;AC_DEFINE(BROKEN_GETPGRP))
    ;;
esac

dnl
dnl work out wait type..
dnl

if test x$posix != x1
then
AC_COMPILE_CHECK(union wait,[#include <sys/types.h>
#include <sys/wait.h>
],[
  union wait x;
  int y;
#ifdef WEXITSTATUS
  y = WEXITSTATUS(x);
#endif
],AC_DEFINE(BSDWAIT), AC_FUNC_CHECK(waitpid,, AC_DEFINE(NEED_WAITPID)))
fi

dnl
dnl checking for select()
dnl

oldlibs="$LIBS"
AC_COMPILE_CHECK(select,,[select(0, 0, 0, 0, 0);],,
LIBS="$oldlibs -lsocket"
AC_COMPILE_CHECK(select with $LIBS,,[select(0, 0, 0, 0, 0);],libsocket=1,
LIBS="$oldlibs -lnsl"
AC_COMPILE_CHECK(select with $LIBS,,[select(0, 0, 0, 0, 0);],libnsl=1,
LIBS="$oldlibs -linet"
AC_COMPILE_CHECK(select with $LIBS,,[select(0, 0, 0, 0, 0);],libinet=1,
LIBS="$oldlibs -lcposix"
AC_COMPILE_CHECK(select with $LIBS,,[select(0, 0, 0, 0, 0);],libcposix=1,
LIBS="$oldlibs -lnet -lnsl_s"
AC_COMPILE_CHECK(select with $LIBS,,[select(0, 0, 0, 0, 0);],libnet=1;libnsl_s=1,
LIBS=$oldlibs
echo "I can not find select!  You might need to help me"
))))))

dnl
dnl termcap or curses
dnl
if test x$hpux = x1 -o x$aix = x1
then
  LIBS="-lcurses $LIBS"
  AC_DEFINE(USING_CURSES)
else
echo searching for tgetent
olibs="$LIBS"
  LIBS="-ltermcap $olibs"
  AC_COMPILE_CHECK(libtermcap,,tgetent((char *)0, (char *)0);,AC_DEFINE(USING_TERMCAP),
  LIBS="-ltermlib $olibs"
  AC_COMPILE_CHECK(libtermlib,,tgetent((char *)0, (char *)0);,AC_DEFINE(USING_TERMLIB),
  LIBS="-lcurses $olibs"
  AC_COMPILE_CHECK(libcurses,,tgetent((char *)0, (char *)0);,AC_DEFINE(USING_CURSES),
  LIBS="-lxtermcap $olibs"
  AC_COMPILE_CHECK(libxtermcap,,tgetent((char *)0, (char *)0);,AC_DEFINE(USING_XTERMCAP),
  echo "no tgetent - no ircii";exit)
)))
fi

dnl
dnl signal handling
dnl

echo checking signal implimentation
AC_FUNC_CHECK(sigaction, AC_DEFINE(USE_SIGACTION),
  AC_FUNC_CHECK(sigset, AC_DEFINE(USE_SIGSET),

echo checking for reliable signals
AC_TEST_PROGRAM([
#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);
}
],,AC_DEFINE(SYSVSIGNALS))
))

case "$DEFS" in
  *BSDWAIT*)
echo checking for wait3 declaration
AC_PROGRAM_EGREP([wait3( |	|\()],[#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
],AC_DEFINE(WAIT3_DECLARED))
  ;;
  *)
echo checking for waitpid declaration
AC_PROGRAM_EGREP([waitpid( |	|\()],[#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
],AC_DEFINE(WAITPID_DECLARED))
  ;;
esac

dnl
dnl check for sys_errlist declaration
dnl

echo checking for sys_errlist declaration
AC_TEST_PROGRAM([
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
main()
{
  char *s = sys_errlist[0];
  exit(0);
}
],AC_DEFINE(SYS_ERRLIST_DECLARED),)

dnl
dnl need this for dcc stuff
dnl

echo checking for a 32 bit integer
AC_TEST_PROGRAM([
main()
{
  if (4 == sizeof(unsigned long))
    exit(0);
  exit(1);
}
],AC_DEFINE(UNSIGNED_LONG32),AC_TEST_PROGRAM([
main()
{
  if (4 == sizeof(unsigned int))
    exit(0);
  exit(1);
}
],AC_DEFINE(UNSIGNED_INT32),AC_DEFINE(UNKNOWN_32INT)))

dnl
dnl libraries
dnl

AC_HAVE_LIBRARY(elf, LIBS="$LIBS -lelf",)
AC_HAVE_LIBRARY(nls, libnls=1;AC_DEFINE(HAVE_LIB_NLS),)
if test ! -n "$libsocket"; then
	AC_HAVE_LIBRARY(socket, LIBS="$LIBS -lsocket",)
fi
if test ! -n "$libinet"; then
	AC_HAVE_LIBRARY(inet, LIBS="$LIBS -linet",)
fi
if test ! -n "$libnsl"; then
	AC_HAVE_LIBRARY(nsl, LIBS="$LIBS -lnsl",)
fi
AC_HAVE_LIBRARY(dgc, LIBS="$LIBS -ldgc",)
AC_HAVE_LIBRARY(resolv,
olibs=$LIBS
LIBS="$LIBS -lresolv"
AC_TEST_PROGRAM([
#include <netdb.h>
main()
{
  struct hostent *hp;

  hp = gethostbyname("foo");
  exit(0);
}
],,LIBS=$olibs),)
dnl AC_HAVE_LIBRARY(PW, LIBS="$LIBS -lPW",)

dnl this is all stolen from perl-4.036's Configure.  larry is god.
echo "generating a list of signal names..."
set X `cat /usr/include/signal.h /usr/include/sys/signal.h 2>&1 | awk '
$1 ~ /^#define$/ && $2 ~ /^SIG[A-Z0-9]*$/ && $3 ~ /^[1-9][0-9]*$/ {
  [sig[$3] = substr($2,4,20)]
  if (max < $3 && $3 < 60) {
	max = $3
  }
}

END {
  for (i=1; i<=max; i++) {
    [if (sig[i] == "")]
      printf "%d", i
    else
      [printf "%s", sig[i]]
    if (i < max)
      printf " "
  }
  printf "\n"
}
'`
shift
case $# in
0)
  set X `kill -l 2> /dev/null`
  shift
  case $# in
  0) set HUP INT QUIT ILL TRAP IOT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM
    ;;
  esac
  ;;
esac
sig_name="ZERO $*"
if test -f source/sig.inc; then
  /bin/rm -f source/sig.inc
fi
[echo $sig_name | sed -e 's/$/"};/' -e 's/ /", "/g' -e 's/^/char *signals[] = { "/' >> source/sig.inc]

dnl work out UNIX mail dir..

echo 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\"
else
  UNIX_MAIL=\"unknown\"
fi

AC_DEFINE_UNQUOTED(UNIX_MAIL, \"$mdir\")

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

if test ! -n "$IRCLIB";
then
	IRCLIB=\${prefix}/lib/irc
fi

AC_SUBST(CFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(RM)
AC_SUBST(LN)
AC_SUBST(IRCLIB)
AC_SUBST(bindir)
AC_OUTPUT(Makefile source/Makefile)

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

