#!/bin/sh
# @configure_input@
dnl Process this file with autoconf to produce a configure script.
AC_INIT(Config.mk)
AC_LANG_CPLUSPLUS
MACH=`uname -s`-`uname -r | cut -d. -f1,2`-`uname -m`
AC_SUBST(MACH)
unset LD_LIBRARY_PATH
dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_DEFUN(AC_CHECK_GNU_PROG,
[AC_REQUIRE([AC_CHECK_PROG],[AC_MSG_CHECKING])dnl
AC_MSG_CHECKING(for gnu's $2)
AC_CHECK_PROG($1, "$2", $2, nope)
if test $$1 = "nope"; then
	echo "************************************************************"
	echo "Error  : LINK only works with gnu's \"$2.\""
	echo "       : $2 not found.  $2, or a symbolic link to "
	echo "       : $2, must be placed somewhere in PATH"
	echo "       : (also, remove config.cache)           "
	echo "************************************************************"
fi
AC_SUBST($1)
])
AC_CHECK_GNU_PROG(PARSE, bison)
AC_CHECK_GNU_PROG(FLEX, flex)
AC_PATH_XTRA
if test "$XPM" = ""
then 
  AC_CHECK_LIB(Xpm, XpmCreateXpmImageFromBuffer, [XPM="-lXpm"],,-lX11)
fi
if test "$XPM" != ""
then
	echo "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Xpm"
	XLIBSW="$XLIBSW $X_PRE_LIBS $XPM -lX11 $X_EXTRA_LIBS"
else
	echo "########################################################no Xpm"
	XLIBSW="$XLIBSW $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
fi
echo "extra libs: $X_EXTRA_LIBS"
echo "extra flags: $X_CFLAGS"
echo "extra linker flags: $X_LIBS"
echo "X pre libs: $X_PRE_LIBS"
XINCLUDES=$X_CFLAGS
XLIBSW=$X_LIBS
AC_SUBST(XINCLUDES)
AC_SUBST(X_PRE_LIBS)
AC_SUBST(X_EXTRA_LIBS)
AC_SUBST(XLIBSW)
dnl #--------------------------------------------------------------------------
dnl #--The following macros check Config.mk for headers & libs, then ----------
dnl #--check the system if not found                                 ----------
dnl #--------------------------------------------------------------------------
AC_DEFUN(AC_CHECK_CONFIG_FOR_HEADER,
[AC_REQUIRE([AC_CHECK_HEADERS],[AC_MSG_CHECKING],[AC_MSG_RESULT])dnl
changequote(<<, >>)dnl
$1=`awk 'BEGIN {FS="=";} \
		 /$1/ {if ($<<2>> ~ /[^ 	]/) \
					{print $<<2>>} \
				    else {print ""}}' Config.mk`
changequote([, ])dnl
if test "$$1" = "" ; then
	AC_CHECK_HEADERS($2.h, $1=".", $1="nope")
	if test "$$1" = "nope" ; then
		echo "********************************************************"
		echo "Error  : ****************headers not found**************"
		echo "       : ***********************************************"
		echo "       : $2.h not found.  Please set $1                 "
		echo "       : in Config.mk to be the full pathname of $2.h's "
		echo "       : directory and rerun ./configure.               "
		echo "********************************************************"
	fi
else
	AC_MSG_CHECKING($2.h in Config.mk)
	AC_MSG_RESULT($$1)
fi
AC_SUBST($1)
])
dnl #--------------------------------------------------------------------------
AC_DEFUN(AC_CHECK_CONFIG_FOR_LIB,
[AC_REQUIRE([AC_CHECK_LIB],[AC_MSG_CHECKING],[AC_MSG_RESULT])dnl
changequote(<<, >>)dnl
$1=`awk 'BEGIN {FS="=";} \
		 /$1/ {if ($<<2>> ~ /[^ 	]/) \
					{print $<<2>>} \
				    else {print ""}}' Config.mk`
changequote([, ])dnl
if test "$$1" = "" ; then
    	AC_CHECK_LIB($2, main, $1=".", $1="nope", $3)
	if test "$$1" = "nope" ; then
		echo "********************************************************"
		echo "Error  : ****************library not found**************"
		echo "       : ***********************************************"
		echo "       : library lib$2.a not found.  Please set $1      "
		echo "       : in Config.mk to be the full pathname of        "
		echo "       : lib$2.a and rerun ./configure.                 "
		echo "********************************************************"
	fi
else
	AC_MSG_CHECKING(for lib$2.a in Config.mk)
	AC_MSG_RESULT($$1)
fi
AC_SUBST($1)
])
dnl #--------------------------------------------------------------------------
dnl #--------------------------------------------------------------------------
AC_DEFUN(AC_CHECK_CONFIG_FOR_PATH,
[AC_REQUIRE([AC_CHECK_HEADERS],[AC_MSG_CHECKING],[AC_MSG_RESULT])dnl
changequote(<<, >>)dnl
$1=`awk 'BEGIN {FS="=";} \
                 /$1/ {if ($<<2>> ~ /[^         ]/) \
                                        {print $<<2>>} \
                                    else {print ""}}' Config.mk`
changequote([, ])dnl
if test "$$1" = "" ; then
                echo "********************************************************"
                echo "Error  : ****************string not defined*************"
                echo "       : ***********************************************"
                echo "       : Please set $1                 "
                echo "       : in Config.mk (the full pathname of the root    "
                echo "       : directory) and rerun ./configure.              "
                echo "********************************************************"
else
        AC_MSG_CHECKING($2.h in Config.mk)
        AC_MSG_RESULT($$1)
fi
AC_SUBST($1)
])
dnl #--------------------------------------------------------------------------
dnl #--------------------------------------------------------------------------
AC_DEFUN(AC_CHECK_CONFIG_FOR_STRING,
[AC_REQUIRE([AC_CHECK_HEADERS],[AC_MSG_CHECKING],[AC_MSG_RESULT])dnl
changequote(<<, >>)dnl
$1=`awk 'BEGIN {FS="=";} \
                 /$1/ {if ($<<2>> ~ /[^         ]/) \
                                        {print $<<2>>} \
                                    else {print ""}}' Config.mk`
changequote([, ])dnl
if test "$$1" = "" ; then
                echo "********************************************************"
                echo "Error  : ****************string not defined*************"
                echo "       : ***********************************************"
                echo "       : Please set $1 in Config.mk                     "
                echo "********************************************************"
else
        AC_MSG_CHECKING($2.h in Config.mk)
        AC_MSG_RESULT($$1)
fi
AC_SUBST($1)
])
dnl #--------------------------------------------------------------------------
dnl #########################################################################
dnl Checks for libraries 
dnl #########################################################################
AC_ARG_WITH(fgmp,
        [       --with-fgmp             Use a free replacement of Gnu MP package
],
        MP=libfgmp,
        MP=libgmp)
echo "Multiple precision library:       $MP"
AC_SUBST(MP)
AC_CHECK_CONFIG_FOR_LIB(TCL_STK_LIB, tcl)
AC_CHECK_CONFIG_FOR_LIB(TK_STK_LIB, tk)
AC_CHECK_CONFIG_FOR_LIB(MP_LIB, gmp)
AC_CHECK_CONFIG_FOR_LIB(FLEX_LIB, fl)
dnl #########################################################################
dnl Checks for header files
dnl #########################################################################
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/time.h unistd.h)
AC_CHECK_CONFIG_FOR_HEADER(TCL_STK_INCLUDE,tcl)
AC_CHECK_CONFIG_FOR_HEADER(TK_STK_INCLUDE,tk)
AC_CHECK_CONFIG_FOR_HEADER(MP_STK_INCLUDE,gmp)
dnl #########################################################################
dnl Checks for strings
dnl #########################################################################
AC_CHECK_CONFIG_FOR_PATH(LINK_PREFIX,tcl)
AC_CHECK_CONFIG_FOR_PATH(STK_PREFIX,tcl)
dnl Checks for typedefs, structures, and compiler characteristics.
dnl Checks for library functions.
CPPFLAGS="$XINCLUDES"
LDFLAGS="$X_PRE_LIBS $XLIBSW $X_EXTRA_LIBS"
AC_LANG_CPLUSPLUS
dnl ---------------------------------------------------------------------------
dnl   reset ac_cpp ac_link to try to link conftest*.cpp instead of conftest.cpp
dnl   test for template bug introduced in g++ 2.6.1 & fixed in a patch of 2.6.3
dnl ---------------------------------------------------------------------------
AC_MSG_CHECKING(to see if template support is adequate)
ac_compile='${CXX-g++} $CXXFLAGS $CPPFLAGS conftest*.$ac_ext -c 1>&AC_FD_CC 2>&AC_FD_CC'
ac_link='${CXX-g++} $CXXFLAGS $CPPFLAGS $LDFLAGS conftest*.$ac_ext -o conftest $LIBS 1>&AC_FD_CC 2>&AC_FD_CC'
echo "template <class T> void f(T t) {} template void f(int); void g() { f(8); }" >conftest1.$ac_ext
echo "extern void g(); template <class T> void f(T t){}void main(){f(4); g();}" > conftest2.$ac_ext
if eval $ac_link; then
	AC_MSG_RESULT(yes)
else
	AC_MSG_RESULT(no)
	echo "ERROR: The current version of g++ lacks adequate template       "
	echo "       support to build LINK.  g++ version 2.6.3 requires patch:"
	echo "       ftp.cygnus.com:pub/g++/gcc-2.6.3-template-fix	      "
fi
rm -rf conftest*
dnl -------------------------------------------------------------------------
dnl -- Machine parameters (copied from STk's configure.in for compatibility)-
dnl -------------------------------------------------------------------------
os=`uname -s`
version=`uname -r`
machine=`uname -m`
case $os in
  Linux*)
          case $version in
            1.*) version=1.X;;
          esac
          case $machine in
            i*86) machine=ix86;;
          esac
          echo $version is $machine;;
  SunOS*)
          case $machine in
            sun4*) machine=sun4;;
          esac
esac
AC_CHECK_CONFIG_FOR_STRING(STK_VERSION,nil)
AC_SUBST(STK_VERSION)
AC_CHECK_CONFIG_FOR_STRING(OP_SYS,nil)
AC_SUBST(OP_SYS)
dnl #MACHINE=$os-$version-$machine
dnl #AC_SUBST(MACHINE)
AC_OUTPUT(Makefile)
