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
dnl Process this file with autoconf to produce a configure script.
AC_INIT()
AC_PREREQ(2.52)
AC_CONFIG_AUX_DIR([.])
AC_CONFIG_HEADER([trace_impl.h])
 
AC_ARG_ENABLE(strict,
[--enable-strict                    - Turn on strict compilation testing
                                     when using gcc],
COPTIONS="${COPTIONS} -Wall -O -Wstrict-prototypes -Wmissing-prototypes -DGCC_WALL")

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})
 
# Find the home directory if not specified
if test "X$srcdir" != "X." -a -s $srcdir/src/Makefile.in ; then
    top_srcdir_abs=$srcdir
else
    PAC_GETWD(top_srcdir_abs,src/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)

# Determine this package's name based on its directory name
changequote(,)
tmpname="`echo $top_srcdir_abs | sed -e 's%\(.*\)/\([^/]*\)%\2%'`"
pkgname="`echo $tmpname | sed -e 'y%abcdefghijklmnopqrstuvwxyz/%ABCDEFGHIJKLMNOPQRSTUVWXYZ_%'`"
changequote([,])
if test "x$pkgname" = "x" ; then
    pkgname="TRACE-IMPL"
fi

echo "Configuring $pkgname implementation with $ac_configure_args"

# 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(jvmflags,
[--with-jvmflags=JVMFLAGS           - supply java virtual machine flags,
                                     e.g. -Xms32m -Xmx256m.],
JVMFLAGS="$withval")

MAKE=${MAKE:-"make"}
PAC_MAKE_IS_GNUMAKE
PAC_MAKE_IS_BSD44
PAC_MAKE_IS_OSF
PAC_MAKE_VPATH
if test ! -s src/rlog.h -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

dnl Check if TRACE_CC has been set before AC_PROG_CC
if test -n "$TRACE_CC" ; then
    TRACE_ORIG_CC="$CC"
    CC="$TRACE_CC"
fi
dnl Checks for programs.
AC_PROG_CC
dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS( stdlib.h stdio.h string.h ctype.h errno.h )
dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
dnl SIZEOF_VOIDP, SIZEOF_LONG, SIZEOF_LONG_LONG and SIZEOF__INT64 created
dnl by AC_CHECK_SIZEOF( *, * ) are needed to compile
dnl slog2sdk/src/logformat/trace/logformat_trace_InputLog.c
AC_CHECK_SIZEOF( void*, $CROSS_SIZEOF_VOIDP )
AC_CHECK_SIZEOF( int, $CROSS_SIZEOF_INT )
AC_CHECK_SIZEOF( long, $CROSS_SIZEOF_LONG )
AC_CHECK_SIZEOF( long long, $CROSS_SIZEOF_LONG_LONG )
AC_CHECK_SIZEOF( __int64, $CROSS_SIZEOF___INT64 )
dnl   -- Check if byteswapping needs to be done.
if test "$cross_compiling" = "yes" -o "$cross_compiling" = 1 ; then
    AC_MSG_CHECKING([for byte ordering])
    if test "X$CROSS_BIGENDIAN" != "X" ; then
        if test "$CROSS_BIGENDIAN" = "true" \
             -o "$CROSS_BIGENDIAN" = "false" ; then
            if test "$CROSS_BIGENDIAN" = "true" ; then
                AC_DEFINE( WORDS_BIGENDIAN, 1,
                           [Define if byte order is bigendian] )
                AC_MSG_RESULT( [defined to be big endian] )
            else
                AC_MSG_RESULT( [defined to be little endian] )
            fi
        else
            AC_DEFINE( WORDS_BIGENDIAN, 1,
                       [Define if byte order is bigendian] )
            AC_MSG_RESULT( [Unknown CROSS_BIGENDIAN, $CROSS_BIGENDIAN! Assumed big endian] )
        fi
    else
        AC_DEFINE( WORDS_BIGENDIAN, 1, [Define if byte order is bigendian] )
        AC_MSG_RESULT( [Undefined CROSS_BIGENDIAN! Assumed big endian] )
    fi
else
    AC_C_BIGENDIAN
fi

LIBSO_PREFIX="lib"
AC_CANONICAL_HOST
case "$host_os" in
    aix4* | aix5* )
        dnl  temporary fix to force libtool on AIX to generate .so file,
        dnl  i.e. inform libtool to do runtime linking.
        LDFLAGS="$LDFLAGS -brtl"
        ;;
    *darwin* )
        dnl  force libtool to build .jnilib which is needed by Mac's JVM
        LDFLAGS="$LDFLAGS -shrext .jnilib"
        ;;
    *cygwin* )
        dnl  Since SUN's JDK assumes a win32 compiler, so force cygwin/gcc
        dnl  to be in MINGW32 mode. 
        CC="gcc -mno-cygwin"
        LDFLAGS="$LDFLAGS -Wl,--enable-stdcall-fixup"
        LIBSO_PREFIX=""
        ;;
esac
AC_SUBST(LIBSO_PREFIX)

dnl  set COPTS equal to CFLAGS during configuration
if test "x$CFLAGS" != "x" ; then
    COPTS="$CFLAGS"
    AC_SUBST(COPTS)
fi

dnl  intel compiler does not need shared library symbol exported.
EXPORT_SYMBOLS=yes
case "$CC" in
    *icc* )
        EXPORT_SYMBOLS=no
        ;;
    *pgcc )
        ;;
esac
AC_SUBST(EXPORT_SYMBOLS)

AC_DISABLE_STATIC
AC_LIBTOOL_WIN32_DLL
dnl  Set SED that is not set in AC_PROG_LIBTOOL in autoconf 2.5x
AC_CHECK_PROG(SED, sed, sed, ;)
AC_PROG_LIBTOOL
AC_SUBST(LIBTOOL_DEPS)

dnl Checks for libraries.
dnl Replace `main' with a function in -lefence:
AC_CHECK_LIB(efence, malloc, [DEBUG_LIBS="-lefence"; AC_SUBST(DEBUG_LIBS)])

dnl Checks for library functions.
AC_CHECK_FUNCS(strstr)

JAC_JNI_HEADERS(JNI_INC, JDK_TOPDIR)
if test "x$JNI_INC" = "x" ; then
    PAC_MSG_ERROR($enable_softerror, [Invalid JNI include dir, exit!])
fi
AC_DEFINE(HAVE_JNI_H,1,[Define if <jni.h> is found])
AC_SUBST(JNI_INC)

JVM="$JDK_TOPDIR/bin/java"

AC_SUBST(JVM)
AC_SUBST(JVMFLAGS)

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

dnl Checking for working INSTALL
AC_PROG_INSTALL
PAC_PROG_CHECK_INSTALL_WORKS
PAC_PROG_INSTALL_BREAKS_LIBS

CPRP=cp
RM="rm -f"
MV="mv -f"
MKDIR=mkdir

AC_SUBST(RM)
#
# libbuild_dir is used to build the libraries in before they are installed.
# binbuild_dir is for the scripts/programs
# includebuild_dir is for all user header files
#
rootbuild_dir=`pwd`
for dir in lib bin share sbin ; do
    dirname=${dir}build_dir
    eval dirvalue=\$"$dirname"
    eval $dirname=$rootbuild_dir/$dir
done
# echo "rootbuild_dir = $rootbuild_dir"
changequote(,)
gui_libbuild_dir="`echo $rootbuild_dir | sed -e 's%\(.*\)/[^/]*$%\1/lib%'`"
changequote([,])
# echo "gui_libbuild_dir = $gui_libbuild_dir"

# VPATH build: Set the libbuild_dir
if test ! -d $libbuild_dir ; then
    echo "creating directory $libbuild_dir"
    $MKDIR $libbuild_dir
fi

# VPATH build: Set the sharebuild_dir
if test ! -d $sharebuild_dir ; then
    echo "creating $sharebuild_dir"
    $MKDIR $sharebuild_dir
fi

# Copying $srcdir/logfiles to sharebuild_dir
if test -d $srcdir/logfiles ; then
    echo "creating sample logfiles from source"
    $CPRP -pr $srcdir/logfiles $sharebuild_dir
fi

AC_SUBST(libbuild_dir)
AC_SUBST(binbuild_dir)
AC_SUBST(sbinbuild_dir)
AC_SUBST(sharebuild_dir)
AC_SUBST(gui_libbuild_dir)

# Define bindir's executables, and pass them down to sbindir's install-package
OLD_EXEFILES="traceprint traceTOslog2"

PATH_OLD_EXEFILES="sbin/install-package"
for exefile in $OLD_EXEFILES ; do
    PATH_OLD_EXEFILES="$PATH_OLD_EXEFILES bin/$exefile"
done

TRACE_NAME=rlog
AC_SUBST(TRACE_NAME)

if test "X$TRACE_NAME" != "X" -a "$TRACE_NAME" != "trace" ; then
    for oldpgm in $OLD_EXEFILES ; do
        newpgm=$TRACE_NAME`echo $oldpgm | sed -e 's%trace%%'`
        EXEFILES="$EXEFILES $newpgm"
    done
fi
AC_SUBST(EXEFILES)

AC_OUTPUT(Makefile src/Makefile $PATH_OLD_EXEFILES)

for script in $PATH_OLD_EXEFILES ; do
    if test -f "$script" ; then
        chmod a+x $script
    else
        PAC_MSG_ERROR([*** $script is not a valid file - check if $srcdir is a valid TRACE-API implementation.])
    fi
done

dnl Rename all the scripts in bindir according to the TRACE_NAME
if test "X$TRACE_NAME" != "X" -a "$TRACE_NAME" != "trace" ; then
    for oldpgm in $OLD_EXEFILES ; do
        newpgm=$TRACE_NAME`echo $oldpgm | sed -e 's%trace%%'`
        $MV bin/$oldpgm bin/$newpgm
    done
fi

dnl Restore the original CC if it is needed
if test -n "$TRACE_CC" ; then
    CC="$TRACE_ORIG_CC"
fi
