dnl Process this file with autoconf to produce a configure script
dnl
dnl Configure.in --
dnl
dnl Configuration script for building e4Graph
dnl
dnl Copyright (c) 2000-2003, JYL Software Inc.
dnl 
dnl Permission is hereby granted, free of charge, to any person obtaining
dnl a copy of this software and associated documentation files (the
dnl "Software"), to deal in the Software without restriction, including
dnl without limitation the rights to use, copy, modify, merge, publish,
dnl distribute, sublicense, and/or sell copies of the Software, and to
dnl permit persons to whom the Software is furnished to do so, subject to
dnl the following conditions:
dnl 
dnl The above copyright notice and this permission notice shall be
dnl included in all copies or substantial portions of the Software.
dnl 
dnl THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
dnl EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
dnl MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
dnl NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
dnl LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
dnl OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
dnl WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

AC_INIT(../main/include/e4graph.h)
AC_PREREQ(2.13)
AC_CONFIG_AUX_DIR(scripts)
AC_LANG_CPLUSPLUS

# Figure out where we're building from:

BUILD_DIR=`(pwd)`

# Checks for programs.

AC_PROG_CC
AC_PROG_CXX
AC_PROG_INSTALL

# Libtool support

AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL

# Checks for header files.

AC_HEADER_STDC

# Checks for typedefs, structures, and compiler characteristics.

AC_C_CONST
AC_C_INLINE
AC_CHECK_SIZEOF(long,4)

# The following check does not seem to work on autoconf 2.13:
# AC_CHECK_TYPE(long long)

# Checks for library functions:

AC_CHECK_FUNCS(mmap memmove bcopy flock)

# Get a few things defined before we start (this is all in tcl.m4):

SC_CONFIG_CFLAGS

# Define the version of e4Graph we're building:

VERSION=1.0a10

dnl Default values for main targets:

E4GRAPH_TARGETS="main testprog"
E4GRAPH_INSTALL="install_main install_testprog"

# Release engineering:

ac_cv_rel_tmp_dir_parent="/tmp"
ac_cv_rel_filename="e4graph-${VERSION}"
ac_cv_rel_tmp_dir="${ac_cv_rel_tmp_dir_parent}/${ac_cv_rel_filename}"

dnl Check for --with-tmpdir flag.

AC_ARG_WITH(tmpdir,
	[  --with-tmpdir=DIR	  directory to use for temp files],
	with_tmpdir=${withval}
)

AC_MSG_CHECKING([for tmp dir])
AC_CACHE_VAL(ac_cv_rel_tmp_dir_parent,[
    # Check to see if --with-tmpdir was specified. This allows overriding
    # the name of the directory used to build the release.

    if test x"$(with_tmpdir)" != x ; then
	ac_cv_rel_tmp_dir_parent="${with_tmpdir}"
	ac_cv_rel_tmp_dir="${ac_cv_rel_tmp_dir_parent}/${ac_cv_rel_filename}"
    fi
])
AC_MSG_RESULT([$ac_cv_rel_tmp_dir_parent])

dnl Check for --with-release flag.

AC_ARG_WITH(release,
	[  --with-release=FILE	  file name for release],
	with_release=${withval})

AC_MSG_CHECKING([for release specification])
AC_CACHE_VAL(ac_cv_rel_filename,[

    # Check to see if --with-release was specified. This allows overriding
    # the name of the distribution being built.

    if test x"${with_release}" != x ; then
	ac_cv_rel_filename="${with_release}-${VERSION}"
	ac_cv_rel_tmp_dir="${ac_cv_rel_tmp_dir_parent}/${ac_cv_rel_filename}"
    fi
])
AC_MSG_RESULT([$ac_cv_rel_filename])

RELEASE_FILE=${ac_cv_rel_filename}
RELEASE_TMP_DIR=${ac_cv_rel_tmp_dir}
RELEASE_TMP_DIR_PARENT=${ac_cv_rel_tmp_dir_parent}

dnl Check for --with-metakit flag and attempt to find where Metakit is
dnl installed. Metakit is absolutely required, if we don't find it we
dnl bail out.

AC_ARG_WITH(metakit,
	[  --with-metakit=DIR	  directory where Metakit is installed], 
	with_metakit=${withval})

AC_MSG_CHECKING([for Metakit 2.4.*])
AC_CACHE_VAL(ac_cv_c_metakit,[

    # First check to see if --with-metakit was specified. This allows pointing
    # to an installed version of Metakit, in a non-standard place.

    if test x"${with_metakit}" != x ; then
	if test -f "${with_metakit}/include/mk4.h" ; then
	     ac_cv_c_metakit=`(cd ${with_metakit}; pwd)`
	     ac_cv_c_metakit_lib="${ac_cv_c_metakit}/lib"
	     ac_cv_c_metakit_inc="${ac_cv_c_metakit}/include"
	else
	     AC_MSG_ERROR([${with_metakit}/include directory doesn't contain mk4.h])
	fi
    fi

    # Check in ../../metakit-<x>.<y>

    if test x"${ac_cv_c_metakit}" = x ; then
	for i in `ls -dr ../../metakit-2.4.[[8-9]] 2>/dev/null` \
		 `ls -dr ../../metakit-2.4.[[8-9]].* 2>/dev/null` ; do
		if test -f $i/include/mk4.h ; then
		    ac_cv_c_metakit=`(cd $i; pwd)`
		    ac_cv_c_metakit_inc=${ac_cv_c_metakit}/include
		    ac_cv_c_metakit_lib=${ac_cv_c_metakit}/builds/.libs
		    break
		fi
	done
    fi

    # Check in a private location, first in $prefix and then relative to me.

    if test x"${ac_cv_c_metakit}" = x ; then
	for i in `ls -dr ${prefix} 2>/dev/null` ; do
	    if test -f $i/include/mk4.h ; then
		ac_cv_c_metakit=`(cd $i; pwd)`
		ac_cv_c_metakit_inc=${ac_cv_c_metakit}/include
		ac_cv_c_metakit_lib=${ac_cv_c_metakit}/lib
		break
	    fi
	done
    fi

    # check in a few common install locations

    if test x"${ac_cv_c_metakit}" = x ; then
	for i in `ls -dr /usr/local 2>/dev/null` \
		 `ls -dr /usr 2>/dev/null` ; do
	    if test -f "$i/include/mk4.h" ; then
		ac_cv_c_metakit=`(cd $i; pwd)`
		ac_cv_c_metakit_lib=${ac_cv_c_metakit}/lib
		ac_cv_c_metakit_inc=${ac_cv_c_metakit}/include
		break
	    fi
	done
    fi
])

# If its still undefined then complain.

if test x"${ac_cv_c_metakit}" = x ; then
    AC_MSG_WARN(Can't find Metakit, aborting)
    exit 0
else
    METAKIT_LIB=${ac_cv_c_metakit_lib}
    METAKIT_INC=${ac_cv_c_metakit_inc}
    AC_MSG_RESULT(${METAKIT_INC}/mk4.h)
fi

#--------------------------------------------------------------------
# Find and load the tclConfig.sh file
#--------------------------------------------------------------------

SC_PATH_TCLCONFIG
SC_LOAD_TCLCONFIG

if test x"${TCL_PREFIX}" != x ; then
    TCL_LIB=${TCL_LIB_SPEC}
    TCL_INC=${TCL_INCLUDE_SPEC}
    TCL_STB=${TCL_STUB_LIB_SPEC}
    TCL_PKG=${TCL_PACKAGE_PATH}/tcl${TCL_VERSION}
    AC_MSG_RESULT(${TCL_INCLUDE_SPEC})
else
    TCL_LIB="# No Tcl library found"
    TCL_INC="# No tcl.h found"
    TCL_STB="# No libtclstubs.a found"
    TCL_PKG="# No TCL_PACKAGE_PATH found"
    AC_MSG_RESULT(not found)
fi

#----------------------------------------------------------------------
# Find Python
#----------------------------------------------------------------------

dnl Check for -with-python flag and attempt to find Python.

AC_MSG_CHECKING([for Python 2.*])
AC_ARG_WITH(python,
	[  --with-python=DIR	  directory containing Python.h],
	with_python=${withval})

AC_CACHE_VAL(ac_cv_c_python,[

    # First check if --with-python was specified.

    if test x"${with_python}" != x ; then
        if test -f "${with_python}/Python.h" ; then
	    ac_cv_c_python=`(cd ${with_python}; pwd)`
	else
	    AC_MSG_ERROR([${with_python} does not contain Python.h])
	fi
    fi

    # For some reason (at least with Python 2.2) it is impossible to
    # build Python from its build area. We must use its install area
    # instead. Therefore the following code is commented out:
    #
    ## Check in ../../Python-<x>.<y>
    #
    # if test x"${ac_cv_c_python}" = x ; then
    #	for i in `ls -dr ../../Python-2.* 2>/dev/null` \
    #		 `ls -dr ../Python-2.* 2>/dev/null` \
    #		 `ls -dr ../../../Python-2.* 2>/dev/null` ; do
    #	    if test -f $i/Include/Python.h ; then
    #	        ac_cv_c_python=`(cd $i/Include; pwd)`
    #		break
    #	    fi
    #	done
    # fi


    # Check for a private Python installation, first where we're going
    # to install e4graph, and then relative to this dir:

    if test x"${ac_cv_c_python}" = x ; then
	for i in `ls -dr ${prefix} 2>/dev/null` ; do
	    if test -f $i/include/python2.2/Python.h ; then
		ac_cv_c_python=`(cd $i/include/python2.2; pwd)`
	    fi
	done
    fi

    # Check in a few common install locations:

    if test x"${ac_cv_c_python}" = x ; then
	for i in `ls -dr /usr/local/include/python2.* 2>/dev/null` \
		 `ls -dr /usr/include/python2.* 2>/dev/null` ; do
	    if test -f $i/Python.h ; then
		ac_cv_c_python=`(cd $i; pwd)`
		break
	    fi
	done
    fi
])

if test x"${ac_cv_c_python}" != x ; then
    PYTHON_INC=${ac_cv_c_python}
    AC_MSG_RESULT(${PYTHON_INC}/Python.h)
else
    PYTHON_INC="# No Python.h found"
    AC_MSG_RESULT(not found)
fi

#----------------------------------------------------------------------
# Find expat
#----------------------------------------------------------------------

dnl Check for -with-expat flag and attempt to find expat.

AC_MSG_CHECKING([for Expat 1.95.*])
AC_ARG_WITH(expat,
	[  --with-expat=DIR	  directory containing Expat (expat.h)], 
	with_expat=${withval})

AC_CACHE_VAL(ac_cv_c_expat,[

    # First check to see if --with-expat was specified.

    if test x"${with_expat}" != x ; then
	if test -f "${with_expat}/include/expat.h" ; then
	    ac_cv_c_expat=`(cd ${with_expat}; pwd)`
	    ac_cv_c_expat_inc=${ac_cv_c_expat}/include
	    ac_cv_c_expat_lib=${ac_cv_c_expat}/lib
	else
	    AC_MSG_ERROR([${with_expat}/include does not contain expat.h])
	fi
    fi

    # Check in ../../expat-1.95.<x>:

    if test x"${ac_cv_c_expat}" = x ; then
	for i in `ls -dr ../expat-1.95.7 2>/dev/null` \
	         `ls -dr ../expat-1.95.[[4-5]] 2>/dev/null` \
		 `ls -dr ../../expat-1.95.7 2>/dev/null` \
		 `ls -dr ../../expat-1.95.[[4-5]] 2>/dev/null` \
		 `ls -dr ../../../expat-1.95.7 2>/dev/null` \
		 `ls -dr ../../../expat-1.95.[[4-5]] 2>/dev/null` ; do
	    if test -f $i/lib/expat.h ; then
		ac_cv_c_expat=`(cd $i; pwd)`
		ac_cv_c_expat_inc=${ac_cv_c_expat}/lib
		ac_cv_c_expat_lib=${ac_cv_c_expat}/.libs
		break
	    fi
	done
    fi

    # Check for a private Expat installation, first where we're going
    # to install e4graph, and then relative to this dir:

    if test x"${ac_cv_c_expat}" = x ; then
        for i in `ls -dr ${prefix} 2>/dev/null` ; do
	    if test -f $i/include/expat.h ; then
	        ac_cv_c_expat=`(cd $i; pwd)`
		ac_cv_c_expat_inc=${ac_cv_c_expat}/include
		ac_cv_c_expat_lib=${ac_cv_c_expat}/lib
		break
	    fi
	done
    fi

    # check in a few common install locations

    if test x"${ac_cv_c_expat}" = x ; then
	for i in `ls -dr /usr/local 2>/dev/null` \
		 `ls -dr /usr 2>/dev/null`  ; do
	    if test -f "$i/include/expat.h" ; then
		ac_cv_c_expat=`(cd $i; pwd)`
		ac_cv_c_expat_lib=${ac_cv_c_expat}/lib
		ac_cv_c_expat_inc=${ac_cv_c_expat}/include
		break
	    fi
	done
    fi
])

if test x"${ac_cv_c_expat}" != x ; then
    EXPAT_INC=${ac_cv_c_expat_inc}
    EXPAT_LIB=${ac_cv_c_expat_lib}
    AC_MSG_RESULT(${EXPAT_INC}/expat.h)
else
    EXPAT_INCLUDE_DIR="# No expat.h found"
    EXPAT_LIB_DIR="# No libexpat.so found"
    AC_MSG_RESULT(not found)
fi

#----------------------------------------------------------------------
# Find Java
#----------------------------------------------------------------------

dnl Check for -with-java flag and attempt to find the Java installation

AC_MSG_CHECKING([for Java])
AC_ARG_WITH(java,
	[  --with-java=DIR	  directory containing Java (jni.h)],
	with_java=${withval})

AC_CACHE_VAL(ac_cv_c_java,[

	# First check to see if --with-java was specified.

	if test x"${with_java}" != x ; then
	    if test -f "${with_java}/include/jni.h" ; then
	        ac_cv_c_java=`(cd ${with_java}; pwd)`
		ac_cv_c_java_include=${ac_cv_c_java}/include
	    else
	        AC_MSG_ERROR([${with_java}/include does not contain jni.h])
	    fi
	fi

	# Check for installs of Java in /usr/java

	if test x"${ac_cv_c_java}" = x ; then
	    for i in `ls -dr /usr/java/* 2>/dev/null` \
		     `ls -dr /usr/java 2>/dev/null` ; do
	        if test -f "$i/include/jni.h" ; then
		    ac_cv_c_java=`(cd $i; pwd)`
		    ac_cv_c_java_include=${ac_cv_c_java}/include
		    break
		fi
	    done
	fi
])

if test x"${ac_cv_c_java}" != x ; then
    JAVA_DIR=${ac_cv_c_java}
    for i in linux solaris ; do
        if test -d ${ac_cv_c_java}/include/$i ; then
	     JAVA_SYS_DIR=$i
	     break
	fi
    done
    AC_MSG_RESULT(${JAVA_DIR}/include/jni.h)
else
    JAVA_DIR="# No jni.h found"
    JAVA_SYS_DIR="# No system specific Java dir found"
    AC_MSG_RESULT(not found)
fi

#----------------------------------------------------------------------
# Are we building the XML extension?
#----------------------------------------------------------------------

dnl Check if we're building the XML extension:

AC_ARG_ENABLE(xml,
	[  --enable-xml		  build the XML extension (no)],
	[xml_ok=$enableval],
	[xml_ok=no])

if test "$xml_ok" != "no" ; then
    if test x"${ac_cv_c_expat}" != x ; then
	E4GRAPH_TARGETS="$E4GRAPH_TARGETS xml"
	E4GRAPH_INSTALL="$E4GRAPH_INSTALL install_xml"
    else
	AC_MSG_WARN(Can't build e4xml because Expat not found)
    fi
fi

#----------------------------------------------------------------------
# Are we building the Tcl binding?
#----------------------------------------------------------------------

dnl Check if we're building the Tcl binding:

AC_ARG_ENABLE(tcl, 
	[  --enable-tcl		  build the tgraph extension for Tcl (no)],
	[tcl_ok=$enableval],
	[tcl_ok=no])

if test "$tcl_ok" != "no" ; then
    if test x"${TCL_PREFIX}" != x ; then
	E4GRAPH_TARGETS="$E4GRAPH_TARGETS tcl"
	E4GRAPH_INSTALL="$E4GRAPH_INSTALL install_tcl"
	if test "$xml_ok" != "no"; then
	    if test x"${ac_cv_c_expat}" != x ; then
		E4GRAPH_TARGETS="$E4GRAPH_TARGETS t4xml"
		E4GRAPH_INSTALL="$E4GRAPH_INSTALL install_t4xml"
	    else
		AC_MSG_WARN(Can't build t4xml because Expat not found)
	    fi
	fi
    else
	AC_MSG_WARN(Can't build tgraph because Tcl config not found)
    fi
fi

#----------------------------------------------------------------------
# Are we building the Python binding?
#----------------------------------------------------------------------

dnl Check if we're building the Python binding:

AC_ARG_ENABLE(python, 
	[  --enable-python	  build the e4py extension (Python) (no)],
	[python_ok=$enableval],
	[python_ok=no])

if test "$python_ok" != "no" ; then
    if test x"${PYTHON_INC}" != x ; then
	E4GRAPH_TARGETS="$E4GRAPH_TARGETS python"
	E4GRAPH_INSTALL="$E4GRAPH_INSTALL install_python"
    else
	AC_MSG_WARN(Can't build e4py because Python.h not found)
    fi
fi

#----------------------------------------------------------------------
# Are we building the Java binding?
#----------------------------------------------------------------------

dnl Check if we're building the Java binding:

AC_ARG_ENABLE(java,
	[  --enable-java           build the Java binding for e4Graph (no)],
	[java_ok=$enableval],
	[java_ok=no])

if test "$java_ok" != "no" ; then
    if test x"${ac_cv_c_java}" != x ; then
	E4GRAPH_TARGETS="$E4GRAPH_TARGETS java"
	E4GRAPH_INSTALL="$E4GRAPH_INSTALL install_java"
    else
	AC_MSG_WARN(Can't build Java binding because jni.h not found)
    fi
fi

# Expand the variables we computed over the makefile.in:

AC_SUBST(VERSION)
AC_SUBST(E4GRAPH_TARGETS)
AC_SUBST(E4GRAPH_INSTALL)
AC_SUBST(METAKIT_LIB)
AC_SUBST(METAKIT_INC)
AC_SUBST(TCL_INC)
AC_SUBST(TCL_LIB)
AC_SUBST(TCL_STB)
AC_SUBST(TCL_PKG)
AC_SUBST(PYTHON_INC)
AC_SUBST(JAVA_DIR)
AC_SUBST(JAVA_SYS_DIR)
AC_SUBST(EXPAT_INC)
AC_SUBST(EXPAT_LIB)
AC_SUBST(BUILD_DIR)
AC_SUBST(RELEASE_FILE)
AC_SUBST(RELEASE_TMP_DIR)
AC_SUBST(RELEASE_TMP_DIR_PARENT)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(SHLIB_LD)
AC_SUBST(SHLIB_LD_FLAGS)
AC_SUBST(SHLIB_CFLAGS)
AC_SUBST(SHLIB_SUFFIX)
AC_SUBST(CFLAGS_DEBUG)
AC_SUBST(CFLAGS_OPTIMIZE)
AC_SUBST(AR)
AC_SUBST(RANLIB)
AC_SUBST(host_alias)

# Produce the Makefile include file:

AC_OUTPUT(makefile.include)
