dnl tempnam needs replaced on some systems e.g. next
dnl
dnl Look for the X11 include files in various places, if they're
dnl not in the compiler's path.  Substitute for `xincludedir' and
dnl `xlibdir'.  Perhaps we need different checks for Xt and Xaw?
define(AC_X11_LOCATION,
[echo checking for X11 headers and libraries
dir=""
AC_TEST_CPP([#include <X11/Intrinsic.h>], :,
  if test -r /usr/local/[[include]]/X11/Intrinsic.h; then
    dir=/usr/local/[[include]]

  elif test -r /usr/[[include]]/X11R4/X11/Intrinsic.h; then
    dir=/usr/[[include]]/X11R4
  elif test -r /usr/[[include]]/X11R5/X11/Intrinsic.h; then
    dir=/usr/[[include]]/X11R5

  elif test -r /usr/lpp/X11/Xamples/[[include]]/X11/Intrinsic.h; then
    dir=/usr/lpp/X11/Xamples/[[include]]

  elif test -r /usr/X11/[[include]]/X11/Intrinsic.h; then
    dir=/usr/X11/[[include]]
  elif test -r /usr/X11R5/[[include]]/X11/Intrinsic.h; then
    dir=/usr/X11R5/[[include]]

  elif test -r /usr/openwin/[[include]]/X11/Intrinsic.h; then
    dir=/usr/openwin/[[include]]
  elif test -r /usr/openwin/share/[[include]]/X11/Intrinsic.h; then
    dir=/usr/openwin/share/[[include]]

  elif test -r /usr/[[include]]/X11/Intrinsic.h; then
    dir= # everyone's cc searches /usr/include, right?
  else
    no_x=t
  fi
)
dnl Can't use AC_SUBST inside AC_TEST_CPP.
if test -n "$dir"; then
  xincludedir=-I$dir
  echo "(using $xincludedir)"
elif test -n "$no_x"; then 
  # Not all programs may use this symbol, but it won't hurt to define it.
  xincludedir=-DX_DISPLAY_MISSING
fi
AC_SUBST(xincludedir)
#
# Now check for the libraries.  Amazing how every single X vendor puts
# these in a different place, and all because MIT thought they should go
# in /usr/lib.
dir1=""
if test -r /usr/local/lib/libXt.a; then
  dir1=/usr/local/lib

elif test -r /usr/lib/X11R4/libXt.sl; then
  dir1=/usr/lib/X11R4
elif test -r /usr/lib/X11R4/libXt.a; then
  dir1=/usr/lib/X11R4
elif test -r /usr/lib/X11R5/libXt.a; then
  dir1=/usr/lib/X11R5

elif test -r /usr/lpp/X11/Xamples/lib/Xt/libXt.a; then
  dir1=/usr/lpp/X11/Xamples/lib/Xt

elif test -r /usr/X11/lib/libXt.a; then
  dir1=/usr/X11/lib
elif test -r /usr/X11R5/lib/libXt.a; then
  dir1=/usr/X11R5/lib

elif test -r /usr/openwin/lib/libXt.a; then
  dir1=/usr/openwin/lib
elif test -r /usr/openwin/lib/libXt.so; then
  dir1=/usr/openwin/lib

fi
dir2=""
if test -r /usr/lpp/X11/Xamples/lib/Xmu/libXmu.a; then
  dir2=/usr/lpp/X11/Xamples/lib/Xmu
fi
#
# It would be nice to have a more robust check for the -R ld option then
# just checking for Solaris.  The parentheses elide `uname: not found'.
# 
# It would also be nice to do this for all -L options, not just this one.
if test -n "$dir1"; then
  xlibdir=-L$dir1
  if test "`(uname) >/dev/null 2>&1`" = SunOS && uname -r | grep '^5'; then
    xlibdir="$xlibdir -R$dir1"
  fi
  #
  # Don't need all that stuff for dir2, since it only gets used on AIX.
  test -n "$dir2" && xlibdir="$xlibdir -L$dir2"
fi
#
# Check for additional X libraries.
# 
# Since we already have an explicit check for POSIXified ISC, use it.
if test -n "$ISC"; then
  wlibs="$wlibs -lnsl_s -linet"
  echo "(adding -lnsl_s -linet to wlibs)"
else
  # Martyn.Johnson@cl.cam.ac.uk says this is needed for Ultrix, if the X
  # libraries were built with DECnet support.  And karl@cs.umb.edu's Alpha
  # needs dnet_stubs.
  AC_HAVE_LIBRARY(dnet,
    [wlibs="$wlibs -ldnet"
     have_dnet=t
     echo "(adding -ldnet to wlibs)"])
  if test -z "$have_dnet"; then
    AC_HAVE_LIBRARY(dnet_stub,
      [wlibs="$wlibs -ldnet_stub"
       echo "(adding -ldnet_stub to wlibs)"])
  fi
  # lieder@skyler.mavd.honeywell.com says without -lsocket,
  # socket/setsockopt and other routines are undefined under SCO ODT 2.0.
  AC_HAVE_LIBRARY(socket,
    [wlibs="$wlibs -lsocket"
     echo "(adding -lsocket to wlibs)"])
fi
test -n "$xlibdir" && echo "(using $xlibdir)"
AC_SUBST(xlibdir)dnl
AC_SUBST(wlibs)dnl
])dnl
dnl Process this file with autoconf to produce a configure script.
dnl
AC_INIT(io-term.c)
AC_SUBST(LIBS)dnl
dnl Checks for programs:
AC_PROG_CC
AC_PROG_CPP
AC_PROG_YACC
AC_PROG_INSTALL

dnl Checks for UNIX variants that set `DEFS':
AC_ISC_POSIX
AC_AIX

dnl Checks for header files:
AC_STDC_HEADERS
AC_UNISTD_H
AC_DIR_HEADER
AC_HAVE_HEADERS(sys/time.h string.h memory.h sys/select.h)
AC_X11_LOCATION
AC_HEADER_CHECK(X11/X.h, AC_DEFINE(HAVE_X11_X_H); LIBS="$LIBS -lX11")

dnl checks for functions
AC_RETSIGTYPE
AC_ALLOCA
AC_REPLACE_FUNCS(random strftime mktime xmalloc)

AC_CONST

dnl Replace vfprintf and vsprintf.
dnl If either needs to be replaced, replace _doprnt.
need_doprnt=
AC_COMPILE_CHECK([vfprintf], , [vfprintf();], ,
[LIBOBJS="$LIBOBJS vfprintf.o"; need_doprnt=1])
AC_COMPILE_CHECK([vsprintf], , [vsprintf();], ,
[LIBOBJS="$LIBOBJS vsprintf.o"; need_doprnt=1])
if test $need_doprnt; then
  AC_REPLACE_FUNCS(_doprnt)
fi

AC_HAVE_LIBRARY(termcap, \
		LIBS="$LIBS -lcurses -ltermcap", \
		AC_HAVE_LIBRARY(termlib, \
				LIBS="$LIBS -lcurses -ltermlib", \
				LIBS="$LIBS -lcurses"))

LIBS_save="$LIBS"
LIBS="$LIBS -lm"
compile='${CC-cc} $DEFS conftest.c -o conftest $LIBS >/dev/null 2>&1'
AC_HAVE_FUNCS(rint rename \
	      getcap stricmp strincmp \
	      strdup strstr strftime mktime)
AC_COMPILE_CHECK([cbreak], [#include <curses.h>], [cbreak();],
AC_DEFINE(HAVE_CBREAK))
LIBS="$LIBS -lm"
AC_COMPILE_CHECK([hypot], [], [hypot();],
AC_DEFINE(HAVE_HYPOT))
LIBS="$LIBS_save"
AC_COMPILE_CHECK([bogus definition of __GNUC__], [static int x = 54;], [
#if defined (__GNUC__) && __GNUC__ >= 2
exit (__extension__ x);
#endif
],
[echo looks ok],
[DEFS="$DEFS -D__extension__"])
AC_OUTPUT(Makefile)
