dnl	This file is an input file used by the GNU "autoconf" program to
dnl	generate the file "configure", which is run during Tk installation
dnl	to configure the system for the local environment.

AC_INIT(tree.c)
# RCS: $Id$

AC_PREREQ(2.0)
AC_C_CROSS

# -----------------------------------------------------------------------
#   Set up a new default --prefix.  If a previous installation of
#   [incr Tcl] can be found searching $PATH use that directory.
# -----------------------------------------------------------------------

AC_PREFIX_DEFAULT(/usr/local/itcl)
AC_PREFIX_PROGRAM(itkwish)

# -----------------------------------------------------------------------
#   C compiler and debugging flags
# -----------------------------------------------------------------------
AC_MSG_CHECKING([which C compiler])
if test -z "$CC" ; then
    CC="cc"
fi
AC_ARG_WITH(cc, [  --with-cc=CC            set C compiler to CC],
    [CC=$with_cc])
AC_MSG_RESULT([$CC])
AC_SUBST(CC)
AC_ISC_POSIX

AC_PROG_CPP

AC_MSG_CHECKING([whether C compiler is gcc])
AC_CACHE_VAL(itcl_cv_prog_gcc,
    AC_EGREP_CPP(_cc_is_gcc_, [
#ifdef __GNUC__
_cc_is_gcc_
#endif
],      [itcl_cv_prog_gcc=yes], [itcl_cv_prog_gcc=no]))
AC_MSG_RESULT([$itcl_cv_prog_gcc])

AC_MSG_CHECKING([default compiler flags])
if test -z "$CFLAGS" ; then
    CFLAGS="-O"
fi
AC_ARG_WITH(cflags, [  --with-cflags=FLAGS     set compiler flags to FLAGS],
    [CFLAGS="$with_cflags"])
if test "$itcl_cv_prog_gcc" = "yes" ; then
    CFLAGS="$CFLAGS -Wshadow -Wtraditional -Wall"
fi
AC_MSG_RESULT([$CFLAGS])
AC_SUBST(CFLAGS)

AC_PROG_INSTALL
AC_PROG_RANLIB

#--------------------------------------------------------------------
#   Supply substitutes for missing POSIX header files.
#   Replacements are handled in "tclInt.h" which we include here.
#--------------------------------------------------------------------

AC_CHECK_HEADER(limits.h, , AC_DEFINE(NO_LIMITS_H))
AC_CHECK_HEADER(stdlib.h, tcl_ok=1, tcl_ok=0)
AC_EGREP_HEADER(strtol, stdlib.h, , tcl_ok=0)
AC_EGREP_HEADER(strtoul, stdlib.h, , tcl_ok=0)
AC_EGREP_HEADER(strtod, stdlib.h, , tcl_ok=0)
if test $tcl_ok = 0; then
    AC_DEFINE(NO_STDLIB_H)
fi
AC_CHECK_HEADER(string.h, tcl_ok=1, tcl_ok=0)
AC_EGREP_HEADER(strstr, string.h, , tcl_ok=0)
AC_EGREP_HEADER(strerror, string.h, , tcl_ok=0)
if test $tcl_ok = 0; then
    AC_DEFINE(NO_STRING_H)
fi

#--------------------------------------------------------------------
#	Check for various typedefs and provide substitutes if
#	they don't exist.
#--------------------------------------------------------------------

AC_MODE_T
AC_PID_T
AC_SIZE_T
AC_UID_T

# -----------------------------------------------------------------------
#
# 	Make some guesses where the [incr Tcl] installation is.
# 	Look in the usual places.  If it's still not found then
#	query for it.
#
# -----------------------------------------------------------------------

echo "checking for [incr Tcl]"
ITCL_DIR=""
places="$prefix \
    /usr/local/itcl \
    /usr/local/itcl/itcl2.0 \
    /usr/local \
    /usr"
for dir in $places; do
    if test -r $dir/include/itcl.h -a -r $dir/lib/libitcl2.0.a ; then
        ITCL_DIR=$dir
        break
    fi
done
while test -z "$ITCL_DIR" ; do
    echo "What directory contains the itcl2.0 installation (absolute path) ?"
    read ITCL_DIR
done

AC_SUBST(ITCL_DIR)

#--------------------------------------------------------------------
#	Locate the X11 header files and the X11 library archive.  Try
#	the ac_path_x macro first, but if it doesn't find the X stuff
#	(e.g. because there's no xmkmf program) then check through
#	a list of possible directories.  Under some conditions the
#	autoconf macro will return an include directory that contains
#	no include files, so double-check its result just to be safe.
#--------------------------------------------------------------------

AC_PATH_X
not_really_there=""
if test "$no_x" = ""; then
    if test "$x_includes" = ""; then
        AC_TRY_CPP([#include <X11/XIntrinsic.h>], , not_really_there="yes")
    else
        if test ! -r $x_includes/X11/Intrinsic.h; then
            not_really_there="yes"
        fi
    fi
fi
if test "$no_x" = "yes" -o "$not_really_there" = "yes"; then
    echo checking for X11 header files
    XINCLUDES="# no special path needed"
    AC_TRY_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 /usr/sww/include"
        for i in $dirs ; do
            if test -r $i/X11/Intrinsic.h; then
                XINCLUDES=" -I$i"
            fi
        done
    fi
else
    if test "$x_includes" != ""; then
        XINCLUDES=-I$x_includes
    else
        XINCLUDES="# no special path needed"
    fi
fi
if test "$XINCLUDES" = nope; then
  echo "Warning:  couldn't find any X11 include files."
  XINCLUDES="# no include files found"
fi
AC_SUBST(XINCLUDES)

if test "$no_x" = yes; then
    XLIBSW=nope
    if test "$XLIBSW" = nope; then
        dirs="/usr/unsupported/lib /usr/local/lib /usr/X386/lib /usr/lib/X11R4 /usr/X11R5/lib /usr/lib/X11R5 /usr/openwin/lib /usr/X11/lib /usr/sww/X11/lib"
        for i in $dirs ; do
            if test -r $i/libX11.a; then
                XLIBSW="-L$i -lX11"
            fi
        done
    fi
else
    if test "$x_libraries" = ""; then
        XLIBSW=-lX11
    else
        XLIBSW="-L$x_libraries -lX11"
    fi
fi
if test "$XLIBSW" = nope ; then
    AC_CHECK_LIB(Xwindow, XCreateWindow, XLIBSW=-lXwindow)
fi
if test "$XLIBSW" = nope ; then
    echo "Warning:  couldn't find the X11 library archive.  Using -lX11."
    XLIBSW=-lX11
fi
AC_SUBST(XLIBSW)

#--------------------------------------------------------------------
#   Check for the existence of various libraries.  The order here
#   is important, so that then end up in the right order in the
#   command line generated by make.  The -lsocket and -lnsl libraries
#   require a couple of special tricks:
#   1. Use "connect" and "accept" to check for -lsocket, and
#      "gethostbyname" to check for -lnsl.
#   2. Use each function name only once:  can't redo a check because
#      autoconf caches the results of the last check and won't redo it.
#   3. Use -lnsl and -lsocket only if they supply procedures that
#      aren't already present in the normal libraries.  This is because
#      IRIX 5.2 has libraries, but they aren't needed and they're
#      bogus:  they goof up name resolution if used.
#   4. On some SVR4 systems, can't use -lsocket without -lnsl too.
#      To get around this problem, check for both libraries together
#      if -lsocket doesn't work by itself.
#--------------------------------------------------------------------

AC_CHECK_LIB(Xbsd, main, [LIBS="$LIBS -lXbsd"])

tk_checkBoth=0
AC_CHECK_FUNC(connect, tk_checkSocket=0, tk_checkSocket=1)
if test "$tk_checkSocket" = 1; then
    AC_CHECK_LIB(socket, main, LIBS="$LIBS -lsocket", tk_checkBoth=1)
fi
if test "$tk_checkBoth" = 1; then
    tk_oldLibs=$LIBS
    LIBS="$LIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, tk_checkNsl=0, [LIBS=$tk_oldLibs])
fi
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [LIBS="$LIBS -lnsl"]))

#--------------------------------------------------------------------
#   On a few very rare systems, all of the libm.a stuff is
#   already in libc.a.  Set compiler flags accordingly.
#   Also, Linux requires the "ieee" library for math to
#   work right (and it must appear before "-lm").
#--------------------------------------------------------------------

MATH_LIBS=""
AC_CHECK_FUNC(sin, , MATH_LIBS="-lm")
AC_CHECK_LIB(ieee, main, [MATH_LIBS="-lieee $MATH_LIBS"])
AC_SUBST(MATH_LIBS)

AC_OUTPUT(Makefile)
