#! /bin/sh
dnl
dnl Caching is usually WRONG for systems with cross-mounted file systems
dnl (the cache file may correspond to a different system).  Since configure
dnl is not on a performance-critical path, go for robustness over speed.
dnl
define([AC_CACHE_LOAD], )dnl
define([AC_CACHE_SAVE], )dnl
dnl
dnl
AC_INIT()
dnl Strictly speaking slog2sdk/src does not need 2.52, but since the parent
dnl package, slog2sdk or mpe2, require 2.5x, which allows environment
dnl variables set in configure command not allowed in 2.13.  Because of
dnl that reason, it is safer to use 2.5x here in this package so mpe2's
dnl or slog2sdk configure can complete successfully.
AC_PREREQ(2.52)
AC_CONFIG_AUX_DIR([..])
 
AC_ARG_ENABLE(echo, 
[--enable-echo                      - Turn on strong echoing.  The default 
                                     is enable=no.] ,set -x)
 
AC_ARG_ENABLE(softerror, 
[--enable-softerror                 - Turn on soft error, i.e. instead of 
                                     issuing MSG_ERROR, MSG_WARN + exit 0 
                                     are used.  The default is enable=no.],
, enable_softerror=${slog2_enable_softerror:-no})

AC_ARG_ENABLE(g, 
[--enable-g                         - Turn on the debugging flags, -g, and 
                                     uses jdb to run the GUI.  The default 
                                     is enable=no], ,enable_g=no)

AC_ARG_ENABLE(checkversion, 
[--enable-checkversion              - Turn on the Java version checking code. 
                                     The default is enable=yes],
, enable_checkversion=yes)
 
echo "Configuring the Build of SLOG-2/Jumpshot-4 with $ac_configure_args"
#
# Find the home directory if not specified
if test "X$srcdir" != "X." -a -s $srcdir/base/drawable/Makefile.in ; then
    top_srcdir_abs=$srcdir
else
    PAC_GETWD(top_srcdir_abs, base/drawable/Makefile.in)
fi
 
if test -z "$top_srcdir_abs" ; then
    top_srcdir_abs=`pwd`
fi

if test ! -d "$top_srcdir_abs" ; then
    PAC_MSG_ERROR($enable_softerror, [*** $top_srcdir_abs is not a valid directory - cannot continue])
fi

AC_SUBST(top_srcdir_abs)
 
# Cannot use JAVA_HOME here in this configure.in.
# Because JAVA_HOME is used in Blackdown's .java_wrapper in jdk117_v3
# Using JAVA_HOME here would mistakenly set some variable in .java_wrapper
# when .java_wrapper is invoked.  i.e. causes mysterious error in tests.
AC_ARG_WITH(java, 
[--with-java=JDK_TOPDIR             - The path of the top-level directory of 
                                     the Java SDK installation.  If this option 
                                     or with-java2 is not given, configure
                                     will try to locate java for you.  Also, 
                                     with-java2 option overwrites the 
                                     with-java option to set JDK_TOPDIR.],
JDK_TOPDIR=$withval, JDK_TOPDIR="")
 
AC_ARG_WITH(java2, 
[--with-java2=JDK_TOPDIR            - The path of the top-level directory of 
                                     the Java SDK installation.  If this option 
                                     or with-java is not given, configure will
                                     try to locate java for you.  Also, 
                                     with-java2 option overwrites the 
                                     with-java option to set JDK_TOPDIR.],
JDK_TOPDIR=$withval, )

AC_ARG_WITH(jflags, 
[--with-jflags=JFLAGS               - supply java compiler flags, e.g. 
                                     optimization flags, -O, to the java 
                                     compiler], JFLAGS="$withval")

AC_ARG_ENABLE(clog,
[--enable-clog                      - Build CLOG related utility jar files,
                                     clog2TOslog2.jar, clog2print.jar....
                                     Java Standard Development Kit, SDK,
                                     is needed.  The default is enable=yes.],
,enable_clog=yes)

AC_ARG_WITH(oldsource,
[--with-oldsource=OLD_SRCDIR        - Build slog2updater from the given OLD
                                     Java source directory OLD_SRCDIR which
                                     should be a valid directory name in the
                                     parent directory of this configure.
                                     e.g. src_205, --with-oldsource=src_205.
                                     The default is old_src.],
old_slog2srcdir="$withval", old_slog2srcdir="old_src")
AC_SUBST(old_slog2srcdir)

# Append "-g" to JFLAGS if enable_g=yes
if test "$enable_g" = "yes" ; then
    if test -z "`echo $JFLAGS | grep "\-g"`" ; then
        JFLAGS="-g $JFLAGS"
    fi
fi

# Fixup for make
MAKE=${MAKE:-make}
PAC_MAKE_IS_GNUMAKE
PAC_MAKE_IS_BSD44
PAC_MAKE_IS_OSF
PAC_MAKE_VPATH
# If we are relying on VPATH and no VPATH is set, then we must exit
if test ! -s base/drawable/TimeBoundingBox.java -a -z "$VPATH" ; then
    AC_MSG_ERROR([No virtual MAKE path command found.
	You may need to set your make command.
	The GNU make (sometimes available as gnumake) can be used.])
fi
AC_SUBST(MAKE)
 
# Check if --without-java is specified
if test "$JDK_TOPDIR" = "no" ; then
    PAC_MSG_ERROR($enable_softerror, [--without-java is specified, exit!])
fi

is_javac_working=no
if test "x$JDK_TOPDIR" != "x" ; then
dnl
    JC="$JDK_TOPDIR/bin/javac"
    AC_MSG_CHECKING([if $JC exists])
    if test -x "$JC" ; then
        AC_MSG_RESULT(yes)
        jac_javac_working=yes
    else
        AC_MSG_RESULT(no)
        jac_javac_working=no
    fi
dnl
    if test "$jac_javac_working" = "yes" ; then
        AC_MSG_CHECKING([if $JC compiles])
        JAC_TRY_COMPILE( [$JC], , , [
            public static void main( String args[] )
            {
                System.out.println( "Hello world" );
            }
        ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
        else
            AC_MSG_RESULT(no)
        fi
    fi
dnl
    if test "$jac_javac_working" = "yes" ; then
        AC_MSG_CHECKING([if $JC accepts -classpath, -d & -sourcepath])
        JAC_TRY_COMPILE( [$JC], [-classpath . -d . -sourcepath .], , [
            public static void main( String args[] )
            {
                System.out.println( "Hello world" );
            }
        ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
        else
            AC_MSG_RESULT(no)
        fi
    fi
dnl
    if test "$jac_javac_working" = "yes" ; then
        AC_MSG_CHECKING([if $JC understands List & Map])
        JAC_TRY_COMPILE( [$JC], [-classpath . -d . -sourcepath .], [
            import java.util.List;
            import java.util.Map;
        ], [
            public static void main( String args[] )
            {
                List list;
                Map  map;
            }
        ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
        else
            AC_MSG_RESULT(no)
        fi
    fi
dnl
    if test "$jac_javac_working" = "yes" ; then
        AC_MSG_CHECKING([if $JC handles static-inner-class reference])
        JAC_TRY_COMPILE( [$JC], [-classpath . -d . -sourcepath .], , [
            public class RecType
            {
                static final int STATEDEF = 6;
            }
            public static void main( String args[] )
            {
                int itmp = conftest.RecType.STATEDEF;
                System.out.println( itmp );
            }
        ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
        else
            AC_MSG_RESULT(no)
        fi
    fi
dnl
    if test "$jac_javac_working" = "yes" ; then
        AC_MSG_CHECKING([if $JC compiles Swing-1.1.1 code])
        JAC_TRY_COMPILE( [$JC], ,[
            import javax.swing.*;
        ], [
            public static void main( String args[] )
            {
               JFrame frame = new JFrame();
               System.out.println( "Checking Swing" );
            }
        ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT([yes, assume Java 2 SDK!])
        else
            AC_MSG_RESULT([no, assume Java 1 SDK!])
        fi
    fi
dnl
    if test "$jac_javac_working" = "yes" ; then
        is_javac_working=yes
    else
        is_javac_working=no
    fi
dnl
fi               dnl endof { if test "x$JDK_TOPDIR" = "x" }

if test "$is_javac_working" = "no" ; then
#   Locate Java from user's PATH and known locations
    JAC_PATH_PROG(JC, javac, [
        JAC_TRY_COMPILE( [$JC], , , [
            public static void main( String args[] )
            {
                System.out.println( "Hello world" );
            }
        ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
            AC_MSG_CHECKING([if $JC accepts -classpath, -d & -sourcepath])
            JAC_TRY_COMPILE( [$JC], [-classpath . -d . -sourcepath .], , [
                public static void main( String args[] )
                {
                    System.out.println( "Hello world" );
                }
            ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        fi
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
            AC_MSG_CHECKING([if $JC understands List & Map])
            JAC_TRY_COMPILE( [$JC], [-classpath . -d . -sourcepath .], [
                import java.util.List;
                import java.util.Map;
            ], [
                public static void main( String args[] )
                {
                    List list;
                    Map  map;
                }
            ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        fi
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
            AC_MSG_CHECKING([if $JC handles static-inner-class reference])
            JAC_TRY_COMPILE( [$JC], [-classpath . -d . -sourcepath .], , [
                public class RecType
                {
                    static final int STATEDEF = 6;
                }
                public static void main( String args[] )
                {
                    int itmp = conftest.RecType.STATEDEF;
                    System.out.println( itmp );
                }
            ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        fi
        if test "$jac_javac_working" = "yes" ; then
            AC_MSG_RESULT(yes)
            AC_MSG_CHECKING([if $JC compiles Swing-1.1.1 code])
            JAC_TRY_COMPILE( [$JC], ,[
                import javax.swing.*;
            ], [
                public static void main( String args[] )
                {
                    JFrame frame = new JFrame();
                    System.out.println( "Checking Swing" );
                }
            ], [ jac_javac_working=yes ], [ jac_javac_working=no ] )
        fi
        if test "$jac_javac_working" = "yes" ; then
            jac_prog_working=yes
        else
            jac_prog_working=no
        fi
    ])
#   if JC still hasn't been found, we have exhausted all options! Abort.
    if test "x$JC" = "x" ; then
        AC_MSG_WARN([Cannot find a working javac in \$PATH or known locations!])
        PAC_MSG_ERROR($enable_softerror,
        [Put a working javac in your path or set the --with-java option])
    fi
#   Set JDK_TOPDIR from JC found by JAC_PATH_PROG
    changequote(,)dnl
    JDK_TOPDIR="`echo $JC | sed -e 's%\(.*\)/[^/]*/[^/]*$%\1%'`"
    changequote([,])dnl
    is_javac_working=yes
dnl
fi               dnl end of { if test "x$JDK_TOPDIR" = "x" }

# Print the version of Java used 
# Warn the version of JDK that causes problem
if test "$enable_checkversion" = "yes" ; then
    AC_MSG_CHECKING(for Java version)
    VERSION=`$JDK_TOPDIR/bin/java -fullversion 2>&1`
    case "$VERSION" in
	*1.0.*)
            AC_MSG_RESULT([$VERSION, i.e. the oldest Java])
            AC_MSG_WARN([Java $VERSION is too old to be used with SLOG-2/Jumpshot-2 !])
            ;;
	*1.1.*)
            AC_MSG_RESULT([$VERSION, i.e. Java 1 SDK])
            AC_MSG_WARN([Java $VERSION is too old to be used with SLOG-2/Jumpshot-2 !])
            ;;
	*)
            AC_MSG_RESULT([$VERSION, i.e. Java 2 SDK])
            ;;
    esac
fi

if test "$is_javac_working" = "no" ; then
    PAC_MSG_ERROR($enable_softerror,
        [SLOG-2/Jumpshot-4 can be compiled with Java 2 SDK,
	i.e. j2sdk 1.2.2 or newer.])
fi

# Set the JAVA related tools, like javah and jar.
JH="$JDK_TOPDIR/bin/javah"
RMIC="$JDK_TOPDIR/bin/rmic"
JAR="$JDK_TOPDIR/bin/jar"
for exe in "$JH" "$RMIC" "$JAR" ; do
    AC_MSG_CHECKING([if $exe exists])
    if test -x "$exe" ; then
        AC_MSG_RESULT(yes)
    else
        AC_MSG_RESULT(no)
        PAC_MSG_ERROR($enable_softerror,
                      [$exe does NOT exist, check JDK_TOPDIR = $JDK_TOPDIR])
    fi
done

CPRP=cp
RM="rm -f"
MKDIR=mkdir
AC_SUBST(RM)

AC_SUBST(JFLAGS)
AC_SUBST(JC)
AC_SUBST(JH)
AC_SUBST(RMIC)
AC_SUBST(JAR)

JAC_CHECK_CYGPATH(JPATH_START, JPATH_FINAL)
AC_SUBST(JPATH_START)
AC_SUBST(JPATH_FINAL)

#
# Naming the various build directories, don't inherit them from environment
# Only srcbuild_dir( i.e. this directory ) and libbuild_dir are needed
#
srcbuild_dir=`pwd`
changequote(, )dnl
rootbuild_dir="`echo $srcbuild_dir | sed -e 's%\(.*\)/[^/]*$%\1%'`"
changequote([, ])dnl
# echo "rootbuild_dir = $rootbuild_dir"
libbuild_dir=$rootbuild_dir/lib

# VPATH: Set the srcbuild_dir
if test ! -d $srcbuild_dir/base ; then
    echo "creating directory $srcbuild_dir/base"
    $MKDIR $srcbuild_dir/base
fi
if test ! -d $srcbuild_dir/logformat ; then
    echo "creating directory $srcbuild_dir/logformat"
    $MKDIR $srcbuild_dir/logformat
fi
if test ! -d $srcbuild_dir/viewer ; then
    echo "creating directory $srcbuild_dir/viewer"
    $MKDIR $srcbuild_dir/viewer
fi
if test ! -d $srcbuild_dir/images ; then
    $CPRP -pr $srcdir/images $srcbuild_dir
    $CPRP $srcdir/jumpshot.colors $srcbuild_dir
    $CPRP $srcdir/applet.html $srcbuild_dir
fi
if test ! -d $srcbuild_dir/html ; then
    $CPRP -pr $srcdir/../doc/jumpshot-4/html $srcbuild_dir
fi
AC_SUBST(srcbuild_dir)

# VPATH build: Set the libbuild_dir
if test ! -d $libbuild_dir ; then
    echo "creating $libbuild_dir from source"
    $MKDIR $libbuild_dir
fi
AC_SUBST(libbuild_dir)

# Define the "dirs" components
base_dirs="base/io base/drawable base/statistics base/topology"
logformat_clog_dirs="logformat/clog logformat/clogTOdrawable logformat/clog2 logformat/clog2TOdrawable"
logformat_basic_dirs="logformat/trace logformat/slog2"
logformat_slog2_dirs="logformat/slog2/output logformat/slog2/input logformat/slog2/pipe logformat/slog2/update"
viewer_dirs="viewer/common viewer/convertor viewer/legends viewer/zoomable viewer/histogram viewer/timelines viewer/first viewer/launcher"

# Initial the "dirs" components: slog2_make_dirs, slog2_makefile_dirs.
slog2_make_dirs="$base_dirs"
slog2_makefile_dirs="$base_dirs"
if test "$enable_clog" = "yes" ; then
    slog2_make_dirs="$slog2_make_dirs $logformat_clog_dirs"
    slog2_makefile_dirs="$slog2_makefile_dirs $logformat_clog_dirs"
fi
slog2_make_dirs="$slog2_make_dirs $logformat_basic_dirs"
slog2_makefile_dirs="$slog2_makefile_dirs $logformat_basic_dirs"
# Only slog2_makefile_dirs needs logformat/slog2's subdirectory list
slog2_makefile_dirs="$slog2_makefile_dirs $logformat_slog2_dirs"
slog2_make_dirs="$slog2_make_dirs $viewer_dirs"
slog2_makefile_dirs="$slog2_makefile_dirs $viewer_dirs"

# Output all the directories that needs to do "make"
AC_SUBST(slog2_make_dirs)

# Append each directory slog2_makefile_dirs with "/Makefile"
slog2_makefiles="Makefile"
for dir in $slog2_makefile_dirs ; do
    slog2_makefiles="$slog2_makefiles $dir/Makefile"
done

AC_OUTPUT($slog2_makefiles)
