dnl	This file is an input file used by the GNU "autoconf" program to
dnl	generate the file "configure", which is run to configure the
dnl	Makefile in this directory.
AC_INIT(ChangeLog)

AC_PREREQ(2.14)


#--------------------------------------------------------------------
# 	Version identification info (also set in source files)
#--------------------------------------------------------------------
TCLJAVA_VERSION=1.3.0
TCLJAVA_NODOT_VERSION=`echo $TCLJAVA_VERSION | sed -e 's/\.//g'`

# Convert into full path name
srcdir=`cd $srcdir ; pwd`
AC_MSG_LOG(srcdir is $srcdir)


# Check our version numbers hard coded into files. Update if needed.

TCLJAVA_VERSION_CHECK

# Figure out if we should be building Jacl or Tcl Blend

TCLJAVA_ENABLE_JACL_OR_TCLBLEND

# Process the --with-tcl command line option

TCLJAVA_WITH_TCL

# Check that Tcl install works and is the correct version.

TCLJAVA_CHECK_TCLSH

# See if --with-jdk command line argument is given

AC_JAVA_WITH_JDK

# See if --with-kaffe command line argument is given

AC_JAVA_WITH_KAFFE

# Use the jikes Java compiler if it can be found

AC_JAVA_WITH_JIKES

# Try to detect the installed JVM, this could be controlled
# by the above --with options

AC_JAVA_DETECT_JVM

# Find all the Java tools like java, jar, and javah

AC_JAVA_TOOLS


# Look for debuggers on the PATH, we do not care if thay can not be found

AC_PATH_PROG(DDD, ddd,)
AC_PATH_PROG(GDB, gdb,)



# Look for JNI include and library info when Tcl Blend is being compiled
# We only support native threads with JNI so don't worry about green threads

if test $TCLJAVA = "tclblend" || test $TCLJAVA = "both"; then

    # Find out where JNI include directories live

    AC_JAVA_JNI_INCLUDE

    # Find out where the JNI native thread libraries live

    AC_JAVA_JNI_LIBS

fi




# Do a more in depth test of the Java tools (JAVAC, JAVA, JAVAH, and JAR)

AC_MSG_CHECKING([the Java compiler])

CLASSPATH=$ac_java_classpath
export CLASSPATH
AC_MSG_LOG([Using CLASSPATH=$CLASSPATH], 1)

AC_MSG_LOG([Copied $srcdir/src/Test.tcl to `pwd`], 1)
cp $srcdir/src/Test.tcl .

rm -f Test.class
AC_MSG_LOG([$JAVAC $JAVAC_FLAGS $JAVAC_D_FLAG . $srcdir/src/Test.java], 1)
if ( $JAVAC $JAVAC_FLAGS $JAVAC_D_FLAG . $srcdir/src/Test.java 1>&5 2>&5 ) &&
    test -f Test.class ; then
    AC_MSG_RESULT([the Java compiler works])

    AC_MSG_CHECKING([the -native flag to the Java runtime])

    CLASSPATH=$ac_java_classpath:.
    export CLASSPATH
    AC_MSG_LOG([Using CLASSPATH=$CLASSPATH], 1)

    if test "$ac_java_jvm_name" = "jdk"; then
        AC_MSG_LOG([$JAVA -native Test], 1)
    fi
    if test "$ac_java_jvm_name" = "jdk" &&
	test "`$JAVA -native Test 2>&5`" = "OK"; then
        JAVA_FLAGS=-native
        AC_MSG_LOG([the -native flag works])

        # -native does not work correctly under JDK 1.1.7 from Blackdown.
        # The problem is related to IO from a shell script.
        # Figure out if this is 1.1.8 and not 1.1.7

        if test "`uname -s`" = "Linux" && test "$ac_java_jvm_version" = "1.1"; then
             AC_GREP_FILE([JDK 1.1.8], $ac_java_jvm_dir/README.linux, IS118=1)
             if test "$IS118" != "1" ; then
                 JAVA_FLAGS=""
                 AC_MSG_LOG([skipping -native option because jacl has some problems running with native threads on linux with JDK 1.1.7 from Blackdown])
             fi
        fi
    else
        # If -native is not supported then run without it
        AC_MSG_LOG([$JAVA Test], 1)
	if test "`$JAVA Test 2>&5`" = "OK"; then
            JAVA_FLAGS=""
            AC_MSG_LOG([the -native flag does not work])
        else
            AC_MSG_ERROR([could not run simple test program Test.class with $JAVA, you need to check the Java runtime to make sure it is installed correctly])
        fi
    fi

    if test $TCLJAVA = "tclblend" || test $TCLJAVA = "both"; then
        #test out the javah program on our Test.class file and generate Test.h
        AC_MSG_CHECKING([the Java header generation program])

        CLASSPATH=$ac_java_classpath:.
        export CLASSPATH
        AC_MSG_LOG([Using CLASSPATH=$CLASSPATH], 1)

        JAVAH_FLAGS="-jni -o"
        rm -f Test.h
        AC_MSG_LOG([$JAVAH $JAVAH_FLAGS Test.h Test], 1)
        if ($JAVAH $JAVAH_FLAGS Test.h Test 2>&5) &&
	    test -f Test.h; then

	    # Echo the lines we might be looking for into config.log
            AC_MSG_LOG([Possible matches for method foo], 1)
	    if (grep foo Test.h 1>&5 2>&5) ; then
                AC_MSG_LOG([End of possible matches for method foo], 1)
            else
                AC_MSG_LOG([No lines matched method foo in Test.h])
            fi

	    # Make sure the JNI style func got defined in the file
            AC_GREP_FILE([JNIEXPORT void JNICALL Java_Test_foo], Test.h, JAVAH_WORKED=1, JAVAH_WORKED=0)

            if test "$JAVAH_WORKED" = "1" ; then
                AC_MSG_RESULT([the Java header generation program works])
            else
               AC_MSG_LOG([No JNI signature matches found in Test.h], 1)
               AC_MSG_ERROR([Could not find native function definition in Test.h
generated by $JAVAH])
            fi

        else
            AC_MSG_ERROR([Could not generate header from Test.class with $JAVAH])
        fi
    fi

    AC_MSG_CHECKING([the Java archive program])

    # Flags we will pass to the jar executable
    JAR_EXTRACT_FLAGS=-xf
    JAR_COMPRESS_FLAGS=-cfM
    JAR_NOCOMPRESS_FLAGS=-cf0M

    # Use jar program to create an uncompressed archive

    CLASSPATH=$ac_java_classpath
    export CLASSPATH
    AC_MSG_LOG([Using CLASSPATH=$CLASSPATH], 1)

    rm -f Test.jar
    AC_MSG_LOG([$JAR $JAR_NOCOMPRESS_FLAGS Test.jar Test.class], 1)
    if ($JAR $JAR_NOCOMPRESS_FLAGS Test.jar Test.class 2>&5) && test -f Test.jar; then
        AC_MSG_RESULT([the jar program creates uncompressed archives])
    else
	AC_MSG_LOG([the jar program could not create an uncompressed archive], 1)
	AC_MSG_ERROR([the jar program could not create an uncompressed archive])
    fi

    # Test out extraction of .class file from an uncompressed jar

    rm -f Test.class
    AC_MSG_LOG([$JAR $JAR_EXTRACT_FLAGS Test.jar], 1)
    if ($JAR $JAR_EXTRACT_FLAGS Test.jar 2>&5) && test -f Test.class; then
        AC_MSG_RESULT([the jar program extracts uncompressed archives])
    else
        AC_MSG_LOG([the jar program could not extract from an uncompressed archive], 1)
        AC_MSG_ERROR([the jar program could not extract from an uncompressed archive])
    fi

    # Use jar program to create a compressed archive
    rm -f Test.jar
    AC_MSG_LOG([$JAR $JAR_COMPRESS_FLAGS Test.jar Test.class], 1)
    if ($JAR $JAR_COMPRESS_FLAGS Test.jar Test.class 2>&5) && test -f Test.jar; then
        AC_MSG_RESULT([the jar program creates compressed archives])
    else
        AC_MSG_LOG([the jar program could not create a compressed archive], 1)
        AC_MSG_ERROR([the jar program could not create a compressed archive])
    fi

    # Test out extraction of .class file from a compressed jar

    rm -f Test.class
    AC_MSG_LOG([$JAR $JAR_EXTRACT_FLAGS Test.jar], 1)
    if ($JAR $JAR_EXTRACT_FLAGS Test.jar 2>&5) && test -f Test.class; then
        AC_MSG_RESULT([the jar program extracts compressed archives])
    else
        AC_MSG_LOG([the jar program could not extract from a compressed archive], 1)
        AC_MSG_ERROR([the jar program could not extract from a compressed archive])
    fi

    # Clean up, people might think they need Test.jar
    rm -f Test.jar


    # Run Test.class again so that we are sure the jar
    # programs did not screw it up

    AC_MSG_CHECKING([that the jar program did not corrupt the test file])

    CLASSPATH=$ac_java_classpath:.
    export CLASSPATH

    AC_MSG_LOG([Using CLASSPATH=$CLASSPATH], 1)

    AC_MSG_LOG([$JAVA Test], 1)
    if test "`$JAVA Test 2>&5`" = "OK"; then
        AC_MSG_RESULT([it did not])
    else
        AC_MSG_ERROR([Test.class has been corrupted.])
    fi

    # Test.class is not removed because it is needed later
    # to verify that the installed shell is working.

else
    AC_MSG_LOG([Could not compile simple test program Test.java with $JAVAC], 1)
    AC_MSG_ERROR([Could not compile simple test program Test.java with $JAVAC])
fi





# Subst variables that are only used by Tcl Blend

if test "$ac_java_jvm_name" = "kaffe"; then
    TCL_CFLAGS="$TCL_CFLAGS -DTCLBLEND_KAFFE"
fi

AC_SUBST(CC)
AC_SUBST(SHLIB_CFLAGS)
AC_SUBST(SHLIB_LD)
AC_SUBST(SHLIB_LD_LIBS)
AC_SUBST(SHLIB_PREFIX)
AC_SUBST(SHLIB_SUFFIX)
AC_SUBST(SHLIB_VERSION)
AC_SUBST(TCL_BUILD_LIB_SPEC)
AC_SUBST(TCL_CFLAGS)
AC_SUBST(TCL_LIBS)
AC_SUBST(TCL_VERSION)
AC_SUBST(TCL_SRC_DIR)
AC_SUBST(TCL_BIN_DIR)
AC_SUBST(TCL_LD_SEARCH_FLAGS)
AC_SUBST(TCL_DBGX)

AC_SUBST(TCLJAVA_DBGX)


JAVA_INCLUDE_FLAGS=$ac_java_jvm_jni_include_flags
AC_SUBST(JAVA_INCLUDE_FLAGS)

JAVA_LIB_FLAGS=$ac_java_jvm_jni_lib_flags
AC_SUBST(JAVA_LIB_FLAGS)

JAVA_LIB_RUNTIME_PATH=$ac_java_jvm_jni_lib_runtime_path
AC_SUBST(JAVA_LIB_RUNTIME_PATH)

TCLBLEND_LD_PRELOAD=$ac_java_jvm_ld_preload
TCLBLEND_LD_BIND_NOW=$ac_java_jvm_ld_bind_now

AC_SUBST(TCLBLEND_LD_PRELOAD)
AC_SUBST(TCLBLEND_LD_BIND_NOW)

AC_SUBST(WISH)
AC_SUBST(TCLSH)
AC_SUBST(TCL_INSTALL_LIB_DIR)


# Subst variables that are used in both jacl and tclblend

BUILD_DIR=`pwd`
AC_SUBST(BUILD_DIR)

AC_SUBST(JAVA)
AC_SUBST(JAVA_G)
AC_SUBST(JAVAC)
AC_SUBST(JAVAH)
AC_SUBST(JAR)
AC_SUBST(JDB)
AC_SUBST(DDD)
AC_SUBST(GDB)

JAVA_CLASSPATH=$ac_java_classpath
AC_SUBST(JAVA_CLASSPATH)

# Start Java runtime with 5 Meg Min Heap and 22 Meg Max Heap
JAVA_FLAGS="$JAVA_FLAGS -ms5m -mx22m"
AC_SUBST(JAVA_FLAGS)

AC_SUBST(JAVAC_FLAGS)
AC_SUBST(JAVAC_D_FLAG)
AC_SUBST(JAVA_G_FLAGS)

AC_SUBST(JAR_EXTRACT_FLAGS)
AC_SUBST(JAR_COMPRESS_FLAGS)
AC_SUBST(JAR_NOCOMPRESS_FLAGS)

AC_SUBST(TCLJAVA_VERSION)
AC_SUBST(TCLJAVA_NODOT_VERSION)

# We need to pass "tclblend" as the TCLJAVA name instead of "both"
if test "$TCLJAVA" = "both" ; then
    TCLJAVA=tclblend
fi
AC_SUBST(TCLJAVA)

AC_OUTPUT([Makefile jaclsh jtclsh tcljavaConfig.sh])
