# --*-sh-*---------------------------------------------------------------
#
# sysloc.in
#
# Maurice LeBrun
# IFS, University of Texas at Austin
# 14-Jul-1994
#
# This script sets up config variables for a Unix-like system.
# The stuff set here is fairly generic and can be automated.
# This includes how to find required header files and libraries.
# Includes code and ideas taken from the BLT (Tk extension) configure.
# -----------------------------------------------------------------------

# -----------------------------------------------------------------------
# Run xmkmf to check for location of X libraries and includes.
# If not found, I have to assume that X isn't available (it happens).
# Can disable explicitly by specifying --disable-xwin.
# -----------------------------------------------------------------------

AC_ENABLE(xwin, , enable_xwin="yes")

if test "$enable_xwin" = "yes"; then
    if test -z "$XINCDIR" -o -z "$XLIBDIR"; then
	AC_FIND_X

	XINCDIR="$x_includes"
	if test -z "$XINCDIR"; then
	    AC_TEST_CPP([#include <X11/Intrinsic.h>], [ XINCDIR="default" ], [
	    places="\
	    /usr/include \
	    /usr/include/X11 \
	    /usr/include/X11R5 \
	    /usr/include/X11R4 \
	    /usr/include/X11/X11R5 \
	    /usr/include/X11/X11R4 \
	    /usr/X11/include \
	    /usr/X386/include \
	    /usr/X11R5/include \
	    /usr/local/X11R5/include \
	    /usr/openwin/include \
	    /usr/local/X11R4/include \
	    /usr/include/X11R4 \
	    /usr/local/include \
	    /usr/unsupported/include"

	    for dir in $places; do
		if test -r "$dir/X11/Intrinsic.h"; then
		    XINCDIR="$dir"
		    break
		fi
	    done
	    if test -z "$XINCDIR"; then
		echo "Warning:  couldn't find any X11 include files."
		XINCDIR="default"
		enable_xwin="no"
	    fi ])
	fi
	if test "$XINCDIR" = "/usr/include"; then
	    XINCDIR="default"
	fi

	XLIBDIR="$x_libraries"
	if test -z "$XLIBDIR"; then
	    AC_HAVE_LIBRARY(X11, [ XLIBDIR="default" ], [
	    places="\
	    /lib \
	    /usr/lib \
	    /usr/lib/X11R5 \
	    /usr/X11/lib \
	    /usr/X386/lib \
	    /usr/X11R5/lib \
	    /usr/local/X11R5/lib \
	    /usr/openwin/lib \
	    /usr/local/X11R4/lib \
	    /usr/lib/X11R4 \ 
	    /usr/local/lib \
	    /usr/unsupported/lib"

	    for dir in $places; do
		if test -r "$dir/libX11.a"; then
		    XLIBDIR="$dir"
		    break
		fi
	    done
	    if test -z "$XLIBDIR"; then
		echo "Warning:  couldn't find the X11 library archive. "
		XLIBDIR="default"
	    fi ])
	fi
	if test "$XLIBDIR" = "/usr/lib"; then
	    XLIBDIR="default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# Make some guesses where the Tcl/Tk include files and libraries are.
# Look first in the Tcl/Tk distributions and then move on to the standard
# include file/library locations.  If it's still not found I assume it's
# not here.
# -----------------------------------------------------------------------

# -----------------------------------------------------------------------
# tcl.h
# -----------------------------------------------------------------------

AC_ENABLE(tcl, , enable_tcl=$enable_xwin)
header="tcl.h"

if test "$enable_tcl" = "yes"; then
    if test -z "$TCLINCDIR"; then
	echo "checking for $header"
	TCLINCDIR=""
	places="\
	$prefix/include \
	/usr/local/include \
	/usr/include/tcl \
	/usr/include \
	/usr/local/tcl7.3/include \
	$HOME/local/tcl7.3/include \
	/usr/local/tk3.6/include \
	$HOME/local/tk3.6/include \
	$HOME/local/include \
	$HOME/include \
	$PWD/../tcl7.3 \
	$XINCDIR/tk \
	$XINCDIR"

	for dir in $places; do
	    if test -r "$dir/$header"; then
		TCLINCDIR="$dir"
		break
	    fi
	done
	if test -z "$TCLINCDIR"; then
	    echo "can't find $header, disabling Tcl support. "
	    enable_tcl="no"
	fi
	if test "$TCLINCDIR" = "/usr/include"; then
	    TCLINCDIR= "default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# libtcl
# -----------------------------------------------------------------------

if test "$enable_tcl" = "yes"; then
    if test -z "$TCLLIBDIR"; then
	echo "checking for -ltcl"
	TCLLIBDIR=""
	places="\
	$prefix/lib \
	/usr/local/lib \
	/usr/local/tk3.6/lib \
	$HOME/local/tk3.6/lib \
	/usr/local/tcl7.3/lib \
	$HOME/local/tcl7.3/lib \
	$HOME/local/lib \
	$HOME/lib \
	$XLIBDIR \
	$PWD/../tcl7.3 \
	/usr/lib"

	for dir in $places; do
	    if test -r "$dir/libtcl.a" -o -r "$dir/libtcl.sl"; then
		TCLLIBDIR="$dir"
		break
	    fi
	done

	if test -z "$TCLLIBDIR"; then
	    echo "can't find Tcl library, disabling Tcl support. "
	    enable_tcl="no"
	fi
	if test "$TCLLIBDIR" = "/usr/lib"; then
	    TCLLIBDIR= "default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# dp.h
#
# If not in $TCLINCDIR, probably in the user's area.
# -----------------------------------------------------------------------

AC_ENABLE(dp, , enable_dp=$enable_tcl)

if test "$enable_dp" = "yes"; then
    if test -z "$DPINCDIR"; then
	echo "checking for dp.h"
	DPINCDIR=""
	places="\
	$prefix/include \
	$TCLINCDIR \
	$HOME/local/tcl7.3/include \
	$HOME/local/tk3.6/include \
	$HOME/local/include \
	$HOME/include \
	$PWD/../tcl-dp3.1 \
	$PWD/../tcl-dp3.2"

	for dir in $places; do
	    if test -r "$dir/dp.h"; then
		DPINCDIR="$dir"
		break
	    fi
	done
	if test -z "$DPINCDIR"; then
	    echo "can't find dp.h, disabling Tcl-DP support. "
	    enable_dp="no"
	fi
	if test "$DPINCDIR" = "/usr/include"; then
	    DPINCDIR= "default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# libdpnetwork (Tcl-DP)
# -----------------------------------------------------------------------

if test "$enable_dp" = "yes"; then
    if test -z "$DPLIBDIR"; then
	echo "checking for -ldpnetwork"
	DPLIBDIR=""
	places="\
	$prefix/lib \
	$TCLLIBDIR \
	/usr/local/lib \
	/usr/local/tk3.6/lib \
	$HOME/local/tk3.6/lib \
	$HOME/local/lib \
	$HOME/lib \
	$XLIBDIR \
	$PWD/../tcl-dp3.1 \
	$PWD/../tcl-dp3.2 \
	/usr/lib"

	for dir in $places; do
	    if test -r "$dir/libdpnetwork.a" -o -r "$dir/libdpnetwork.sl"; then
		DPLIBDIR="$dir"
		break
	    fi
	done
	if test -z "$DPLIBDIR"; then
	    echo "can't find Tcl-DP library, disabling Tcl-DP support. "
	    enable_dp="no"
	fi
	if test "$DPLIBDIR" = "/usr/lib"; then
	    DPLIBDIR="default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# itcl.h
#
# If not in $TCLINCDIR, probably in the user's area.
# -----------------------------------------------------------------------

AC_ENABLE(itcl, , enable_itcl=$enable_tcl)
header="itcl.h"

if test "$enable_itcl" = "yes"; then
    if test -z "$ITCLINCDIR"; then
	echo "checking for $header"
	ITCLINCDIR=""
	places="\
	$prefix/include \
	$TCLINCDIR \
	$HOME/local/tcl7.3/include \
	$HOME/local/tk3.6/include \
	$HOME/local/include \
	$HOME/include \
	$PWD/../itcl-1.5"

	for dir in $places; do
	    if test -r "$dir/$header"; then
		ITCLINCDIR="$dir"
		break
	    fi
	done
	if test -z "$ITCLINCDIR"; then
	    echo "can't find $header, disabling Itcl support. "
	    enable_itcl="no"
	fi
	if test "$ITCLINCDIR" = "/usr/include"; then
	    ITCLINCDIR= "default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# libitcl ([incr Tcl])
# -----------------------------------------------------------------------

if test "$enable_itcl" = "yes"; then
    if test -z "$ITCLLIBDIR"; then
	echo "checking for -litcl"
	ITCLLIBDIR=""
	places="\
	$prefix/lib \
	$TCLLIBDIR \
	/usr/local/lib \
	/usr/local/tk3.6/lib \
	$HOME/local/tk3.6/lib \
	$HOME/local/lib \
	$HOME/lib \
	$XLIBDIR \
	$PWD/../itcl-1.5 \
	/usr/lib"

	for dir in $places; do
	    if test -r "$dir/libitcl.a" -o -r "$dir/libitcl.sl"; then
		ITCLLIBDIR="$dir"
		break
	    fi
	done
	if test -z "$ITCLLIBDIR"; then
	    echo "can't find Itcl library, disabling Itcl support. "
	    enable_itcl="no"
	fi
	if test "$ITCLLIBDIR" = "/usr/lib"; then
	    ITCLLIBDIR="default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# tk.h
# -----------------------------------------------------------------------

AC_ENABLE(tk, , enable_tk=$enable_tcl)
header="tk.h"

if test "$enable_tk" = "yes"; then
    if test -z "$TKINCDIR"; then
	echo "checking for $header"
	TKINCDIR=""
	places="\
	$prefix/include \
	$TCLINCDIR \
	/usr/local/include \
	/usr/local/tk3.6/include \
	$HOME/local/tk3.6/include \
	$HOME/local/include \
	$HOME/include \
	$PWD/../tk3.6 \
	$XINCDIR/tk \
	$XINCDIR \
	/usr/include"

	for dir in $places; do
	    if test -r "$dir/$header"; then
		TKINCDIR="$dir"
		break
	    fi
	done
	if test -z "$TKINCDIR"; then
	    echo "can't find $header, disabling Tk support. "
	    enable_tk="no"
	fi
	if test "$TKINCDIR" = "/usr/include"; then
	    TKINCDIR="default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# libtk
# -----------------------------------------------------------------------

if test "$enable_tk" = "yes"; then
    if test -z "$TKLIBDIR"; then
	echo "checking for -ltk"
	TKLIBDIR=""
	places="\
	$prefix/lib \
	$TCLLIBDIR \
	/usr/local/lib \
	/usr/local/tk3.6/lib \
	$HOME/local/tk3.6/lib \
	$HOME/local/lib \
	$HOME/lib \
	$XLIBDIR \
	$PWD/../tk3.6 \
	/usr/lib"

	for dir in $places; do
	    if test -r "$dir/libtk.a" -o -r "$dir/libtk.sl"; then
		TKLIBDIR="$dir"
		break
	    fi
	done
	if test -z "$TKLIBDIR"; then
	    echo "can't find TK library, disabling Tk support. "
	    enable_tk="no"
	fi
	if test "$TKLIBDIR" = "/usr/lib"; then
	    TKLIBDIR="default"
	fi
    fi
fi

# -----------------------------------------------------------------------
# Assemble list of tcl/TK/X/whatever include directives.  These can go in
# any order.
#
# I go to a bit of trouble to eliminate redundant -I specifications (to
# keep the build line concise).
# -----------------------------------------------------------------------

inc_path=""

if test "$enable_xwin" = "yes"; then
    XINCSW=""
    if test "$XINCDIR" != "default"; then
	XINCSW="-I$XINCDIR"
    fi
    for dir in $inc_path; do
	if test "$XINCDIR" = "$dir"; then
	    XINCSW=""
	    break
	fi
    done
    INCS="$INCS $XINCSW"
    inc_path="$inc_path $XINCDIR"
fi

if test "$enable_tcl" = "yes"; then
    TCLINCSW=""
    if test "$TCLINCDIR" != "default"; then
	TCLINCSW="-I$TCLINCDIR"
    fi
    for dir in $inc_path; do
	if test "$TCLINCDIR" = "$dir"; then
	    TCLINCSW=""
	    break
	fi
    done
    INCS="$INCS $TCLINCSW"
    inc_path="$inc_path $TCLINCDIR"
fi

if test "$enable_dp" = "yes"; then
    DPINCSW=""
    if test "$DPINCDIR" != "default"; then
	DPINCSW="-I$DPINCDIR"
    fi
    for dir in $inc_path; do
	if test "$DPINCDIR" = "$dir"; then
	    DPINCSW=""
	    break
	fi
    done
    INCS="$INCS $DPINCSW"
    inc_path="$inc_path $DPINCDIR"
fi

if test "$enable_itcl" = "yes"; then
    ITCLINCSW=""
    if test "$ITCLINCDIR" != "default"; then
	ITCLINCSW="-I$ITCLINCDIR"
    fi
    for dir in $inc_path; do
	if test "$ITCLINCDIR" = "$dir"; then
	    ITCLINCSW=""
	    break
	fi
    done
    INCS="$INCS $ITCLINCSW"
    inc_path="$inc_path $ITCLINCDIR"
fi

if test "$enable_tk" = "yes"; then
    AC_SUBST(TKINCDIR)
    TKINCSW=""
    if test "$TKINCDIR" != "default"; then
	TKINCSW="-I$TKINCDIR"
    fi
    for dir in $inc_path; do
	if test "$TKINCDIR" = "$dir"; then
	    TKINCSW=""
	    break
	fi
    done
    INCS="$INCS $TKINCSW"
    inc_path="$inc_path $TKINCDIR"
fi

# -----------------------------------------------------------------------
# Assemble list of tcl/TK/X/whatever libraries.
# Note the library order CAN be important, depending on the system.  
#
# I go to a bit of trouble to eliminate redundant -L specifications (to
# keep the build line concise).
# -----------------------------------------------------------------------

lib_path=""

# The full Tcl-DP library depends on both Tk and Tcl so must come first.

if test "$enable_dp" = "yes"; then
    DPLIBSW=""
    if test "$DPLIBDIR" != "default"; then
	DPLIBSW="-L$DPLIBDIR"
    fi
    for dir in $lib_path; do
	if test "$DPLIBDIR" = "$dir"; then
	    DPLIBSW=""
	    break
	fi
    done
    LIBS="$LIBS $DPLIBSW -ldpnetwork"
    lib_path="$lib_path $DPLIBDIR"
fi

# Tk depends on Tcl and X11 so must come before both.

if test "$enable_tk" = "yes"; then
    TKLIBSW=""
    if test "$TKLIBDIR" != "default"; then
	TKLIBSW="-L$TKLIBDIR"
    fi
    for dir in $lib_path; do
	if test "$TKLIBDIR" = "$dir"; then
	    TKLIBSW=""
	    break
	fi
    done
    LIBS="$LIBS $TKLIBSW -ltk"
    lib_path="$lib_path $TKLIBDIR"
fi

# Itcl depends on Tcl so must come before it.

if test "$enable_itcl" = "yes"; then
    ITCLLIBSW=""
    if test "$ITCLLIBDIR" != "default"; then
	ITCLLIBSW="-L$ITCLLIBDIR"
    fi
    for dir in $lib_path; do
	if test "$ITCLLIBDIR" = "$dir"; then
	    ITCLLIBSW=""
	    break
	fi
    done
    LIBS="$LIBS $ITCLLIBSW -litcl"
    lib_path="$lib_path $ITCLLIBDIR"
fi

# Tcl and X11 can be put in either order.

if test "$enable_tcl" = "yes"; then
    TCLLIBSW=""
    if test "$TCLLIBDIR" != "default"; then
	TCLLIBSW="-L$TCLLIBDIR"
    fi
    for dir in $lib_path; do
	if test "$TCLLIBDIR" = "$dir"; then
	    TCLLIBSW=""
	    break
	fi
    done
    LIBS="$LIBS $TCLLIBSW -ltcl"
    lib_path="$lib_path $TCLLIBDIR"
fi

if test "$enable_xwin" = "yes"; then
    XLIBSW=""
    if test "$XLIBDIR" != "default"; then
	XLIBSW="-L$XLIBDIR"
    fi
    for dir in $lib_path; do
	if test "$XLIBDIR" = "$dir"; then
	    XLIBSW=""
	    break
	fi
    done
    LIBS="$LIBS $XLIBSW -lX11"
    lib_path="$lib_path $XLIBDIR"
fi

#--------------------------------------------------------------------
# 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.
#
# Most of this is copied from the BLT configure script.  Dunno if the
# Xbsd library is really needed.  libsocket and libnsl are needed on
# some SysV systems.  libieee.a is supposed to be a Linux deal, dunno if
# it's important either.
#
# On some systems the linker doesn't return an error code to the shell if
# you try to link with nonexistant libraries, so need to handle these
# explicitly.
#--------------------------------------------------------------------

if test -z "$EXTRA_LIBS"; then
    EXTRA_LIBS=
    case $system in 
	CRAY* ) 
	    EXTRA_LIBS="$EXTRA_LIBS -lm"
	;;
	* ) 	
	    AC_HAVE_LIBRARY(Xbsd,   [EXTRA_LIBS="$EXTRA_LIBS -lXbsd"])
	    AC_HAVE_LIBRARY(socket, [EXTRA_LIBS="$EXTRA_LIBS -lsocket"])
	    AC_HAVE_LIBRARY(nsl,    [EXTRA_LIBS="$EXTRA_LIBS -lnsl"])
	    AC_HAVE_LIBRARY(ieee,   [EXTRA_LIBS="$EXTRA_LIBS -lieee"])
	    AC_HAVE_LIBRARY(m,      [EXTRA_LIBS="$EXTRA_LIBS -lm"])
	;;
    esac
fi
LIBS="$LIBS $EXTRA_LIBS"

AC_SUBST(EXTRA_LIBS)
AC_SUBST(LIBS)

# -----------------------------------------------------------------------
# libf2c
# -----------------------------------------------------------------------

if test "$with_f2c" = "yes"; then
    AC_HAVE_LIBRARY(f2c, [LIBS="$LIBS -lf2c"],
    [echo "Warning:  couldn't find libf2c."])
fi

AC_SUBST(INCS)
AC_SUBST(LIBS)
