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

#--------------------------------------------------------------------
# PROGRAM NAME/VERSION
#--------------------------------------------------------------------
ROOTNAME=itcl
VERSION=1.5

AC_INIT(src/itcl_core.c)

AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PROG_CPP

CC=${CC-cc}
CFLAGS=${CFLAGS--O}
AC_SUBST(CC)
AC_SUBST(CFLAGS)

AC_SUBST(ROOTNAME)
AC_SUBST(VERSION)

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

AC_MODE_T
AC_PID_T
AC_SIZE_T
AC_UID_T

# -----------------------------------------------------------------------
#   Set PWD variable if vendor's /bin/sh doesn't
# -----------------------------------------------------------------------
if test -z "$PWD" ; then
	PWD=`pwd`
fi

#--------------------------------------------------------------------
# Check "uname" to determine system type
#--------------------------------------------------------------------
AC_PROGRAM_CHECK(uname_found, uname, 1, 0)
if test $uname_found -eq 0 ; then
    echo "== System type?"
    read system
else
    system=`uname -s`-`uname -r`
fi

#--------------------------------------------------------------------
# Locate the X11 header files and the X11 library archive.  Try
# the ac_find_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.
#--------------------------------------------------------------------

AC_PROGRAM_CHECK(tk_ok, xmkmf, 1, 0)
if test $tk_ok = 1; then
    AC_FIND_X
fi
if test "$x_includes" = /usr/include; then
    XINCLUDES="# no special path needed"
elif test "$x_includes" != ""; then
    XINCLUDES=" -I$x_includes"
else
    echo checking for X11 header files
    XINCLUDES="# no special path needed"
    AC_TEST_CPP([#include <X11/Intrinsic.h>], , XINCLUDES="nope")
    if test "$XINCLUDES" = nope; then
        dirs=${XINCLUDE_DIR-"/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
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 "$x_libraries" = /usr/lib; then
    XLIBSW=-lX11
elif test "$x_libraries" != ""; then
    XLIBSW="-L$x_libraries -lX11"
else
    echo "checking for X11 library archive"
    AC_HAVE_LIBRARY(X11, XLIBSW="-lX11", XLIBSW=nope)
    if test "$XLIBSW" = nope; then
	dirs=${XLIBRARY_DIR-"/usr/unsupported/lib /usr/local/lib /usr/X386/lib /usr/lib/X11R4 /usr/X11R5/lib /usr/lib/X11R5 /usr/openwin/lib /usr/X11/lib"}
	for i in $dirs ; do
	    if test -r $i/libX11.a; then
		XLIBSW="-L$i -lX11"
	    fi
	done
    fi
fi
if test "$XLIBSW" = nope ; then
    AC_HAVE_LIBRARY(Xwindow, 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.
#--------------------------------------------------------------------
AC_HAVE_LIBRARY(m, [LIBS="$LIBS -lm"])

#--------------------------------------------------------------------
# Search for the Tcl source code, to get access to "tclInt.h".
# Source code probably resides in a sibling directory at this
# same level.  Always look for symbolic links like "tcl" first,
# in case these are set up to point to the latest version.
#--------------------------------------------------------------------
echo "checking for tclInt.h"
TCL_SRCDIR=""
places="$PWD/../tcl \
	$PWD/../tcl7.3 \
	$PWD/../tcl7.2 \
	$PWD/../tcl7.1 \
	$PWD/../tcl7.0 \
	$prefix/include \
	$XINCDIR/tcl \
	$XINCDIR \
	/usr/local/include \
	/usr/include"
for dir in $places; do
	if test -r $dir/tclInt.h ; then
		TCL_SRCDIR=$dir
		break
	fi
done

if test -z "$TCL_SRCDIR" ; then
	echo "== [incr Tcl] needs access to Tcl source code."

	echo "== Directory containing Tcl source code? (absolute path)"
	read TCL_SRCDIR
	if test ! -r $TCL_SRCDIR/tclInt.h ; then
		echo "== Cannot find Tcl source code in this directory"
		echo "== Check your Tcl installation and try again"
		exit 1
	fi
fi
echo "setting TCL_SRCDIR as $TCL_SRCDIR"
AC_SUBST(TCL_SRCDIR)

#--------------------------------------------------------------------
# libtcl.a
#--------------------------------------------------------------------
echo "checking for libtcl.a"
TCL_LIBDIR=""
places="$PWD/../tcl \
	$PWD/../tcl7.3 \
	$PWD/../tcl7.2 \
	$PWD/../tcl7.1 \
	$PWD/../tcl7.0 \
	$prefix/lib \
	$XINCDIR/tcl \
	$XINCDIR \
	$TCL_SRCDIR \
	/usr/local/lib \
	/usr/lib"
for dir in $places; do
	if test -r $dir/libtcl.a ; then
		TCL_LIBDIR=$dir
		break
	fi
done

if test -z "$TCL_LIBDIR" ; then
	echo "== Directory containing libtcl.a? (absolute path)"
	read TCL_LIBDIR
fi
echo "setting TCL_LIBDIR as $TCL_LIBDIR"
AC_SUBST(TCL_LIBDIR)

#--------------------------------------------------------------------
# tk.h
#--------------------------------------------------------------------
echo "checking for tk.h"
TK_INCDIR=""
places="$PWD/../tk \
	$PWD/../tk3.6 \
	$PWD/../tk3.5 \
	$PWD/../tk3.4 \
	$PWD/../tk3.3 \
	$prefix/include \
	$XINCDIR/tk \
	$XINCDIR \
	/usr/local/include \
	/usr/include"
for dir in $places; do
	if test -r $dir/tk.h ; then
		TK_INCDIR=$dir
		break
	fi
done

if test -z "$TK_INCDIR" ; then
	echo "== Directory containing tk.h? (absolute path)"
	read TK_INCDIR
fi
echo "setting TK_INCDIR as $TK_INCDIR"
AC_SUBST(TK_INCDIR)

#--------------------------------------------------------------------
# libtk.a
#--------------------------------------------------------------------
echo "checking for libtk.a"
TK_LIBDIR=""
places="$PWD/../tk \
	$PWD/../tk3.6 \
	$PWD/../tk3.5 \
	$PWD/../tk3.4 \
	$PWD/../tk3.3 \
	$prefix/include \
	$XINCDIR/tk \
	$XINCDIR \
	$TK_INCDIR/../lib \
	/usr/local/include \
	/usr/include"
for dir in $places; do
	if test -r $dir/libtk.a ; then
		TK_LIBDIR=$dir
		break
	fi
done

if test -z "$TK_LIBDIR" ; then
	echo "== Directory containing libtk.a? (absolute path)"
	read TK_LIBDIR
fi
echo "setting TK_LIBDIR as $TK_LIBDIR"
AC_SUBST(TK_LIBDIR)

#--------------------------------------------------------------------
# See if shared libraries can and should be built
#--------------------------------------------------------------------
SHLIB=""
SHLIB_CCFLAGS=""
SHLIB_LDFLAGS=""
SHLIB_LOADER=""
SHLIB_SUFFIX=""

case $system in
    SunOS-4*)
		SHLIB="libitcl.so.$(VERSION)"
		SHLIB_CCFLAGS="-pic"
		SHLIB_LDFLAGS="-assert pure-text"
		SHLIB_LOADER="ld"
		SHLIB_SUFFIX=".so.$(VERSION)"
		;;
	HP-UX-*.09.*)
		SHLIB="libitcl.sl"
		SHLIB_CCFLAGS="+z"
		SHLIB_LDFLAGS="-b -n"
		SHLIB_LOADER="ld"
		SHLIB_SUFFIX=".sl"
		;;
	SunOS-5*)
		SHLIB="libitcl.so.$(VERSION)"
		SHLIB_CCFLAGS="-K pic"
		SHLIB_LDFLAGS='-G -ztext -h $(SHARED_LIBRARY)'
		SHLIB_LOADER="$CC"
		SHLIB_SUFFIX=".so.$(VERSION)"
		;;
	OSF-1.*)
		SHLIB="libitcl.so.$(VERSION)"
		SHLIB_CCFLAGS="-fpic"
		SHLIB_LDFLAGS="-shared"
		SHLIB_LOADER="$CC"
		SHLIB_SUFFIX=".so.$(VERSION)"
		;;
	IRIX-5.*)
		SHLIB="libitcl.so.$(VERSION)"
		SHLIB_CCFLAGS="-KPIC"
		SHLIB_LDFLAGS="-shared"
		SHLIB_LOADER="$CC"
		SHLIB_SUFFIX=".so.$(VERSION)"
		;;
	*)
		echo "== Don't know how to build shared libraries for $system"
		;;
esac
if test ! -z "SHLIB_CCFLAGS" ; then
	if test "$compiler" = "gcc" ; then
		SHLIB_CCFLAGS="-fpic"
	fi
fi

if test -n "$SHLIB" ; then
	echo "== Build shared library? (y/n)"
	read shared
	if test "$shared" != "y" ; then
		SHLIB=""
	fi
fi

AC_SUBST(SHLIB)
AC_SUBST(SHLIB_CCFLAGS)
AC_SUBST(SHLIB_LDFLAGS)
AC_SUBST(SHLIB_LOADER)
AC_SUBST(SHLIB_SUFFIX)

AC_OUTPUT(Makefile src/Makefile man/Makefile library/Makefile)
