dnl Process this file with autoconf to produce a configure script.
AC_INIT(exp_global.h)
AC_CONFIG_HEADER(exp_conf.h)
AC_PROG_INSTALL
AC_PROGRAM_CHECK(RANLIB, ranlib, ranlib, true)
dnl AC_PROG_RANLIB

AC_PID_T

#
# Look for various header files
#
AC_HEADER_CHECK(stdlib.h, ,AC_DEFINE(NOSTDLIB))
AC_HEADER_CHECK(sys/wait.h, AC_DEFINE(HAVE_SYS_WAIT_H))
AC_HEADER_CHECK(sys/stropts.h, AC_DEFINE(HAVE_STROPTS_H))
AC_HEADER_CHECK(sys/sysconfig.h, AC_DEFINE(HAVE_SYSCONF_H))
AC_HEADER_CHECK(sys/fcntl.h, AC_DEFINE(HAVE_SYS_FCNTL_H))
AC_HEADER_CHECK(sys/sysmacros.h, AC_DEFINE(HAVE_SYSMACROS_H))
AC_UNISTD_H

#
# Look for various functions
#
AC_FUNC_CHECK(memcpy, ,AC_DEFINE(NO_MEMCPY))
AC_FUNC_CHECK(waitpid, ,AC_DEFINE(NOWAITPID))
AC_FUNC_CHECK(wait4, AC_DEFINE(HAVE_WAIT4))
AC_FUNC_CHECK(sysconf, AC_DEFINE(HAVE_SYSCONF))

#
# check how signals work
#

# Check for the data type of the function used in signal(). This
# must be before the test for rearming.
echo checking return type of signal handlers
AC_HEADER_EGREP([(void|sighandler_t).*signal], signal.h, retsigtype=void,AC_DEFINE(RETSIGTYPE, int) retsigtype=int)

# FIXME: check to see it alarm exists
# Check to see it signals need to be re-armed after use.
echo checking to see if signals need to be re-armed
AC_TEST_PROGRAM([
#include <signal.h>
#define RETSIGTYPE $retsigtype

void
sigquit_handler(n)
int n;
{
}

main()
{
	if (0 == fork()) {
		unlink("core");
		signal(SIGQUIT,sigquit_handler);
		kill(getpid(),SIGQUIT);
		kill(getpid(),SIGQUIT);
	} else {
		int status;

		wait(&status);
		exit(unlink("core")==-1?0:1);
	}
}], ,AC_DEFINE(REARM_SIG))

# HPUX7 has trouble with the big cat so split it
# Owen Rees <rtor@ansa.co.uk> 29Mar93 
SEDDEFS="${SEDDEFS}CONFEOF
cat >> conftest.sed <<CONFEOF
"
#
# Look for various features to determine what kind of pty
# we have. For some weird reason, ac_compile_check would not
# work, but ac_test_program does.
#
echo checking for HP style pty allocation
# following test fails on DECstations and other things that don't grok -c
# but that's ok, since they don't have PTYMs anyway
if test -c /dev/ptym/ptyp0 2>>/dev/null ; then
    AC_DEFINE(HAVE_PTYM)
fi

echo checking for HP style pty-trapping
AC_HEADER_EGREP([struct.*request_info], sys/ptyio.h, AC_DEFINE(HAVE_PTYTRAP), )

echo checking for AIX new-style pty allocation
if test -r /dev/ptc -a -r /dev/pts ; then
    AC_DEFINE(HAVE_PTC_PTS)
fi

echo checking for SGI old-style pty allocation
if test -r /dev/ptc -a ! -r /dev/pts ; then
    AC_DEFINE(HAVE_PTC)
fi

echo checking for SVR4 style pty allocation
if test -r /dev/ptmx ; then
  AC_DEFINE(HAVE_PTMX)
  # aargg. Some systems need libpt.a to use /dev/ptmx
    echo checking for libpt.a
    AC_FUNC_CHECK(ptsname, ,LIBS=-lpt)
fi

# In OSF/1 case, SVR4 are somewhat different.
# Gregory Depp <depp@osf.org> 17Aug93
echo checking for OSF/1 style pty allocation
if test -r /dev/ptmx_bsd ; then
    AC_DEFINE(HAVE_PTMX_BSD)
fi


AC_FUNC_CHECK(tcgetattr, tcgetattr=1)
AC_FUNC_CHECK(tcsetattr, tcsetattr=1)
if test $tcgetattr -a $tcsetattr ; then
    AC_DEFINE(POSIX)
fi

# first check for the pure bsd
echo checking for struct sgttyb
AC_TEST_PROGRAM([#include <sgtty.h>
main()
{
  struct sgttyb tmp;
  exit(0);
}], AC_DEFINE(HAVE_SGTTYB) PTY_TYPE=sgttyb)

# next check for the older style ttys
# note that if we detect termio.h (only), we still set PTY_TYPE=termios
# since that just controls which of pty_XXXX.c file is use and
# pty_termios.c is set up to handle pty_termio.
echo checking for struct termio
AC_TEST_PROGRAM([#include <termio.h>
main()
{
  struct termio tmp;
  exit(0);
}], AC_DEFINE(HAVE_TERMIO) PTY_TYPE=termios)

# now check for the new style ttys (not yet posix)
echo checking for struct termios
AC_TEST_PROGRAM([#include <termios.h>
main()
{
  struct termios tmp;
  exit(0);
}], AC_DEFINE(HAVE_TERMIOS) PTY_TYPE=termios)

# finally check for Cray style ttys
echo checking for Cray-style ptys
SETUID="@:"
AC_TEST_PROGRAM([
main(){
#ifdef CRAY
	return 0;
#else
	return 1;
#endif
}
], PTY_TYPE=unicos SETUID="chmod u+s", )
#
# Check for select and/or poll. If both exist, we prefer select.
# if neither exists, define SIMPLE_EVENT.
#
select=0
poll=0
AC_FUNC_CHECK(select, select=1)
AC_FUNC_CHECK(poll,   poll=1)
if test $poll -a $select ; then
  EVENT_TYPE=select
  poll=0
elif test $poll ; then
  EVENT_TYPE=poll
elif test $select ; then
  EVENT_TYPE=select
else
  EVENT_TYPE=simple
  AC_DEFINE(SIMPLE_EVENT)
fi

AC_HAVE_FUNCS(getpty)
AC_HAVE_FUNCS(_getpty)

#
# Look for the X11 include files in various places.
#
Xbuild=1
echo checking for the X11 headers
XINCLUDES="# no special path needed"
AC_TEST_CPP([#include <X11/Intrinsic.h>], , XINCLUDES="nope")
if test "$XINCLUDES" = "nope" ; then
  dirs="/usr/unsupported/include /usr/local/include /usr/X386/include /usr/include/X11R4 /usr/X11R5/include /usr/include/X11R5 /usr/openwin/include /usr/X11/include"
  for i in $dirs ; do
    if test -r $i/X11/Intrinsic.h; then
      XINCLUDES=" -I$i"
    fi
  done
fi

if test "$XINCLUDES" = "nope" ; then
  XINCLUDES="# no X support on this system"
  Xbuild=0
fi

#
# Now check for the libraries. The first test merely looks in a few
# different places.
# 
XLIBS="-lX11"
AC_HAVE_LIBRARY(libX11.a, , XLIBS="nope")
if test "$XLIBS" = "nope" ; then
  dirs="/usr/local/lib /usr/lib/X11R4 /usr/lib/X11R5 /usr/X11/lib /usr/lib/X11 /usr/openwin/lib"
  for i in $dirs ; do
    if test -r $i/libX11.a; then
      XLIBS="-L$i -lX11"
    fi
  done
fi

#
# HP-UX wants this instead of libX11
#
if test "$XLIBS" = "nope" ; then
  AC_HAVE_LIBRARY(libXwindow.a, XLIBS="-lXwindow")
fi

if test "$XLIBS" = "nope" ; then
  Xbuild=0
fi

XPROGS=""
if test $Xbuild = 1; then
  XPROGS="expectk \$(LIBEXPTK)"
else
  XPROGS="# no X support on this system"
  echo "No X based programs will be built"
fi

#
# Set up makefile substitutions
#
AC_SUBST(XINCLUDES)
AC_SUBST(XLIBS)
AC_SUBST(XPROGS)
AC_SUBST(PTY_TYPE)
AC_SUBST(EVENT_TYPE)
AC_SUBST(SETUID)
AC_OUTPUT(Makefile)
