dnl: You can process this file with autoconf to produce a configure script.
dnl: However, normally the supplied configure script will work fine.

AC_INIT(xboard.c)
AC_CONFIG_HEADER(config.h)

PRODUCT=xboard
VERSION=3.3
PATCHLEVEL=0

if test -z "$CFLAGS" ; then
dnl: Prevent the next macro from setting CFLAGS to -g
  CFLAGS=" "
fi
AC_PROG_CC
AC_ISC_POSIX
AC_PROG_INSTALL
AC_CHECK_PROGS(LEX, lex flex, lex)
AC_DECL_YYTEXT
AC_CHECK_PROGS(RSH, remsh rsh, rsh)
AC_DEFINE_UNQUOTED(REMOTE_SHELL, "$RSH")
AC_CHECK_PROG(NROFF, nroff, [nroff -man], cat)
AC_SUBST(NROFFFLAGS)
AC_PATH_PROGS(AWKPATH, awk mawk gawk nawk)
AC_PATH_PROGS(PERLPATH, perl)

AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_SYS_WAIT
AC_TYPE_SIGNAL
AC_CHECK_HEADERS(stropts.h sys/time.h string.h unistd.h sys/systeminfo.h)
AC_CHECK_HEADER(fcntl.h,
  AC_DEFINE(HAVE_FCNTL_H), AC_CHECK_HEADERS(sys/fcntl.h))
AC_CHECK_HEADER(sys/socket.h,
  AC_DEFINE(HAVE_SYS_SOCKET_H), AC_CHECK_HEADERS(lan/socket.h))
AC_CHECK_HEADER(stddef.h, [], AC_DEFINE(X_WCHAR, 1))

AC_CHECK_FUNCS(_getpty grantpt)
AC_CHECK_FUNC(gettimeofday,
  AC_DEFINE(HAVE_GETTIMEOFDAY), AC_CHECK_FUNCS(ftime))
AC_CHECK_FUNC(random,
  AC_DEFINE(HAVE_RANDOM), AC_CHECK_FUNCS(rand48))
AC_CHECK_FUNC(gethostname,
  AC_DEFINE(HAVE_GETHOSTNAME), AC_CHECK_FUNCS(sysinfo))
AC_CHECK_FUNC(setlocale, [],
  AC_CHECK_LIB(i, setlocale, [], AC_DEFINE(X_LOCALE, 1)))

AC_CHECK_LIB(seq, getpseudotty)

AC_PATH_XTRA

AC_CANONICAL_HOST

dnl: The following info is mostly gathered from GNU Emacs 19.24.  Basically,
dnl: we are trying to find out whether this is a System-V derivative (in
dnl: which case we must use ptys) and if so, whether there is anything
dnl: strange about the way to open a pty.  Some of the work was done above
dnl: by looking for _getpty, grantpt, and getpseudotty.  A few other strange
dnl: properties of particular systems are also handled here.

USE_PTYS=0
case "$host" in
  *-*-hpux* )
    AC_DEFINE(PTY_TTY_NAME_SPRINTF,
	[sprintf (pty_name, "/dev/pty/tty%c%x", c, i);])
    AC_DEFINE(PTY_NAME_SPRINTF,
	[sprintf (pty_name, "/dev/ptym/pty%c%x", c, i);])
    USE_PTYS=1
    if test "$GCC" = yes; then
      CONF_CFLAGS="-fwritable-strings"
    else

dnl: Note: You might be able to build xboard even if your compiler does not
dnl: support ANSI C (-Aa).  xboard itself does not require ANSI C.  I don't
dnl: know whether the X header files on HP-UX require it.

      CONF_CFLAGS="-Aa -D_HPUX_SOURCE"
    fi

dnl: HP doesn't supply a full set of X header files and libraries.  People
dnl: often have some things installed in one place and some in another. 
dnl: AC_PATH_XTRA will find only one place, so we try to add all the
dnl: likely ones that might be missing here.  It might be better to
dnl: change AC_PATH_XTRA to try to extract this information from imake,
dnl: since folks who install the missing bits often configure their
dnl: imake to find them, but I don't want to delve into autoconf deeply
dnl: enough to make it do that right.

    if test -d /usr/include/X11R5; then
      X_CFLAGS="-I/usr/include/X11R5 $X_CFLAGS"
      X_LIBS="-L/usr/lib/X11R5 -L/usr/lib/X11R4 $X_LIBS"
    elif test -d /usr/include/X11R4; then
      X_CFLAGS="-I/usr/include/X11R4 $X_CFLAGS"
      X_LIBS="-L/usr/lib/X11R4 $X_LIBS"
    fi
    if test -d /MIT/X11R5/include; then
      X_CFLAGS="-I/MIT/X11R5/include $X_CFLAGS"
      X_LIBS="-L/MIT/X11R5/lib $X_LIBS"
    elif test -d /usr/local/include/X11R5; then
      X_CFLAGS="-I/usr/local/include/X11R5 $X_CFLAGS"
      X_LIBS="-L/usr/local/lib/X11R5 $X_LIBS"
    fi
  ;;

  romp-ibm-aix* )
    AC_DEFINE(IBMRTAIX, 1)
    USE_PTYS=1
  ;;

  i386-ibm-aix )
    USE_PTYS=1
    if test "$GCC" = yes; then
      CONF_CFLAGS="-fwritable-strings"
    fi
  ;;

  *-*-aix3* | *-*-bosx* )
    AC_DEFINE(PTY_ITERATION, [])
    AC_DEFINE(PTY_NAME_SPRINTF, [strcpy (pty_name, "/dev/ptc");])
    AC_DEFINE(PTY_TTY_NAME_SPRINTF, [strcpy (pty_name, ttyname (fd));])
    USE_PTYS=1
  ;;

  *-*-cxux* )
    AC_DEFINE(FIRST_PTY_LETTER, 'A')
    AC_DEFINE(LAST_PTY_LETTER, 'P')
    USE_PTYS=1
  ;;

  *-*-uniplus* )
    AC_DEFINE(UNIPLUS, 1)
    USE_PTYS=1
  ;;

  *-*-rtu* )
    AC_DEFINE(FIRST_PTY_LETTER, 'z')
    AC_DEFINE(PTY_TTY_NAME_SPRINTF,
        [sprintf (pty_name, "/dev/ttyp%x", i);])
    AC_DEFINE(PTY_NAME_SPRINTF,
	[sprintf (pty_name, "/dev/pty%x", i);])
    AC_DEFINE(RTU, 1)
  ;;

  *-*-iris* | *-*-irix3* )
    AC_DEFINE(PTY_ITERATION, [])
    AC_DEFINE(PTY_NAME_SPRINTF, [strcpy (pty_name, "/dev/ptc");])
    AC_DEFINE(PTY_TTY_NAME_SPRINTF,
	[sprintf (pty_name, "/dev/ttyq%d", minor(stb.st_rdev));])
    USE_PTYS=1
  ;;

  *-*-irix* )
    USE_PTYS=1
  ;;

  *-*-sunos4* | *-*-solaris1* )

dnl: Work around a bug in the SunOS 4.x linker.  Not needed if you have 
dnl:  patches 100512-02 and 100573-03 from Sun.

    if test "$GCC" = yes; then
      PRE_XMULIB="-static"
      POST_XMULIB="-dynamic"
    else
      PRE_XMULIB="-Bstatic"
      POST_XMULIB="-Bdynamic"
    fi
  ;;

  *-*-sunos5* | *-*-solaris2* )
    USE_PTYS=1

dnl: I'm not sure -lelf is needed, but it was in the old Imakefile.
dnl: The other libraries should all be found by Ac_PATH_XTRA or other
dnl: code above.

    X_LIBS="$X_LIBS -lelf"
  ;;

  *-*-sco* )
    AC_DEFINE(PTY_ITERATION, [for (i = 0; ; i++)])
    AC_DEFINE(PTY_NAME_SPRINTF, [sprintf (pty_name, "/dev/ptyp%d", i);])
    AC_DEFINE(PTY_TTY_NAME_SPRINTF, [sprintf (pty_name, "/dev/ttyp%d", i);])
    USE_PTYS=1
  ;;
	
  *-*-dynix* | *-*-ptx* )
    USE_PTYS=1
  ;;

  *-*-esix* )
    USE_PTYS=1
  ;;

  *-*-usg5-4* | *-*-sysvr4* )
    USE_PTYS=1
  ;;

  *-*-usg* | *-*-sysv* | *-*-aix* )
    USE_PTYS=1
  ;;

  vax-*-ultrix )
    if test "$GCC" = yes; then
      CONF_CFLAGS="-fwritable-strings"
    fi
  ;;
esac

AC_SUBST(PRE_XMULIB)
AC_SUBST(POST_XMULIB)
AC_SUBST(CONF_CFLAGS)
AC_SUBST(CONF_LDFLAGS)

AC_MSG_CHECKING(whether ptys or pipes should be used)
AC_ARG_ENABLE(ptys, 
[  --enable-ptys   Force use of pseudo-ttys to communicate with child processes
  --disable-ptys  Force use of pipes to communicate with child processes],
[if test "$enableval" = yes; then
  USE_PTYS=1
  AC_MSG_RESULT([ptys (user override)])
fi
if test "$enableval" = no; then
  USE_PTYS=0
  AC_MSG_RESULT([pipes (user override)])
fi],
[if test "$USE_PTYS" = 1; then
  AC_MSG_RESULT(ptys)
else
  AC_MSG_RESULT(pipes)
fi])
AC_DEFINE_UNQUOTED(USE_PTYS, $USE_PTYS)

AC_ARG_ENABLE(zippy, [],
[if test "$enableval" = yes; then
dnl: Extra software not included in the standard xboard distribution
  PATCHLEVEL="$PATCHLEVEL"-zippy
  AC_DEFINE(ZIPPY, 1)
  ZIPPY_O=zippy.o
  ZIPPY_H=zippy.h
fi])
AC_SUBST(ZIPPY_O)  
AC_SUBST(ZIPPY_H)  

AC_DEFINE_UNQUOTED(PRODUCT, "$PRODUCT")
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_DEFINE_UNQUOTED(PATCHLEVEL, "$PATCHLEVEL")
AC_SUBST(PRODUCT)
AC_SUBST(VERSION)
AC_SUBST(PATCHLEVEL)

AC_OUTPUT(Makefile comment.awk:comment.in cmail xboard.texinfo,
[test -z "$CONFIG_HEADERS" || date > stamp-h
chmod 755 comment.awk cmail
rm -f *.o
])
