dnl ########################################################################
dnl Find the Apache extension builder.

AC_DEFUN(MAS_PATH_APXS, [

    AC_SUBST(APXS)
    AC_SUBST(APACHE_BASE)

    AC_CACHE_CHECK([for apxs], mas_cv_path_apxs, [
	mas_cv_path_apxs=no

	AC_ARG_WITH(apxs, [  --with-apxs=PATH        path to apxs program], [
	    mas_cv_path_apxs="$withval"
	    if test ! -x "$mas_cv_path_apxs"; then
		AC_MSG_ERROR([$mas_cv_path_apxs is not an executable file.])
	    fi
	])

	if test "$mas_cv_path_apxs" = no; then
	    AC_PATH_PROG(mas_cv_path_apxs, apxs, no)
	fi

	if test "$mas_cv_path_apxs" = no; then
	    AC_MSG_ERROR([apxs not found.  Make sure it is in your PATH.])
	fi

    ])

    APXS="$mas_cv_path_apxs"
    apache_include_path="`$APXS -q includedir`"
    if test x"$apache_include_path" != x; then
	CPPFLAGS="$CPPFLAGS -I`$APXS -q includedir`"
    fi
    APACHE_BASE="`$APXS -q prefix`"
])

dnl ########################################################################
dnl Find the MM library.

AC_DEFUN(MAS_ADD_MM_TO_FLAGS,[
    if test x"$1" != x; then
	CPPFLAGS="$CPPFLAGS -I$1/include"
	LIBS="$LIBS -L$1/lib"
    fi
    LIBS="$LIBS -lmm"
])

AC_DEFUN(MAS_TRY_MM_PATH,[
    saved_cppflags="$CPPFLAGS"
    saved_libs="$LIBS"
    MAS_ADD_MM_TO_FLAGS($1)
    AC_TRY_CPP([#include <mm.h>],[
	AC_TRY_LINK_FUNC(mm_malloc,[mas_cv_path_mm="$1"])
    ])
    LIBS="$saved_libs"
    CPPFLAGS="$saved_cppflags"
])

AC_DEFUN(MAS_PATH_MM,[
    AC_MSG_CHECKING([for MM library])

    AC_CACHE_VAL(mas_cv_path_mm,[
	mas_cv_path_mm=no

	AC_ARG_WITH(mm, [  --with-mm=DIR     path where MM is installed, e.g. /usr/local],
	[
	    MAS_TRY_MM_PATH($withval)
	])

	if test "$mas_cv_path_mm" = no; then
	    MAS_TRY_MM_PATH()
	fi

	if test "$mas_cv_path_mm" = no; then
	    MAS_TRY_MM_PATH(/usr/local)
	fi
    ])

    if test "$mas_cv_path_mm" = no; then
	AC_MSG_ERROR([MM library not found])
    fi

    MAS_ADD_MM_TO_FLAGS($mas_cv_path_mm)

    AC_MSG_RESULT($mas_cv_path_mm)
])

dnl ########################################################################
dnl Figure out where PostgreSQL lives.

AC_DEFUN(MAS_ADD_POSTGRES_TO_FLAGS,[
    if test x"$1" != x; then
	CPPFLAGS="$CPPFLAGS -I$1/include"
	LIBS="$LIBS -L$1/lib"
    else
	CPPFLAGS="$CPPFLAGS -I/usr/include/pgsql"
    fi
    LIBS="$LIBS -lpq"
])

AC_DEFUN(MAS_TRY_POSTGRES_PATH,[
    MAS_ADD_POSTGRES_TO_FLAGS($1)
    AC_TRY_CPP([#include <libpq-fe.h>],[
	AC_TRY_LINK([],[PQsetdbLogin();],[mas_cv_path_postgres="$1"])
    ])
])

AC_DEFUN(MAS_PATH_POSTGRES, [

    AC_MSG_CHECKING([for PostgreSQL])

    AC_CACHE_VAL(mas_cv_path_postgres,[
	mas_cv_path_postgres=no

	AC_ARG_WITH(postgres, [  --with-postgres=DIR     path to PostgreSQL installation if using PSQL driver],
	[
	    saved_cppflags="$CPPFLAGS"
	    saved_libs="$LIBS"
	    MAS_TRY_POSTGRES_PATH($withval)
	    CPPFLAGS="$saved_cppflags"
	    LIBS="$saved_libs"
	])

	if test "$mas_cv_path_postgres" = no; then
	    saved_cppflags="$CPPFLAGS"
	    saved_libs="$LIBS"
	    MAS_TRY_POSTGRES_PATH()
	    CPPFLAGS="$saved_cppflags"
	    LIBS="$saved_libs"
	fi

	if test "$mas_cv_path_postgres" = no; then
	    saved_cppflags="$CPPFLAGS"
	    saved_libs="$LIBS"
	    MAS_TRY_POSTGRES_PATH(/usr/local)
	    CPPFLAGS="$saved_cppflags"
	    LIBS="$saved_libs"
	fi
    ])

    if test "$mas_cv_path_postgres" = no; then
	AC_MSG_ERROR([PostgreSQL not found])
    fi

    MAS_ADD_POSTGRES_TO_FLAGS($mas_cv_path_postgres)

    AC_MSG_RESULT($mas_cv_path_postgres)
])

dnl ########################################################################
dnl Figure out where Oracle lives.

AC_DEFUN(MAS_ADD_ORACLE_TO_FLAGS,[
    if test x"$1" != x; then
	CPPFLAGS="$CPPFLAGS -I$1/rdbms/demo -I$1/rdbms/public -I$1/network/public -I$1/plsql/public"
	LIBS="$LIBS -L$1/lib"
    fi
    LIBS="$LIBS -lclntsh -lcore8 -lcommon8 -lgeneric8 -lclient8"
])

AC_DEFUN(MAS_TRY_ORACLE_PATH,[
    MAS_ADD_ORACLE_TO_FLAGS($1)
    AC_TRY_CPP([#include <oci.h>],[
	AC_TRY_LINK([],[OCIInitialize();],[mas_cv_path_oracle="$1"])
    ])
])

AC_DEFUN(MAS_PATH_ORACLE, [

    AC_MSG_CHECKING([for Oracle8])

    AC_CACHE_VAL(mas_cv_path_oracle,[
	mas_cv_path_oracle=no

	AC_ARG_WITH(ora8, [  --with-ora8=DIR         path to Oracle8 installation if using Oracle driver
                          [${ORACLE_HOME-none}]],
	[
	    saved_cppflags="$CPPFLAGS"
	    saved_libs="$LIBS"
	    MAS_TRY_ORACLE_PATH($withval)
	    CPPFLAGS="$saved_cppflags"
	    LIBS="$saved_libs"
	])

	if test "$mas_cv_path_oracle" = no; then
	    saved_libs="$LIBS"
	    MAS_TRY_ORACLE_PATH()
	    LIBS="$saved_libs"
	fi

	if test "$mas_cv_path_oracle" = no; then
	    saved_cppflags="$CPPFLAGS"
	    saved_libs="$LIBS"
	    MAS_TRY_ORACLE_PATH($ORACLE_HOME)
	    CPPFLAGS="$saved_cppflags"
	    LIBS="$saved_libs"
	fi
    ])

    if test "$mas_cv_path_oracle" = no; then
	AC_MSG_ERROR([Oracle8 not found])
    fi

    MAS_ADD_ORACLE_TO_FLAGS($mas_cv_path_oracle)

    AC_MSG_RESULT($mas_cv_path_oracle)
])

dnl ########################################################################
dnl Find Tcl.

AC_DEFUN(SC_PATH_TCLCONFIG, [
    #
    # Ok, lets find the tcl configuration
    # First, look for one uninstalled.
    # the alternative search directory is invoked by --with-tcl
    #

    if test x"${no_tcl}" = x ; then
	# we reset no_tcl in case something fails here
	no_tcl=true
	AC_ARG_WITH(tcl, [  --with-tcl=DIR          directory containing Tcl configuration (tclConfig.sh)], with_tclconfig=${withval})
	AC_MSG_CHECKING([for Tcl configuration])
	AC_CACHE_VAL(ac_cv_c_tclconfig,[

	    # First check to see if --with-tclconfig was specified.
	    if test x"${with_tclconfig}" != x ; then
		if test -f "${with_tclconfig}/tclConfig.sh" ; then
		    ac_cv_c_tclconfig=`(cd ${with_tclconfig}; pwd)`
		else
		    AC_MSG_ERROR([${with_tclconfig} directory doesn't contain tclConfig.sh])
		fi
	    fi

	    # then check for a private Tcl installation
	    if test x"${ac_cv_c_tclconfig}" = x ; then
		for i in \
			../tcl \
			`ls -dr ../tcl[[8-9]].[[0-9]]* 2>/dev/null` \
			../../tcl \
			`ls -dr ../../tcl[[8-9]].[[0-9]]* 2>/dev/null` \
			../../../tcl \
			`ls -dr ../../../tcl[[8-9]].[[0-9]]* 2>/dev/null` ; do
		    if test -f "$i/unix/tclConfig.sh" ; then
			ac_cv_c_tclconfig=`(cd $i/unix; pwd)`
			break
		    fi
		done
	    fi

	    # check in a few common install locations
	    if test x"${ac_cv_c_tclconfig}" = x ; then
		for i in `ls -d ${prefix}/lib 2>/dev/null` \
			`ls -d /usr/local/lib 2>/dev/null` ; do
		    if test -f "$i/tclConfig.sh" ; then
			ac_cv_c_tclconfig=`(cd $i; pwd)`
			break
		    fi
		done
	    fi

	    # check in a few other private locations
	    if test x"${ac_cv_c_tcliconfig}" = x ; then
		for i in \
			${srcdir}/../tcl \
			`ls -dr ${srcdir}/../tcl[[8-9]].[[0-9]]* 2>/dev/null` ; do
		    if test -f "$i/unix/tclConfig.sh" ; then
		    ac_cv_c_tclconfig=`(cd $i/unix; pwd)`
		    break
		fi
		done
	    fi
	])

	if test x"${ac_cv_c_tclconfig}" = x ; then
	    TCL_BIN_DIR="# no Tcl configs found"
	    AC_MSG_WARN(Can't find Tcl configuration definitions)
	    exit 0
	else
	    no_tcl=
	    TCL_BIN_DIR=${ac_cv_c_tclconfig}
	    AC_MSG_RESULT(found $TCL_BIN_DIR/tclConfig.sh)
	fi
    fi
])

AC_DEFUN(SC_LOAD_TCLCONFIG, [
    AC_MSG_CHECKING([for existence of $TCL_BIN_DIR/tclConfig.sh])

    if test -f "$TCL_BIN_DIR/tclConfig.sh" ; then
        AC_MSG_RESULT([loading])
	. $TCL_BIN_DIR/tclConfig.sh
    else
        AC_MSG_RESULT([file not found])
    fi

    #
    # The eval is required to do the TCL_DBGX substitution in the
    # TCL_LIB_FILE variable
    #

    eval TCL_LIB_FILE=${TCL_LIB_FILE}
    eval TCL_LIB_FLAG=${TCL_LIB_FLAG}
    eval "TCL_LIBS=\"${TCL_LIBS}\""
    eval "TCL_LIB_SPEC=\"${TCL_LIB_SPEC}\""

    AC_SUBST(TCL_BIN_DIR)
    AC_SUBST(TCL_SRC_DIR)
    AC_SUBST(TCL_LIB_FILE)
    AC_SUBST(TCL_LIBS)
    AC_SUBST(TCL_LIB_SPEC)
    AC_SUBST(TCL_SHLIB_SUFFIX)

    CPPFLAGS="$CPPFLAGS -I$TCL_PREFIX/include"
])