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()
AC_CONFIG_HEADER(mpeconf.h)
#
echo "Configuring MPE Profiling Libraries with $ac_configure_args"
#
# Set top_srcdir_abs to be the ABSOLUTE path to the home directory of MPE
# top_srcdir is in relative path.
if test "X$srcdir" != "X." -a -s $srcdir/include/mpe.h ; then
    top_srcdir_abs=$srcdir
else
    PAC_GETWD( top_srcdir_abs, include/mpe.h )
fi
AC_SUBST(top_srcdir_abs)

AC_ARG_ENABLE( echo, [--enable-echo                      - Turn on strong echoing.  The default is enable=no.] ,set -x ; enable_args="$enable_args --enable-echo" )
#
AC_ARG_ENABLE( strict, [--enable-strict                    - Turn on strict compilation testing when using gcc], COPTIONS="${COPTIONS} -Wall -O -Wstrict-prototypes -Wmissing-prototypes -DGCC_WALL";enable_args="$enable_args --enable-strict" )

VERSION=""
CC=${CC:-cc}
F77=${F77:-f77}
MPI_INC=""
MPI_LIBS=""
MPELIBNAME=""
MPE_CFLAGS=""
MPE_FFLAGS=""
FORTRANNAMES=""
DEBUG_LIBS=""
LOG_LIBS=""
PROF_LIBS=""
TRACE_LIBS=""
ANIM_LIBS=""
DBX_NAME=""
GDB_NAME=""
# CPRP is the version of cp that accepts -r and -p arguments.
# See CRAY below
CPRP="cp"
NO_X=0
MAKE=${MAKE:-make}

# Fixup for make
PAC_MAKE_IS_GNUMAKE
PAC_MAKE_IS_BSD44
PAC_MAKE_IS_OSF
PAC_MAKE_VPATH
AC_SUBST(MAKE)
#
# If we are relying on vpath and no vpath is set, then we must exit
if test ! -s include/mpe.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_ARG_ENABLE( mpe_graphics, [--enable-mpe_graphics              - build X11 part of MPE graphics routines.  If disabled then the MPE routines that make use of X11 graphics will NOT be built; this is appropriate for systems that either do not have the X11 include files or that do not support X11 graphics.  The default value is enable=yes.], ,enable_mpe_graphics=yes )
#
AC_ARG_ENABLE( mpedbg, [--enable-mpedbg                    - Enable the -mpedbg command-line argument (e.g., errors can start an xterm running a debugger).  Only works with some workstation systems when used with MPICH.  The default is enable=yes], enable_mpedbg=yes )
#
if test "$enable_mpedbg" = "yes" ; then
    MPEDBG=1
else
    MPEDBG=0
fi
#
AC_ARG_ENABLE( g, [--enable-g                         - Turn on/off the debugging & diagnostic messages flags in MPE & SLOG-API code. The default is enable=no.], ,enable_g=no )
#
AC_ARG_ENABLE( f77, [--enable-f77                       - Compile routines that require a Fortran compiler.  If configuring with MPICH, the configure in the top-level MPICH directory will choose the appropriate value for you.  The default is enable=yes.], ,enable_f77=yes )
if test "$enable_f77" = "yes" ; then
    NOF77=0
else
    NOF77=1
fi

# Ideally, enable_f2cmpilib should be extracted from the 
# CC/MPI_CC/MPI_INC/MPI_LIBS supplied by the user of MPE. 
# enable_f2cmpilib is used to determine if libmpe_f2cmpi.a( mpe_proff.o ) 
# is needed for profiling of fortran MPI program
# withOUT a PMPICH type of profiling library.
AC_ARG_ENABLE( f2cmpilib, [--enable-f2cmpilib                 - Indicates if this MPE build will make its own Fortran to C MPI wrapper library.  The library is needed for profiling Fortran code in Non-MPICH platform.  The default is enable=yes], ,enable_f2cmpilib=yes )

AC_ARG_ENABLE( mpich, [--enable-mpich                     - Indicates if this MPE will be built as part of the MPICH build.  The default is enable=no], ,enable_mpich=no )
if test "$enable_mpich" = "yes" ; then
    echo "This MPE build is part of the MPICH build"
    MPI_IMPL="MPICH"
    MPICH=1
else
    echo "User supplied MPI implmentation (Good Luck!)"
    MPI_IMPL="UserSupplied"
    MPICH=0
fi

AC_ARG_ENABLE( checkMPI, [--enable-checkMPI                  - Turn on/off the linking test for the MPI include files, MPI libraries and MPI_CC compiler. The default is enable=yes], ,enable_checkMPI=yes )

AC_ARG_ENABLE( checkMPIwtime, [--enable-checkMPIwtime             - Turn on/off the linking test for the MPI_Wtime(). The default is enable=no], ,enable_checkMPIwtime=no )


#
# Set the MPE Libraries name
#
AC_ARG_WITH( mpelibname, [--with-mpelibname=MPE_LIBNAME      - Set the MPE library name instead of the default 'mpe'.  e.g. if MPE_LIBNAME='mpe_new', then the libraries generated will be libmpe_new.a, liblmpe_new.a, libtmpe_new.a and libampe_new.a.], MPE_LIBNAME="$withval" )

#
# Set the Fortran Library Leading Character
#
AC_ARG_WITH( flib_path_leader, [--with-flib_path_leader=FLIB_PATH_LEADER - This option shows how to specify a Fortran library path .], FLIB_PATH_LEADER="$withval" )
if test -z "$FLIB_PATH_LEADER" ; then
    FLIB_PATH_LEADER=-L
fi

#
# Get the user supplied CFLAGS and FFLAGS
#
AC_ARG_WITH( cflags, [--with-cflags=MPE_CFLAGS           - supply extra MPE CFLAGS to the CC compiler], MPE_CFLAGS="$withval" )

AC_ARG_WITH( fflags, [--with-fflags=MPE_FFLAGS           - supply extra MPE FFLAGS to the Fortran compiler], MPE_FFLAGS="$withval" )


#
# Append "-g" to MPE_CFLAGS and MPE_FFLAGS if enable_g=yes
if test $enable_g = "yes"; then

    if test -z "`echo $MPE_CFLAGS | grep "\-g"`"; then
        MPE_CFLAGS="-g $MPE_CFLAGS"
    fi
    if test -z "`echo $MPE_FFLAGS | grep "\-g"`"; then
        MPE_FFLAGS="-g $MPE_FFLAGS"
    fi

    AC_CHECK_LIB( efence, malloc, DEBUG_LIBS="-lefence" )
fi

if test -z "$nonopt" ; then nonopt=NONE ; fi
AC_CANONICAL_HOST
case "$host" in
   *alpha*)
        MPE_CFLAGS="$MPE_CFLAGS -DDBX_IS_OSF"
        host=alpha
        ;;

   *cray*)
        host=cray
        MPE_CFLAGS="$MPE_CFLAGS -DMPI_CRAY"
        ;;

   *rs6000*)
        host=rs6000
        MPE_CFLAGS="$MPE_CFLAGS -DMPI_rs6000"
        ;;
   *solaris*)
        host=solaris
        MPE_CFLAGS="$MPE_CFLAGS -DMPI_solaris"
        ;;
    *linux*)
        host=linux
        MPE_CFLAGS="$MPE_CFLAGS -DMPI_LINUX"
        ;;
    *sun4*)
        host=sun4
        MPE_CFLAGS="$MPE_CFLAGS -DMPI_sun4"
        ;;
    mips-sgi-irix*)
        host=irix
        MPE_CFLAGS="$MPE_CFLAGS -DMPI_IRIX"
        ;;
esac

AC_ARG_ENABLE( broken-procnull,[--enable-broken-procnull           - Handle MPI implementations with buggy handling of MPI_PROC_NULL],procnull_broken=$enableval,procnull_broken=default)

if test "$host" = "irix" -a "$MPI_IMPL" != "MPICH" ; then
    if test "$procnull_broken" = "default" ; then
	procnull_broken=yes
    fi
fi

if test "$procnull_broken" = "yes" ; then
    AC_DEFINE(HAVE_BROKEN_STATUS_ON_PROC_NULL)
fi

if test -z "$SLOG_HOME" ; then
    SLOG_HOME=$top_srcdir_abs/slog_api
fi

# Set the various build directories
#
# includebuild_dir is for all user header files
# libbuild_dir is used to build the libraries in before they are installed.
# binbuild_dir is for the scripts/programs
# sbinbuild_dir is for all system admin stuffs
#
rootbuild_dir=`pwd`
for dir in include lib bin sbin ; do
    dirname=${dir}build_dir
    eval dirvalue=\$"$dirname"
    eval $dirname=$rootbuild_dir/$dir
done

AC_ARG_ENABLE( build_include, [--enable-build_include             - Turn on/off the building of include files.  The default is enable=yes.  Disabling this option will render the Makefiles in contrib/* not usable], , enable_build_include=yes )
if test "$enable_build_include" = "no" ; then
    includebuild_dir=
fi

for dir in include lib bin sbin ; do
    dirname=${dir}build_dir
    eval dirvalue=\$"$dirname"
    if test -n "$dirvalue" ; then
        if test ! -d $dirvalue ; then
            if mkdir -p $dirvalue ; then
                :
            else
		AC_MSG_ERROR([Could not create directory $dirvalue])
            fi
        fi
    fi
done

AC_SUBST(includebuild_dir)
AC_SUBST(libbuild_dir)
AC_SUBST(binbuild_dir)
AC_SUBST(sbinbuild_dir)


#
#   Unknown MPI implmentation, i.e. User defined MPI_LIBS and MPI_INC
#
AC_ARG_WITH( mpicc, [--with-mpicc=MPI_CC                - C compiler used to compile MPI program and to create parallel executable, e.g. mpcc for IBM MPI], MPI_CC="$withval" )
#
AC_ARG_WITH( mpif77, [--with-mpif77=MPI_F77              - F77 compiler used to compile MPI program and to create parallel executable, mpxlf for IBM MPI], MPI_F77="$withval" )
#
AC_ARG_WITH( mpiinc, [--with-mpiinc=MPI_INC              - compiler's include flag with prefix -I used in the MPI implementation, usually it specifies where mpi.h and mpif.h are located, e.g. -I/pkgs/MPI/include], MPI_INC="$withval" )
#
AC_ARG_WITH( mpilibs, [--with-mpilibs=MPI_LIBS            - compiler's full library flags with prefix -L to the library path and prefix -l to the name of the libraries used in the MPI implementation, usually specify where MPI libraries is located. e.g. -L/pkgs/MPI/lib -lpmpich -lmpich], MPI_LIBS="$withval" )
#
AC_ARG_WITH( f2cmpilibs, [--with-f2cmpilibs=F2CMPI_LIBS      - compiler's library flags for Fortran to C MPI wrapper library.  Using this option disables building of MPE's internal Fortran to C MPI library, --disable-f2cmpilib.  The library is needed for profiling MPI Fortran program.  e.g. "-lfmpich" when MPE is building with MPICH. ], F2CMPI_LIBS="$withval" ; enable_f2cmpilib=no )

AC_PROG_CC

if test -d "$SLOG_HOME"; then
  #
  # For VPATH, we need the directory slog_api for the build of SLOG-API
  if test ! -d slog_api ; then mkdir slog_api ; fi
  echo

  echo "Configuring SLOG-API, for support of scalable logfile format" ; \
  if test "$enable_g" = "yes"; then
     ( cd slog_api ; \
       $SLOG_HOME/configure --enable-g --with-cc="$CC" \
                            --with-cflags="$MPE_CFLAGS" \
                            -prefix=$prefix $enable_args )
  else
     ( cd slog_api ; \
       $SLOG_HOME/configure --with-cc="$CC" \
                            --with-cflags="$MPE_CFLAGS" \
                            -prefix=$prefix $enable_args )   
  fi

  # Check if there is any error in SLOG-API's configure
  if test $? != 0 ; then
      AC_MSG_ERROR([Configure of SLOG-API failed])
  else
      AC_MSG_RESULT([Configure of SLOG-API succeeded])
  fi

  echo
fi
 
AC_MSG_CHECKING( whether MPI_CC has been set )
if test -z "$MPI_CC" ; then
    MPI_CC=$CC
fi
AC_MSG_RESULT( $MPI_CC )

if test "$NOF77" = "0" ; then
    AC_PROG_F77
    if test "$F77" = "" ; then
        NOF77=1
    fi
    AC_MSG_CHECKING( whether MPI_F77 has been set )
    if test -z "$MPI_F77" ; then
        MPI_F77=$F77
    fi
    AC_MSG_RESULT( $MPI_F77 )
fi

# Determine the name of the various library being built
if test -z "$MPE_LIBNAME" ; then
    MPE_LIBNAME=mpe
fi

PROF_LIBNAME="$MPE_LIBNAME"
LOG_LIBNAME="l$MPE_LIBNAME"
TRACE_LIBNAME="t$MPE_LIBNAME"
ANIM_LIBNAME="a$MPE_LIBNAME"
F2CMPI_LIBNAME="${MPE_LIBNAME}_f2cmpi"

PROF_LIBS="-l$PROF_LIBNAME"
LOG_LIBS="-l$LOG_LIBNAME $PROF_LIBS"
TRACE_LIBS="-l$TRACE_LIBNAME"
ANIM_LIBS="-l$ANIM_LIBNAME $PROF_LIBS"

if test "$NOF77" = "0" ; then
    if test "$enable_f2cmpilib" = "yes" ; then
        F2CMPI_LIBS="-l$F2CMPI_LIBNAME"
    fi
#   when --enable-f77 and --disable-f2cmpilib are selected,
#   value supplied in --with-f2cmpilibs will be used.
else
    F2CMPI_LIBS=""
fi

#
#  Check if MPI_INC, MPI_LIBS and MPI_CC provide a linkable environment
#  for a simple MPI program
#
if test "$enable_checkMPI" = "yes" ; then
    AC_MSG_CHECKING( [for the linkage of the supplied MPI C definitions] )
    PAC_MPI_LINK_CC_FUNC( $MPI_CC, [$CFLAGS $MPI_INC], $MPI_LIBS, , ,
			  [ mpe_link_ok=yes ], [ mpe_link_ok=no ] )
    if test "$mpe_link_ok" = "yes" ; then
        AC_MSG_RESULT(yes)
    else
        AC_MSG_RESULT(no)
	AC_MSG_ERROR( [Cannot link with basic MPI C program!
	Check your MPI include paths, MPI libraries and MPI CC compiler] )
    fi
fi
if test "$enable_checkMPI" = "yes" -a "$NOF77" = "0" ; then
    AC_MSG_CHECKING( [for the linkage of the supplied MPI F77 definitions] )
    PAC_MPI_LINK_F77_FUNC( $MPI_F77, [$FFLAGS $MPI_INC], $MPI_LIBS, [
	integer mytid
	integer ierr
	], [ call MPI_Comm_rank( MPI_COMM_WORLD, mytid, ierr ) ],
			    [ mpe_link_ok=yes ], [ mpe_link_ok=no ] )
    if test "$mpe_link_ok" = "yes" ; then
        AC_MSG_RESULT(yes)
    else
        AC_MSG_RESULT(no)
        AC_MSG_ERROR( [Cannot link with basic MPI F77 program!
	Check your MPI include paths, MPI libraries and MPI F77 compiler ] )
    fi
fi

#
# Some MPI's seem to have trouble with MPI_Wtime, PMPI_Wtime, and MPI_Attr_get
# (Fujitsu for MPI_Wtime and MPI_Attr_get; IBM's PMPI_Wtime returned 0)
# Check at least that we can link with MPI_Wtime and MPI_Attr_get
#
if test "$enable_checkMPI" = "yes" -a "$enable_checkMPIwtime" = "yes" ; then
    AC_MSG_CHECKING( [for the C version of MPI_Wtime()] )
    PAC_MPI_LINK_CC_FUNC( $MPI_CC, [$CFLAGS $MPI_INC], $MPI_LIBS,
    		 	  [ double d ],
			  [ d=MPI_Wtime() ],
			  [ mpe_link_ok=yes ], [ mpe_link_ok=no ] )
    if test "$mpe_link_ok" = "yes" ; then
        AC_MSG_RESULT(yes)
	AC_DEFINE( HAVE_MPI_WTIME )
    else
        AC_MSG_RESULT(no)
	AC_MSG_ERROR( [Cannot link with MPI_Wtime()!
	Check your MPI include paths, MPI libraries and MPI CC compiler ] )
    fi
fi


#
if test -z "$CC" -o -z "$MPI_CC"; then
    AC_MSG_ERROR( You must configure MPE with a specific MPI )
fi
#
if test -n "$F77" -a "$NOF77" = 0 ; then 
    PAC_PROG_F77_NAME_MANGLE
fi
#

if test "${F2CMPI_LIBS}" = "-l${F2CMPI_LIBNAME}" ; then

#   Determine MPI_Fint
    AC_MSG_CHECKING( [for MPI_Fint] )
    PAC_MPI_LINK_CC_FUNC( $MPI_CC, [$CFLAGS $MPI_INC], $MPI_LIBS,
                          [ MPI_Fint aa=1 ], ,
			  [ mpe_link_ok=yes ], [ mpe_link_ok=no ] )
    if test "$mpe_link_ok" = "yes" ; then
        AC_MSG_RESULT(yes)
        MPI_FINT_TYPE=MPI_Fint
    else
        AC_MSG_RESULT([no, assumed MPI_Fint to be int])
        MPI_FINT_TYPE=int
	AC_DEFINE( MPI_Fint, int )
    fi

#   Determine MPI_STATUS_SIZE
    mpe_tmpfile=mpe_mpistatusvalue
    AC_MSG_CHECKING( [for the value of MPI_STATUS_SIZE] )
dnl    PAC_MPI_RUN_F77_PGM( $MPI_F77, [$FFLAGS $MPI_INC],
dnl                         $F77, $FFLAGS, $LIBS, [
dnl      program get_mpi_status_size
dnl      include 'mpif.h'
dnl      open(10, file="$mpe_tmpfile", status='unknown')
dnl      rewind(10)
dnl      write(10,*) MPI_STATUS_SIZE
dnl      close(10)
dnl      end
dnl                         ], [ mpe_run_ok=yes ], [ mpe_run_ok=no ] )
    PAC_MPI_RUN_CC_PGM( $MPI_CC, [$CFLAGS $MPI_INC],
                        $CC, $CFLAGS, $LIBS, [
#include <stdio.h>
#include "mpi.h"
main()
{
    FILE *mpe_file = fopen( "$mpe_tmpfile", "w" );
    if ( ! mpe_file ) exit( 1 );
    fprintf( mpe_file, "%d\n",
             sizeof( MPI_Status )/sizeof( ${MPI_FINT_TYPE} ) );
    fclose( mpe_file );
    exit( 0 );
}
                        ], [ mpe_run_ok=yes ], [ mpe_run_ok=no ] )
    if test "$mpe_run_ok" = "yes" ; then
	mpe_MPI_STATUS_SIZE="`cat ${mpe_tmpfile} | sed -e 's/ //g'`"
	AC_DEFINE_UNQUOTED( MPI_STATUS_SIZE, $mpe_MPI_STATUS_SIZE )
	AC_MSG_RESULT( [${mpe_MPI_STATUS_SIZE}] )
	rm -f ${mpe_tmpfile}
    else
	AC_MSG_ERROR( [could NOT determine!] )
    fi

#   Determmine if MPI_Status_f2c() and MPI_Status_c2f() are there
    AC_MSG_CHECKING( [for MPI_Status_c2f() and MPI_Status_f2c()] )
    PAC_MPI_LINK_CC_FUNC( $MPI_CC, [$CFLAGS $MPI_INC], $MPI_LIBS, [
			  MPI_Status        c_status;
			  ${MPI_FINT_TYPE}  f_status[ ${mpe_MPI_STATUS_SIZE} ];
			  int ierr;
			  ], [
			  ierr=MPI_Status_c2f( &c_status, f_status ) ;
			  ierr=MPI_Status_f2c( f_status, &c_status ) ;
			  ], [ mpe_link_ok=yes ], [ mpe_link_ok=no ] )
    if test "$mpe_link_ok" = "yes" ; then
        AC_MSG_RESULT(yes)
        AC_DEFINE( HAVE_MPI_STATUS_C2F )
    else
        AC_MSG_RESULT(no)
    fi

#   Determmine if MPI_xxx_f2c() and MPI_xxx_c2f() are there
    AC_MSG_CHECKING( [for other MPI_xxxx_c2f() and MPI_xxxx_f2c()] )
    PAC_MPI_LINK_CC_FUNC( $MPI_CC, [$CFLAGS $MPI_INC], $MPI_LIBS, [
			  MPI_Request request; ${MPI_FINT_TYPE} aa;
			  ], [ 
			  aa=MPI_Request_c2f(request) ;
			  request=MPI_Request_f2c(aa) ;
			  ], [ mpe_link_ok=yes ], [ mpe_link_ok=no ] )
    if test "$mpe_link_ok" = "yes" ; then
        AC_MSG_RESULT(yes)
        AC_DEFINE( HAVE_MPI_XXXX_F2C )
    else
        AC_MSG_RESULT(no)
    fi

#   Determine the Fortran logical values in MPI C program
    mpe_tmpfile=mpe_f77logicals
    AC_MSG_CHECKING( [for values of Fortran logicals in MPI C program] )
    PAC_MPI_RUN_F77_FUNC_FROM_C( $F77, $FFLAGS,
                                 $MPI_CC, [$CFLAGS $MPI_INC],
				 $CC, $CFLAGS, $LIBS, [
      subroutine ftest( itrue, ifalse )
      logical itrue, ifalse
      itrue  = .TRUE.
      ifalse = .FALSE.
      return
      end
                                 ], [
#ifdef F77_NAME_UPPER
#define ftest_ FTEST
#elif defined(F77_NAME_LOWER) || defined(F77_NAME_MIXED)
#define ftest_ ftest
#endif
#include <stdio.h>
#include "mpi.h"
int main()
{
    ${MPI_FINT_TYPE} itrue, ifalse;
    FILE *mpe_file = fopen( "$mpe_tmpfile", "w" );
    if ( ! mpe_file ) exit( 1 );
    ftest_( &itrue, &ifalse );
    fprintf( mpe_file, "%d %d\n", itrue, ifalse );
    fclose( mpe_file );
    exit( 0 );
}
                                 ], [ mpe_link_ok=yes ], [ mpe_link_ok=no ] )
    if test "$mpe_run_ok" = "yes" ; then
	mpe_f77logicals="`cat ${mpe_tmpfile}`"
	true_val="`echo ${mpe_f77logicals} | sed -e 's/ .*//g'`"
	false_val="`echo ${mpe_f77logicals} | sed -e 's/.*  *//g'`"
	if test -n "$true_val" -a -n "$false_val" ; then
	    AC_DEFINE_UNQUOTED( MPE_F77_TRUE_VALUE, $true_val )
	    AC_DEFINE_UNQUOTED( MPE_F77_FALSE_VALUE, $false_val )
	    AC_MSG_RESULT( [True=$true_val and False=$false_val] )
        else
	    AC_MSG_ERROR( [could NOT determine!] )
	fi
	rm -f ${mpe_tmpfile}
    else
	AC_MSG_ERROR( [could NOT determine!] )
    fi
fi

# MPE_HEADERS are header files to be installed into $includedir. 
# It needs to be defined here so we have only ONE source of headers defined,
# otherwise, top_srcdir/Makefile.in and top_srcdir/sbin/mpeinstall will need
# to define MPE_HEADERS separately.
MPE_HEADERS="mpe.h mpe_graphics.h mpe_log.h mpetools.h mpeexten.h basex11.h protofix.h"
AC_SUBST(MPE_HEADERS)

MPE_GRPAHICS=""
MPEGRAPHICS_SOURCE=""
MPEGRAPHICS_FSOURCE=""
#
# We need to check that this has worked.  The autoconf macro is broken
AC_PROG_CPP
if test "$CPP" = "/lib/cpp" -a ! -x /lib/cpp ; then
   AC_MSG_ERROR([configure could not find a working C preprocessor])
fi
if test "$enable_mpe_graphics" = "yes" ; then
    AC_PATH_X
else
    no_x="yes"
fi
#
if test -z "$no_x" ; then
    MPE_GRAPHICS="-DMPE_GRAPHICS"
    MPEGRAPHICS_SOURCE="mpe_graphics.c xcolor.c xframe.c xinit.c xwmap.c xmouse.c"
    MPEGRAPHICS_FSOURCE="mpe_graphicsf.c"
    if test -n "$x_includes" ; then
        X_INC=-I$x_includes
    fi
    if test -n "$x_libraries" ; then
	X_LIB="-L$x_libraries -lX11"
        ANIM_LIBS="$ANIM_LIBS -L$x_libraries -lX11"
    else
	X_LIB="-lX11"
        ANIM_LIBS="$ANIM_LIBS -lX11"
    fi
else
    AC_MSG_WARN([*# Could not find appropriate X windows libraries.  MPE routines that make use of X11 graphics will NOT be built unless you put X windows libraries in your path and reconfigure])
    MPE_GRAPHICS="" 
    NO_X=1
fi
#
#
AC_PATH_PROG(DBX_NAME,dbx)
if test -z "$DBX_NAME" ; then
    if test -n "$MPICH_WARNING_SEP" ; then echo '>'$MPICH_WARNING_SEP ; fi
    echo "*# Cannot find dbx in your path.  You can put dbx in your path and "
    echo "*# reconfigure or else you will not be able to run the debugger code"
    echo "*# using the dbx debugger."
    if test -n "$MPICH_WARNING_SEP" ; then echo '<'$MPICH_WARNING_SEP ; fi
fi
#
AC_PATH_PROG(GDB_NAME,gdb)
if test -z "$GDB_NAME" ; then
    if test -n "$MPICH_WARNING_SEP" ; then echo '>'$MPICH_WARNING_SEP ; fi
    echo "*# Cannot find gdb in your path.  You can put gdb in your path and"
    echo "*# reconfigure or else you will not be able to run the debugger code"
    echo "*# using the gdb debugger."
    if test -n "$MPICH_WARNING_SEP" ; then echo '<'$MPICH_WARNING_SEP ; fi
fi
#   
dnl Check if C compiler fully supports the keyword `const' - if not define 
dnl `const' to be empty.     
AC_C_CONST
AC_C_BIGENDIAN
AC_HEADER_STDC
#
AC_MSG_CHECKING(for long pointers)
AC_TRY_RUN(int main()
{
  if (sizeof(void *) > 4)
    return -1;
  return 0;
}, sizeof_void_greater=no, sizeof_void_greater=yes,
sizeof_void_greater=$CROSS_SIZEOF_VOID_GREATER)
if test "$sizeof_void_greater" = "yes" ; then
    AC_DEFINE(POINTER_64_BITS)
    AC_MSG_RESULT([yes])
else
    AC_MSG_RESULT([no])
fi
AC_HAVE_FUNCS(system)
AC_HAVE_FUNCS(sysinfo)
AC_CHECK_FUNC(uname,AC_DEFINE(HAVE_UNAME)haveuname=1)
AC_CHECK_FUNC(gethostbyname,AC_DEFINE(HAVE_GETHOSTBYNAME)havegethostbyname=1)
#
AC_CHECK_HEADERS(unistd.h stdlib.h string.h netdb.h sys/systeminfo.h)
AC_CHECK_HEADER(stdarg.h,[AC_DEFINE(HAVE_STDARG_H) havestdarg=1],,)
#
# We need to build mpe with the shared library flags
# This should rely on the same approach used in the mpich configure,
# since just using TRY_LINK or TRY_COMPILE doesn't identify warning messages
# about "compiler flag unrecognized".
#
# It isn't enough to check for stdarg.  Even gcc doesn't get it right;
# on some systems, the gcc version of stdio.h loads stdarg.h WITH THE WRONG
# OPTIONS (causing it to choose the OLD STYLE va_start etc).
#
if test -n "$havestdarg" ; then
    PAC_COMPILE_CHECK_FUNC(stdarg is correct,[
#include <stdio.h>
#include <stdarg.h>
int func( int a, ... ){
int b;
va_list ap;
va_start( ap, a );
b = va_arg(ap, int);
printf( "%d-%d\n", a, b );
va_end(ap);
fflush(stdout);
return 0;
}
int main() { func( 1, 2 ); return 0;}],
AC_DEFINE(USE_STDARG)
MPE_CFLAGS="$MPE_CFLAGS -DUSE_STDARG")
fi
#
AC_MSG_CHECKING([if compiler accepts prototypes])
AC_TRY_COMPILE(, int f(double a);, have_proto=yes, have_proto=no)
if test "$have_proto" = "yes" ; then
    AC_DEFINE(HAVE_PROTOTYPES)
    MPE_CFLAGS="$MPE_CFLAGS -DHAVE_PROTOTYPES"
    AC_MSG_RESULT([yes])
else
    AC_MSG_RESULT([no])
fi

AC_MSG_CHECKING([for sighandler_t])
AC_TRY_COMPILE([#include <signal.h>],[sighandler_t *f;],
has_sighandler_t=yes,has_sighandler_t=no)
AC_MSG_RESULT($has_sighandler_t)
if test "$has_sighandler_t" = "yes" ; then
    AC_DEFINE(HAVE_SIGHANDLER_T)
fi
#
#
MPE_CFLAGS="$MPE_CFLAGS $COPTIONS"
#
AC_PROG_RANLIB
AC_CHECK_PROG(AR, ar, ar, ;)
#
AC_SUBST(AR)
AC_SUBST(RANLIB)
AC_SUBST(MPICH)
AC_SUBST(CC)
AC_SUBST(MPI_CC)
AC_SUBST(F77)
AC_SUBST(MPI_F77)
AC_SUBST(LDFLAGS)
AC_SUBST(NO_X)
AC_SUBST(X_INC)
AC_SUBST(X_LIB)
AC_SUBST(MPI_INC)
AC_SUBST(MPI_LIBS)
AC_SUBST(DEBUG_LIBS)

AC_SUBST(PROF_LIBNAME)
AC_SUBST(LOG_LIBNAME)
AC_SUBST(TRACE_LIBNAME)
AC_SUBST(ANIM_LIBNAME)
AC_SUBST(F2CMPI_LIBNAME)

AC_SUBST(LOG_LIBS)
AC_SUBST(TRACE_LIBS)
AC_SUBST(ANIM_LIBS)
AC_SUBST(PROF_LIBS)
AC_SUBST(F2CMPI_LIBS)

AC_SUBST(MPEDBG)
AC_SUBST(MPE_GRAPHICS)
AC_SUBST(MPEGRAPHICS_SOURCE)
AC_SUBST(MPEGRAPHICS_FSOURCE)
AC_SUBST(MPE_CFLAGS)
AC_SUBST(MPE_FFLAGS)
AC_SUBST(FLIB_PATH_LEADER)
AC_SUBST(CPRP)
AC_SUBST(NOF77)
AC_SUBST(SLOG_HOME)

# Make the directories that do not contain Makefiles (should they?) but 
# are needed for the subsequent directories.
if test ! -d contrib ; then mkdir contrib ; fi
rm -f $srcdir/mpeconf.h

# Determine if the log viewers directory is going to be configured.
AC_ARG_ENABLE( viewers, [--enable-viewers                   - Enable the build of all the available log viewers.  The default is enable=yes.], , enable_viewers=yes )

if test "$enable_viewers" = "yes" -a -s $srcdir/viewers/configure ; then
    viewers_dir="viewers"
    LOGVIEWERS=1
else
    viewers_dir=
    LOGVIEWERS=0
fi
AC_CONFIG_SUBDIRS($viewers_dir)
AC_SUBST(LOGVIEWERS)

AC_OUTPUT_COMMANDS( [
if test ! -x ./sbin/mpeinstall -a -s ./sbin/mpeinstall; then
    chmod u+x ./sbin/mpeinstall
fi
] )
AC_OUTPUT( Makefile sbin/mpeinstall src/Makefile src/Makefile_nompi \
	   src/dbxerr.c contrib/life/Makefile contrib/mandel/Makefile \
	   contrib/mastermind/Makefile contrib/test/Makefile )
