SC_PATH_TCLCONFIG
SC_LOAD_TCLCONFIG

AC_MSG_CHECKING([for a compatible version of Tcl])
if test "${TCL_MAJOR_VERSION}" -gt 8  \
  -o \( "${TCL_MAJOR_VERSION}" -eq 8 -a "${TCL_MINOR_VERSION}" -ge 4 \); then
    AC_MSG_RESULT([Tcl ${TCL_VERSION}${TCL_PATCH_LEVEL}])

    if test "$aquatcl" = yes; then
        echo "Using AquaTCL"
        V_DEFINE="$V_DEFINE -DMAC_OSX_TCL"
    fi

elif test "${TCL_VERSION}" == "8.0" ; then
    if test -f "${TCL_SRC_DIR}/generic/tcl2c.c" ; then
        AC_MSG_RESULT([UCL Tcl ${TCL_VERSION}${TCL_PATCH_LEVEL}])
    else
        AC_MSG_ERROR([Found Tcl 8.0 which is not UCL Tcl 8.0])
    fi

else
    AC_MSG_ERROR([Tcl $TCL_VERSION, compatible Tcl for VIC not found
Use --with-tcl= option to indicate location of tclConfig.sh file for Tcl.])
fi

SC_PATH_TKCONFIG
SC_LOAD_TKCONFIG

AC_MSG_CHECKING([for a compatible version of Tk])
if test "${TCL_VERSION}" == "${TK_VERSION}"; then
    AC_MSG_RESULT([Tk ${TK_VERSION}${TK_PATCH_LEVEL}])

    if test "$aquatk" = yes; then
        echo "Using AquaTk"
        V_DEFINE="$V_DEFINE -DMAC_OSX_TK"
    fi

else
    AC_MSG_ERROR([${TCL_BIN_DIR}/tclConfig.sh is for Tcl ${TCL_VERSION}.
${TK_BIN_DIR}/tkConfig.sh is for Tk ${TK_VERSION}.
Tk ${TK_VERSION} needs Tcl ${TK_VERSION}
Use --with-tcl= option to indicate location of tclConfig.sh file for Tcl.
Use --with-tk= option to indicate location of tkConfig.sh file for Tk.])
fi

if test -f "${TCL_BIN_DIR}/Makefile" ; then
    # dealing with Tcl source tree
    V_INCLUDE_TCL="-I${TCL_SRC_DIR}/generic"
    V_LIBRARY_TCL="${TCL_SRC_DIR}/library"
    V_LIB_TCL="-L${TCL_SRC_DIR}/unix ${TCL_LIB_FLAG}"

else
    # dealing with Tcl system install

    if test -d "${TCL_BIN_DIR}/Headers" ; then
        # Darwin / MacOS X
        V_INCLUDE_TCL=-I${TCL_BIN_DIR}/Headers
        V_LIBRARY_TCL=${TCL_BIN_DIR}/Resources/Scripts

    else
        V_INCLUDE_TCL=$TCL_INCLUDE_SPEC

        V_LIBRARY_TCL=FAIL
        for dir in $TCL_PACKAGE_PATH; do
            if test -r "${dir}/init.tcl" ; then
                V_LIBRARY_TCL=$dir
                break
            elif test -r "${dir}/tcl${TCL_VERSION}/init.tcl" ; then
                V_LIBRARY_TCL="${dir}/tcl${TCL_VERSION}"
                break
            fi
        done
        if test $V_LIBRARY_TCL = FAIL ; then
            echo "can't find init.tcl in ${TCL_PACKAGE_PATH}"
            exit 1
        fi
    fi
    V_LIB_TCL=$TCL_LIB_SPEC
fi

AC_SUBST(V_INCLUDE_TCL)
AC_SUBST(V_LIBRARY_TCL)
AC_SUBST(V_LIB_TCL)


if test -f "${TK_BIN_DIR}/Makefile" ; then
    # dealing with Tk source tree
    V_INCLUDE_TK="-I${TK_SRC_DIR}/generic"
    V_LIBRARY_TK="${TK_SRC_DIR}/library"
    V_LIB_TK="-L${TK_SRC_DIR}/unix ${TK_LIB_FLAG}"

else
    # dealing with Tk system install

    if test -d "${TK_BIN_DIR}/Headers" ; then
        # Darwin / MacOS X

        if test ! -f "${TK_BIN_DIR}/PrivateHeaders/tkInt.h" ; then
            AC_MSG_ERROR([${TK_BIN_DIR}/PrivateHeaders/tkInt.h not found.
Use --with-tk= option to indicate location of tkConfig.sh file for a Tk
which comes with the PrivateHeaders folder.])
        fi

        V_INCLUDE_TK="-I${TK_BIN_DIR}/Headers -I${TK_BIN_DIR}/PrivateHeaders"
        V_LIBRARY_TK=${TK_BIN_DIR}/Resources/Scripts

    else
        V_INCLUDE_TK=$TK_INCLUDE_SPEC

        V_LIBRARY_TK=FAIL
        TK_PACKAGE_PATH=`echo $TCL_PACKAGE_PATH | sed 's/tcl/tk/g'`
        TK_PACKAGE_PATH="$TK_PACKAGE_PATH $TCL_PACKAGE_PATH"
        for dir in $TK_PACKAGE_PATH; do
            if test -r "${dir}/tk.tcl" ; then
                V_LIBRARY_TK=$dir
                break
            elif test -r "${dir}/tk${TK_VERSION}/tk.tcl" ; then
                V_LIBRARY_TK="${dir}/tk${TK_VERSION}"
                break
            fi
        done
    fi

    if test $V_LIBRARY_TK = FAIL ; then
        echo "can't find tk.tcl in ${TK_PACKAGE_PATH}"
        exit 1
    fi
    V_LIB_TK=$TK_LIB_SPEC
fi

V_INCLUDE_X11=$TK_XINCLUDES
V_LIB_X11=$TK_XLIBSW

# if using X11 shared memory add -lXext if not already supplied
if test $V_SHM = "-DUSE_SHM" -a "$aquatcl" != yes -a "$mingw" != "yes"; then
    echo $TK_XLIBSW | grep "\-lXext" > /dev/null 2>&1
    if test $? -ne 0 ; then
        V_LIB_X11="$V_LIB_X11 -lXext"
    fi
fi

AC_SUBST(V_INCLUDE_TK)
AC_SUBST(V_LIBRARY_TK)
AC_SUBST(V_LIB_TK)

AC_SUBST(V_INCLUDE_X11)
AC_SUBST(V_LIB_X11)
