AC_INIT()
AC_CONFIG_HEADER(h/gclincl.h)

VERSION=`cat majvers`.`cat minvers`
AC_SUBST(VERSION)

# some parts of this configure script are taken from the tcl configure.in

#
# Arguments
#

help="--enable-maxpage=XXXX will compile in a page table of size XXX (default ${default_maxpage})"
AC_ARG_ENABLE(maxpage,[ --enable-maxpage=XXXX will compile in a page table of size XXX (eg '--enable-maxpage=64*1024' would give 64K pages allowing 256 MB if pages are 4K each)] ,
[AC_DEFINE_UNQUOTED(MAXPAGE,$enable_maxpage)])

AC_ARG_ENABLE(holepage,[ --enable-holepage=XXXX will compile in a XXX-sized hole between the heap and relocatable memory area (eg '--enable-holepage=64*1024' would give 64K pages allowing 256 MB if pages are 4K each)] ,
[AC_DEFINE_UNQUOTED(HOLEPAGE,$enable_holepage)])

AC_ARG_ENABLE(vssize,[ --enable-vssize=XXXX will compile in a value stack of size XXX] ,
[AC_DEFINE_UNQUOTED(VSSIZE,$enable_vssize)])

AC_ARG_ENABLE(bdssize,[ --enable-bdssize=XXXX will compile in a binding stack of size XXX] ,
[AC_DEFINE_UNQUOTED(BDSSIZE,$enable_bdssize)])

AC_ARG_ENABLE(ihssize,[ --enable-ihssize=XXXX will compile in a invocation history stack of size XXX] ,
[AC_DEFINE_UNQUOTED(IHSSIZE,$enable_ihssize)])

AC_ARG_ENABLE(frssize,[ --enable-frssize=XXXX will compile in a frame stack of size XXX] ,
[AC_DEFINE_UNQUOTED(FRSSIZE,$enable_frssize)])

AC_ARG_ENABLE(machine,[ --enable-machine=XXXX will force the use of one of the definitions in h/XXXX.defs] ,
[enable_machine=$enableval],[enable_machine=""])

#AC_ARG_ENABLE(gmp,[ --enable-gmp=no will disable use of GMP gnu multiprecision arithmetic, (default is =yes)] ,
#[use_gmp=$enableval],[use_gmp="yes"])

use_gmp="yes"

AC_ARG_ENABLE(notify,[ --enable-notify=no will disable the automatic notification of gcl maintainers of successful builds/problems] ,
[enable_notify=$enableval],[enable_notify="yes"])

AC_ARG_ENABLE(tkconfig,[ --enable-tkconfig=XXXX will force the use of a TK_CONFIG_PREFIX=XXXXX as place to look for tkConfig.sh and tclConfig.sh ] ,
[TK_CONFIG_PREFIX=$enableval],[TK_CONFIG_PREFIX="unknown"])


AC_ARG_ENABLE(tclconfig,[ --enable-tclconfig=XXXX will force the use of a TCL_CONFIG_PREFIX=XXXXX as place to look for tclConfig.sh and tclConfig.sh ] ,
[TCL_CONFIG_PREFIX=$enableval],[TCL_CONFIG_PREFIX="unknown"])

AC_ARG_ENABLE(infodir,[ --enable-infodir=XXXX will force the use of a INFO_DIR=XXXXX as place to look for info ] ,
[INFO_DIR=$enableval],[INFO_DIR="unknown"])
INFO_DIR=`eval echo $INFO_DIR/`

AC_ARG_ENABLE(emacsdir,[ --enable-emacsdir=XXXX will manually specify the location for elisp files ] ,
[EMACS_SITE_LISP=$enableval],[EMACS_SITE_LISP="unknown"])
EMACS_SITE_LISP=`eval echo $EMACS_SITE_LISP`

AC_ARG_ENABLE(common-binary,[ --enable-common-binary=yes forces use of lowest common denominator instruction sets, (default is =yes)] ,
[use_common_binary=$enableval],[use_common_binary="yes"])

AC_ARG_ENABLE(japi,[ --enable-japi=yes will compile in support for the JAPI graphical interface if present on your system],
[try_japi=$enableval],[try_japi="no"])

AC_ARG_ENABLE(xdr,[ --enable-xdr=yes will compile in support for XDR],
[try_xdr=$enableval],[try_xdr="no"])

#
# Host information 
#


AC_CANONICAL_HOST
canonical=$host
my_host_kernel=`echo $host_os | awk '{j=split($1,A,"-");print A[[1]]}'`
my_host_system=`echo $host_os | awk '{j=split($1,A,"-");if (j>=2) print A[[2]]}'`
AC_DEFINE_UNQUOTED(HOST_CPU,"`echo $host_cpu | awk '{print toupper($0)}'`")
AC_DEFINE_UNQUOTED(HOST_KERNEL,"`echo $my_host_kernel | awk '{print toupper($0)}'`")
if test "$my_host_system" != "" ; then 
	AC_DEFINE_UNQUOTED(HOST_SYSTEM,"`echo $my_host_system | awk '{print toupper($0)}'`")
fi
## host=CPU-COMPANY-SYSTEM
AC_MSG_RESULT(host=$host)

PROCESSOR_FLAGS=${PROCESSOR_FLAGS:-""}

use=unknown
TLDFLAGS=""
case $canonical in
     older)
     use=386-bsd;;

     *x86_64*linux*)
	use=amd64-linux;;

     *86*linux*)
	use=386-linux;;

# m6800 not working with gcc-3.2
     m68k*linux*)
       if test "$use_common_binary" = "yes"; then
         host=m68020-unknown-linux-gnu
         echo "The host is canonicalised to $host"
       fi
     	use=m68k-linux;;

     alpha*linux*)
     	use=alpha-linux;;

     mips*linux*)
     	use=mips-linux;;

     mipsel*linux*)
     	use=mipsel-linux;;

     sparc*linux*)
     	use=sparc-linux;;

     arm*linux*)
     	use=arm-linux;;

     s390*linux*)
     	use=s390-linux;;

     ia64*linux*)
     	use=ia64-linux;;

     hppa*linux*)
     	use=hppa-linux;;

     powerpc*linux*)
     	use=powerpc-linux;;

     powerpc-*-darwin*)
     	use=powerpc-macosx;;
     
     alpha-dec-osf)
     use=alpha-osf1;;

     mips-dec-ultrix)
     use=dec3100;;

     old)
     use=dos-go32;;

     *86*-freebsd*)
     use=FreeBSD;;

     hp3*-*hpux*)
     use=hp300;;

     hp3*-*-*bsd*)
     use=hp300-bsd;;

     hppa*-*hpux*)
     use=hp800;;

     mips-sgi-irix)
       case $system in
           IRIX5*)
            use=irix5;;
           IRIX6*)
            use=irix6;;
           IRIX3*)
            use=sgi4d;;
       esac ;;
 

     m68k-apple-aux*)
     use=mac2;;

     old)
     use=mp386;;

     *86-ncr-sysv4)
     use=ncr;;

     *[3-9]86-*netbsd*)
     use=NetBSD;;

     old)
     use=NeXT;;

     old)
     use=NeXT30-m68k;;

     *86-*nextstep*)
     use=NeXT32-i386;;

     *m68*-*nextstep*)
     use=NeXT32-m68k;;

     *rs6000-*-aix4*)
     use=rios;;

     *rs6000-*-aix3*)
     use=rios-aix3;;

     old)
     use=rt_aix;;

     old)
     use=sgi;;

     sparc-sun-solaris*)
     use=solaris;;

     i?86-pc-solaris*)
     use=solaris-i386;;

     old)
     use=sun2r3;;

     old)
     use=sun3;;

     m68*-sunos*)
     use=sun3-os4;;

     old)
     use=sun386i;;

     sparc*sunos*)
     use=sun4;;

     *86-sequent-dynix)
     use=symmetry;;

     u370*aix)
     use=u370_aix;;

     old)
     use=vax;;

     i*mingw*)
       if test "$use_common_binary" = "yes"; then
         host=i386-pc-mingw32
	 PROCESSOR_FLAGS="-mcpu=i386 -march=i386 "
         echo "The host is canonicalised to $host"
       fi
     use=mingw;;

     i*cygwin*)
	if $CC -v 2>&1 | fgrep ming > /dev/null ;
            then use=mingw
          else  use=gnuwin95
        fi;;

     *openbsd*)
	# 'ld -Z' means disable W^X
	TLDFLAGS="$TLDFLAGS -Z"
	use=FreeBSD;;
  
esac

AC_SUBST(PROCESSOR_FLAGS)

echo enable_machine=$enable_machine
if test "x$enable_machine" != "x" ; then
  use=$enable_machine
fi

def_dlopen="no"
def_statsysbfd="yes"
def_custreloc="no"
def_locbfd="no"
def_oldgmp="no"
def_pic="no";
def_static="no";
def_debug="no";
case $use in 
     *linux)
	ln -snf linux.defs h/$use.defs;
	case $use in
		alpha*)
			def_dlopen="yes" ; def_statsysbfd="no" ;;
		mips*)
			def_dlopen="yes" ; def_statsysbfd="no" ;;
# def_static -- Function descriptors are currently realized at runtime in a non-reproducible fashion
# on these architectures -- CM			
		ia64*)
			def_dlopen="yes" ; def_statsysbfd="no" ; def_static="no" ;;
		hppa*)
			def_dlopen="yes" ; def_statsysbfd="no" ; def_pic="yes" ; def_debug="yes" ;;
#		m68k*)
#			def_oldgmp="yes" ;;
	esac;;
    *mingw*)
	def_statsysbfd="no" ; def_custreloc="yes" ;;
    powerpc-macosx)
	def_statsysbfd="no" ; def_locbfd="yes" ;;
esac

AC_ARG_ENABLE(dlopen,
	[ --enable-dlopen uses dlopen for loading objects, which can then not be retained  in saved images ]
	,,enable_dlopen="$def_dlopen")
AC_ARG_ENABLE(statsysbfd,
	[ --enable-statsysbfd uses a static sytem bfd library for loading and relocationing object files ]
	,,enable_statsysbfd="$def_statsysbfd")
AC_ARG_ENABLE(dynsysbfd,
	[ --enable-dynsysbfd uses a dynamic shared sytem bfd library for loading and relocationing object files ]
	,,enable_dynsysbfd="no")
AC_ARG_ENABLE(locbfd,
	[ --enable-locbfd uses a static bfd library built from this source tree for loading and relocationing object files ]
	,,enable_locbfd="$def_locbfd")
AC_ARG_ENABLE(custreloc,
	[ --enable-custreloc uses custom gcl code if available for loading  and relocationing object files ]
	,,enable_custreloc="$def_custreloc")
AC_ARG_ENABLE(debug,
	[ --enable-debug builds gcl with -g in CFLAGS to enable running under gdb ]
	,,enable_debug="$def_debug")
AC_ARG_ENABLE(gprof,
	[ --enable-gprof builds gcl with -pg in CFLAGS to enable profiling with gprof ]
	,,enable_gprof="no")
AC_ARG_ENABLE(static,[ --enable-static will link your GCL against static as opposed to shared system libraries ] ,
        [enable_static=$enableval],[enable_static="$def_static"])
AC_ARG_ENABLE(pic,
	[ --enable-pic builds gcl with -fPIC in CFLAGS ]
	,,enable_pic="$def_pic")

AC_ARG_ENABLE(oldgmp,
	[ --enable-oldgmp will link against gmp2 instead of gmp3 ]
	,,enable_oldgmp="$def_oldgmp")

AC_ARG_ENABLE(dynsysgmp,
	[ --enable-dynsysgmp will link against the system libgmp3 overriding certain functions with patched versions from the local source ]
	,,enable_dynsysgmp="no")

load_opt="0"
if test "$enable_dlopen" = "yes" ; then
   load_opt=1
fi
if test "$enable_statsysbfd" = "yes" ; then
   case $load_opt in
   0) load_opt=1;;
   1) load_opt=2;;
   esac
fi
if test "$enable_dynsysbfd" = "yes" ; then
   case $load_opt in
   0) load_opt=1;;
   1) load_opt=2;;
   2) load_opt=3;;
   esac
fi
if test "$enable_locbfd" = "yes" ; then
   case $load_opt in
   0) load_opt=1;;
   1) load_opt=2;;
   2) load_opt=3;;
   3) load_opt=4;;
   esac
fi
if test "$enable_custreloc" = "yes" ; then
   case $load_opt in
   0) load_opt=1;;
   1) load_opt=2;;
   2) load_opt=3;;
   3) load_opt=4;;
   4) load_opt=5;;
   esac
fi

if test "$load_opt" != "1" ; then
	echo "Exactly one loader option must be chosen: dlopen=$enable_dlopen statsysbfd=$enable_statsysbfd dynsysbfd=$enable_dynsysbfd locbfd=$enable_locbfd custreloc=$enable_custreloc"
	exit 1
fi

TLIBS=""
if test "$enable_static" = "yes" ; then
   TLIBS="-static $TLIBS";
fi

## finally warn if we did not find a recognized machine.s
##
#if test "$use" = "unknown" ; then
#types=`echo h/*.defs` | sed -e "s:h/::g" -e "s:\.defs:g"`
#echo got canonical=$canonical, but was not recognized. 
#echo Unable to guess type to use.  Try one of 
#exit(1)
#fi

AC_MSG_RESULT(use=$use)	


#
# System programs
#

# We set the default CFLAGS below, and don't want the autoconf default
# CM 20040106
if test "$CFLAGS" = "" ; then
	CFLAGS=" "
fi
if test "$LDFLAGS" = "" ; then
	LDFLAGS=" "
fi

AC_PROG_CC
AC_PROG_CPP
AC_SUBST(CC)


# can only test for numbers -- CM
# if test "${GCC}" -eq "yes" ; then
#if [[ "${GCC}" = "yes" ]] ; then
# Allog for environment variable overrides on compiler selection -- CM
#GCC=$CC
#else
#GCC=""
#fi
# subst GCC not only under 386-linux, but where available -- CM


TCFLAGS="-Wall -DVOL=volatile -fsigned-char"
if test "$GCC" = "yes" ; then
	TCFLAGS="$TCFLAGS -pipe"
	case $use in
	     *mingw*)
	        echo "WARNING: Remove -fno-zero-initialized-in-bss from makedefs if gcc less than 3.3.1."
	        echo "         It is ptherwise needed for the Unexec stuff to work."
		TCFLAGS="$TCFLAGS -fno-zero-initialized-in-bss -mms-bitfields";;
        esac
fi
#if test -f /proc/sys/kernel/exec-shield ; then
#	exec_stat=`cat /proc/sys/kernel/exec-shield`
#	if test "$exec_stat" != "0" ; then
#		# CFLAGS here to hopefully cover the DBEGIN routine below
#		CFLAGS="$CFLAGS -Wa,--execstack"
#	fi
#fi

TO3FLAGS=""
TO2FLAGS=""

#TFPFLAG="-fomit-frame-pointer"
# FIXME -- remove when mingw compiler issues are fixed
case "$use" in
  *mingw*)
	TFPFLAG="";;
  *)
	TFPFLAG="-fomit-frame-pointer";;
esac

if test "$enable_gprof" = "yes" ; then
	TCFLAGS="$TCFLAGS -pg"
	TLIBS="$TLIBS -pg"
	TFPFLAG=""
	AC_DEFINE(GCL_GPROF)
fi

if test "$enable_debug" = "yes" ; then
	TCFLAGS="$TCFLAGS -g"
	# for subconfigurations
	CFLAGS="$CFLAGS -g"
else
# FIXME -- remove when mingw compiler issues are fixed
	case "$use" in
	  *mingw*)
		TO3FLAGS="-O3 $TFPFLAG";;
	  *)
		TO3FLAGS="-O3 $TFPFLAG";;
	esac
#	TO3FLAGS="-O3 $TFPFLAG"
	TO2FLAGS="-O"
fi

AC_CHECK_PROGS(AWK,gawk nawk awk,"")

# gcc on ppc cannot compile our new_init.c with full opts --CM
TONIFLAGS=""
case $use in 
     *linux)
	case $use in
#		amd64*) # stack-boundary option does not work
#			TCFLAGS="$TCFLAGS -m64 -mpreferred-stack-boundary=8";;  
		alpha*)
			TCFLAGS="$TCFLAGS -mieee";;
#		m68k*)
#			TCFLAGS="$TCFLAGS -ffloat-store";;
		hppa*)
			TCFLAGS="$TCFLAGS -ffunction-sections"
			if test "$enable_debug" != "yes" ; then TO3FLAGS="-O $TFPFLAG" ; fi
			if test "$enable_debug" != "yes" ; then TO2FLAGS="-O" ; fi
			;;
		arm*)
			TCFLAGS="$TCFLAGS -mlong-calls";;	
		powerpc*) 
			if $CC -v 2>&1 | grep -q "gcc version 3.2" ; then
			   echo Reducing optimization for buggy gcc-3.2
			   if test "$enable_debug" != "yes" ; then TONIFLAGS="-O $TFPFLAG" ; fi
			fi;
			echo Probing for longcall
			if ! $CC -v 2>&1 | $AWK '/^gcc version / {split($3,A,".");if (A[[1]]+0>=3 && A[[2]]+0>=3) exit 1;}'; then
			   echo Enabling longcall on gcc 3.3 or later
			   TCFLAGS="$TCFLAGS -mlongcall"
			   echo Reducing optimization for buggy gcc 3.3 or later
			   if test "$enable_debug" != "yes" ; then TONIFLAGS="-O $TFPFLAG" ; fi
			fi;;
	esac;;
esac
if test "$enable_pic" = "yes" ; then
	TCFLAGS="$TCFLAGS -fPIC"
fi


# Step 1: set the variable "system" to hold the name and version number
# for the system.  This can usually be done via the "uname" command, but
# there are a few systems, like Next, where this doesn't work.

AC_MSG_CHECKING([system version (for dynamic loading)])
if machine=`uname -m` ; then true; else machine=unknown ; fi

AC_CHECK_PROGS(MAKEINFO,makeinfo,"false")
AC_SUBST(MAKEINFO)

if test -f /usr/lib/NextStep/software_version; then
    system=NEXTSTEP-`${AWK} '/3/,/3/' /usr/lib/NextStep/software_version`
else
    system=`uname -s`-`uname -r`
    if test "$?" -ne 0 ; then
	AC_MSG_RESULT([unknown (can't find uname command)])
	system=unknown
    else
	# Special check for weird MP-RAS system (uname returns weird
	# results, and the version is kept in special file).
    
	if test -r /etc/.relid -a "X`uname -n`" = "X`uname -s`" ; then
	    system=MP-RAS-`${AWK} '{print $3}' /etc/.relid'`
	fi
	if test "`uname -s`" = "AIX" ; then
	    system=AIX-`uname -v`.`uname -r`
	fi
	AC_MSG_RESULT($system)
    fi
fi

#MY_SUBDIRS=

#
# GMP
#

rm -f makedefsafter

MP_INLCUDE=""
if test $use_gmp = yes ; then

 AC_MSG_CHECKING([use_gmp=yes, doing configure in gmp directory])
 echo
 echo "#"
 echo "#"
 echo "# -------------------"
 echo "# Subconfigure of GMP"
 echo "#"
 echo "#"

 if test "$enable_oldgmp" = "yes" ; then
	GMPDIR=gmp
 else
	GMPDIR=gmp3
 fi

 if test "$use_common_binary" = "yes"; then
   cd $GMPDIR && ./configure --host=$host && cd ..
 else
   cd $GMPDIR && ./configure && cd ..
 fi
#MY_SUBDIRS="$MY_SUBDIRS $GMPDIR" 

 echo "#"
 echo "#"
 echo "#"
 echo "# Subconfigure of GMP done"
 echo "# ------------------------"
 echo "#"

 PATCHED_SYMBOLS=""
 if test "$enable_dynsysgmp" = "yes" ; then
	AC_CHECK_HEADER(gmp.h,
		AC_CHECK_LIB(gmp,__gmpz_init,
			AC_MSG_CHECKING("for external gmp version")
			AC_TRY_RUN([#include <gmp.h>
				    int main() {
				    #if __GNU_MP_VERSION == 4
					return 0;
				    #else
					return -1;
				    #endif
				    }],
				MPFILES=$GMPDIR/mpn/mul_n.o
				PATCHED_SYMBOLS=__gmpn_toom3_mul_n
				if test "$use" = "m68k-linux" ; then
					MPFILES="$MPFILES $GMPDIR/mpn/lshift.o $GMPDIR/mpn/rshift.o"
					PATCHED_SYMBOLS="$PATCHED_SYMBOLS __gmpn_lshift __gmpn_rshift"
				fi
				TLIBS="$TLIBS -lgmp"
				echo "#include \"gmp.h\"" >foo.c
				echo "int main() {return 0;}" >>foo.c
				MP_INCLUDE=`cpp foo.c | grep /gmp.h | head -1l | $AWK '{print $3}' | tr -d '"'`
				rm -f foo.c,
				echo "Cannot use dynamic gmp lib" , echo "Cannot use dynamic gmp lib" ),
			echo "Cannot use dynamic gmp lib" ,),
		echo "Cannot use dynamic gmp lib" ,)
fi

if test "$MP_INCLUDE" = "" ; then			

	cp $GMPDIR/gmp.h h/gmp.h
	MP_INCLUDE=h/gmp.h
	MPFILES=gmp_all

fi

AC_MSG_CHECKING("for leading underscore in object symbols")
cat>foo.c <<EOFF
#include <math.h>
int main() {double d=0.0;cos(d);return 0;}
EOFF
$CC -c foo.c -o foo.o
if nm foo.o |grep " U " | grep "_cos" >/dev/null ; then
	LEADING_UNDERSCORE=1
	AC_MSG_RESULT("yes")
else
	LEADING_UNDERSCORE=""
	AC_MSG_RESULT("no")
fi
AC_MSG_CHECKING("for GNU ld option -Map")
touch map
$CC -o foo [ -Wl,-Map ] map foo.o >/dev/null 2>&1
if test `cat map | wc -l` != "0" ; then
	AC_MSG_RESULT("yes")
	AC_DEFINE(HAVE_GNU_LD)
	GNU_LD=1
else
	AC_MSG_RESULT("no")
	GNU_LD=
fi
rm -f foo.c foo.o foo map

 AC_MSG_CHECKING("for size of gmp limbs")
 AC_TRY_RUN([#include <stdio.h>
	#include "$MP_INCLUDE"
	int main() {
	FILE *fp=fopen("conftest1","w");
	fprintf(fp,"%u",sizeof(mp_limb_t));
	fclose(fp);
	return 0;
	}],mpsize=`cat conftest1`,mpsize=0,mpsize=0)
 if test "$mpsize" = "0" ; then
	echo "Cannot determine mpsize"
	exit 1
 fi
 AC_DEFINE_UNQUOTED(MP_LIMB_BYTES,$mpsize)
 AC_MSG_RESULT($mpsize) 

 AC_MSG_CHECKING("_SHORT_LIMB")
 AC_TRY_RUN([#include <stdio.h>
	#include "$MP_INCLUDE"
	int main() {
	#ifdef _SHORT_LIMB
	return 0;
	#else
	return 1;
	#endif
	}],AC_DEFINE(__SHORT_LIMB) AC_MSG_RESULT(yes),AC_MSG_RESULT(no),AC_MSG_RESULT(no))
 AC_MSG_CHECKING("_LONG_LONG_LIMB")
 AC_TRY_RUN([#include <stdio.h>
	#include "$MP_INCLUDE"
	int main() {
	#ifdef _LONG_LONG_LIMB
	return 0;
	#else
	return 1;
	#endif
	}],AC_DEFINE(__LONG_LONG_LIMB) AC_MSG_RESULT(yes),AC_MSG_RESULT(no),AC_MSG_RESULT(no))

 GMP=1
 AC_DEFINE(GMP)
 AC_SUBST(GMP)
 AC_SUBST(GMPDIR)
 echo > makedefsafter
 echo "MPFILES=$MPFILES" >> makedefsafter
 echo "PATCHED_SYMBOLS=$PATCHED_SYMBOLS" >> makedefsafter
 echo >> makedefsafter
fi


#
# X windows
# 

AC_PATH_XTRA
echo $X_CFLAGS
echo $X_LIBS
echo $X_EXTRA_LIBS
echo $X_PRE_LIBS

miss=0
AC_CHECK_LIB(Xmu,main,X_LIBS="$X_LIBS -lXmu",miss=1,$X_LIBS)
AC_CHECK_LIB(Xt,main,X_LIBS="$X_LIBS -lXt",miss=1,$X_LIBS)
AC_CHECK_LIB(Xext,main,X_LIBS="$X_LIBS -lXext",miss=1,$X_LIBS)
AC_CHECK_LIB(Xaw,main,X_LIBS="$X_LIBS -lXaw",miss=1,$X_LIBS)
AC_CHECK_LIB(X11,main,X_LIBS="$X_LIBS -lX11",miss=1,$X_LIBS)
 
if test "$miss" = "1" ; then
	X_CFLAGS=
	X_LIBS=
	echo missing x libraries -- cannot compile xgcl
fi


AC_SUBST(X_LIBS)
AC_SUBST(X_CFLAGS)

#
# Dynamic loading
#

if test "$enable_dlopen" = "yes" ; then

 	AC_CHECK_LIB(dl,dlopen,have_dl=1,have_dl=0)
 	if test "$have_dl" = "0" ; then
 		echo "Cannot find dlopen in -dl"
 		exit 1
 	fi
dnl	AC_SEARCH_LIBS(dlopen, dl, have_dl=1, AC_ERROR(dlopen not found))
dnl LIBS and TLIBS - why not merged from the beginning?

	TLIBS="$TLIBS -ldl -rdynamic"
dnl	TLIBS="$TLIBS -rdynamic"
	AC_DEFINE(USE_DLOPEN)
fi

if test "$enable_statsysbfd" = "yes" || test "$enable_dynsysbfd" = "yes" ; then
	AC_CHECK_HEADER(bfd.h,
		AC_CHECK_LIB(bfd,bfd_init,
		#
		# Old binutils appear to need CONST defined to const
		#
			AC_MSG_CHECKING(if need to define CONST for bfd)
			AC_TRY_RUN([#define IN_GCC
				    #include <bfd.h>
				    int main() { symbol_info t; return 0;}],
				AC_MSG_RESULT(no),
				AC_TRY_RUN([#define CONST const
                                            #define IN_GCC
					    #include <bfd.h>
					    int main() {symbol_info t; return 0;}],
					AC_MSG_RESULT(yes) 
					AC_DEFINE(NEED_CONST),
					AC_MSG_RESULT(cannot use bfd) exit 1;,
					AC_MSG_RESULT(cannot use bfd) exit 1;),
				AC_MSG_RESULT(cannot use bfd) exit 1;)
			,,-liberty))

	AC_DEFINE(HAVE_LIBBFD)

#
# BFD boolean syntax
#

	AC_MSG_CHECKING(for useable bfd_boolean)
	AC_TRY_RUN([#define IN_GCC
		    #include <bfd.h>
		    bfd_boolean foo() {return FALSE;}
		    int main() {return 0;}],
		AC_MSG_RESULT(yes) 
		AC_DEFINE(HAVE_BFD_BOOLEAN),
		AC_MSG_RESULT(no),
		AC_MSG_RESULT(no))


#
# FIXME: Need to workaround mingw before this point -- CM
#
	if test "$enable_statsysbfd" = "yes" && ! $CC -v 2>&1 | fgrep ming > /dev/null ; then
		echo 'int main() {bfd_init();bfd_openr("/dev/null",0);return 0;}' >foo.c
		MP=`$CC [ -Wl,-M ] -static -o foo foo.c -lbfd -liberty 2>&1 | grep -v : | tr '()' '\012\012' | $AWK '{print $NF}' | sort | uniq`
		rm -f foo.c foo
		if echo $MP | tr ' ' '\012' | grep -q libbfd.a ; then
		   TLIBS="$TLIBS `echo $MP | tr ' ' '\012' | grep libbfd.a`"
		else
		   echo Guessing path to libbfd.a due to gcc bug
		   TLIBS="$TLIBS /usr/lib/libbfd.a"
		fi   
		if echo $MP | tr ' ' '\012' | grep -q libiberty.a ; then
		   TLIBS="$TLIBS `echo $MP | tr ' ' '\012' | grep libiberty.a`"
		else
		   echo Guessing path to libiberty.a due to gcc bug
		   TLIBS="$TLIBS /usr/lib/libiberty.a"
		fi   
	else
		TLIBS="$TLIBS -lbfd -liberty"
	fi
fi

if test "$enable_locbfd" = "yes" ; then

	# check for gettext.  It is part of glibc, but others
	# need GNU gettext separately.
	AC_CHECK_HEADER(libintl.h, true,
			   AC_MSG_ERROR(libintl.h (gettext) not found))
	AC_SEARCH_LIBS(dgettext, intl, true, AC_MSG_ERROR(gettext library not found))
 
	echo "#"
	echo "#"
	echo "# -------------------------"
	echo "# Subconfigure of LIBIBERTY"
	echo "#"
	echo "#"
	cd binutils/libiberty && chmod +x configure && ./configure && cd ../..
#       MY_SUBDIRS="$MY_SUBDIRS binutils/libiberty " 
	echo "#"
	echo "#"
	echo "#"
	echo "# Subconfigure of LIBIBERTY done"
	echo "# ------------------------------"
	echo "#"
	echo "#"
	echo "#"
	echo "# -------------------"
	echo "# Subconfigure of BFD"
	echo "#"
	echo "#"
	cd binutils/bfd && chmod +x configure && ./configure && cd ../..
#       MY_SUBDIRS="$MY_SUBDIRS binutils/bfd " 
	echo "#"
	echo "#"
	echo "#"
	echo "# Subconfigure of BFD done"
	echo "# ------------------------"
	echo "#"
#	TLIBS="$TLIBS `pwd`/binutils/bfd/libbfd.a `pwd`/binutils/libiberty/libiberty.a"
	AC_DEFINE(HAVE_LIBBFD)
	BUILD_BFD="h/bfd.h h/bfdlink.h h/ansidecl.h h/symcat.h"
	AC_SUBST(BUILD_BFD)
fi

#AC_CONFIG_SUBDIRS($MY_SUBDIRS)

# Find where Data begins.  This is used by the storage allocation
# mechanism, in the PAGE macro.  This offset is subtracted from
# addresses, in calculating a page for an address in the heap.

AC_CHECK_SIZEOF(long,0)
AC_MSG_CHECKING(sizeof struct contblock)

# work around MSYS pwd result incompatibility
if test "$use" = "mingw" ; then
AC_TRY_RUN([#include <stdio.h>
	#define EXTER
	#include "$MP_INCLUDE"
	#include "h/enum.h"
	#include "h/object.h"
	int main(int argc,char **argv,char **envp) {
	FILE *f=fopen("conftest1","w");
	fprintf(f,"%u",sizeof(struct contblock));
	fclose(f);
	return 0;
	}],sizeof_contblock=`cat conftest1`,
	   echo Cannot find sizeof struct contblock;exit 1,
	   echo Cannot find sizeof struct contblock;exit 1)
else
AC_TRY_RUN([#include <stdio.h>
	#define EXTER
	#include "$MP_INCLUDE"
	#include "`pwd`/h/enum.h"
	#include "`pwd`/h/object.h"
	int main(int argc,char **argv,char **envp) {
	FILE *f=fopen("conftest1","w");
	fprintf(f,"%u",sizeof(struct contblock));
	fclose(f);
	return 0;
	}],sizeof_contblock=`cat conftest1`,
	   echo Cannot find sizeof struct contblock;exit 1,
	   echo Cannot find sizeof struct contblock;exit 1)
fi
AC_MSG_RESULT($sizeof_contblock)
AC_DEFINE_UNQUOTED(SIZEOF_CONTBLOCK,$sizeof_contblock)



AC_CHECK_HEADERS(endian.h,
	AC_MSG_CHECKING("endianness")
	AC_TRY_RUN([#define __ARMEB__
		#include <endian.h> 
		int main() { return BYTE_ORDER == __LITTLE_ENDIAN ? 0 : 1;}],
	AC_DEFINE(LITTLE_END) AC_MSG_RESULT(little),
	AC_MSG_RESULT(big),AC_MSG_RESULT(big)))
AC_SUBST(LITTLE_END)


# On systems with execshield, brk is randomized.  We need to catch
# this and restore the traditional behavior here

AC_MSG_CHECKING([for sbrk])
HAVE_SBRK=""
AC_TRY_RUN([#include <unistd.h>
	    #include <stdio.h>
	    int main() {
		FILE *f;
		if (!(f=fopen("conftest1","w")))
			return -1;
		fprintf(f,"%u",sbrk(0));
		return 0;
		}],
		HAVE_SBRK=1
		AC_MSG_RESULT(yes),
		AC_MSG_RESULT([no: WARNING you must be able to emulate sbrk: as on mingw or macosx]),
		AC_MSG_RESULT([no: WARNING you must be able to emulate sbrk: as on mingw or macosx]))

if test "$HAVE_SBRK" = "1" ; then
	AC_MSG_CHECKING([for randomized sbrk])
	AC_TRY_RUN([#include <unistd.h>
		    #include <stdio.h>
		    int main() {
			FILE *f;
			if (!(f=fopen("conftest1","w")))
				return -1;
			fprintf(f,"%u",sbrk(0));
			return 0;
			}],
			SBRK=`cat conftest1`,SBRK=0,SBRK=0)
	if test "$SBRK" = "0" ; then
		AC_MSG_RESULT(cannot trap sbrk)
		exit 1
	fi
	AC_TRY_RUN([#include <unistd.h>
		    #include <stdio.h>
		    int main() {
			FILE *f;
			if (!(f=fopen("conftest1","w")))
				return -1;
			fprintf(f,"%u",sbrk(0));
			return 0;
			}],
			SBRK1=`cat conftest1`,SBRK1=0,SBRK1=0)
	if test "$SBRK1" = "0" ; then
		AC_MSG_RESULT(cannot trap sbrk)
		exit 1
	fi
	
	if test "$SBRK" != "$SBRK1" ; then
		AC_MSG_RESULT(yes)
		AC_MSG_CHECKING([for randomized brk remedy])
		AC_TRY_RUN([#include <syscall.h>
			    #include <linux/personality.h>
	         	    #include <stdio.h>
			    #include <unistd.h>
			    int main(int argc,char * argv[]) {
				FILE *f;
				#if SIZEOF_LONG == 4
				if (!syscall(SYS_personality,PER_LINUX32))
				#else
				if (!syscall(SYS_personality,PER_LINUX))
				#endif
					execvp(argv[0],argv);
				if (!(f=fopen("conftest1","w")))
					return -1;
				fprintf(f,"%u",sbrk(0));
				return 0;
				}],
				SBRK=`cat conftest1`,SBRK=0,SBRK=0)
		if test "$SBRK" = "0" ; then
			AC_MSG_RESULT(cannot trap sbrk)
			exit 1
		fi
		AC_TRY_RUN([#include <syscall.h>
			    #include <linux/personality.h>
			    #include <stdio.h>
			    #include <unistd.h>
			    int main(int argc,char * argv[]) {
				FILE *f;
				#if SIZEOF_LONG == 4
				if (!syscall(SYS_personality,PER_LINUX32))
				#else
				if (!syscall(SYS_personality,PER_LINUX))
				#endif
					execvp(argv[0],argv);
				if (!(f=fopen("conftest1","w")))
					return -1;
				fprintf(f,"%u",sbrk(0));
				return 0;
				}],
				SBRK1=`cat conftest1`,SBRK1=0,SBRK1=0)
		if test "$SBRK1" = "0" ; then
			AC_MSG_RESULT(cannot trap sbrk)
			exit 1
		fi
		if test "$SBRK" = "$SBRK1" ; then
			AC_MSG_RESULT(yes)
			AC_DEFINE(NEED_NONRANDOM_SBRK)
		else
			AC_MSG_RESULT(no)
			echo "Cannot build with randomized sbrk"
			exit 1
		fi 
	else
		AC_MSG_RESULT(no)
	fi
fi


old_LDFLAGS="$LDFLAGS"
LDFLAGS="$TLDFLAGS"
AC_MSG_CHECKING("finding DBEGIN")
AC_TRY_RUN([#include <stdio.h>
            #include <stdlib.h>
	    #ifdef NEED_NONRANDOM_SBRK
		   #include <syscall.h>
		   #include <linux/personality.h>
		   #include <unistd.h>
	   #endif
int
main(int argc,char * argv[])
{
  char *b;
  FILE *fp;

#ifdef NEED_NONRANDOM_SBRK
#if SIZEOF_LONG == 4
if (!syscall(SYS_personality,PER_LINUX32))
#else
if (!syscall(SYS_personality,PER_LINUX))
#endif
     execvp(argv[0],argv);
#endif  
  b = (void *) malloc(1000);
  fp = fopen("conftest1","w");

#ifdef _WIN32
  fprintf(fp,"_dbegin");
#else
#if defined (__APPLE__) && defined (__MACH__)
  fprintf(fp,"get_dbegin()");
#else
  fprintf(fp,"0x%lx",((unsigned long) b) & ~(unsigned long)0xffffff);
#endif
#endif
  fclose(fp);
  return 0;
}],dbegin=`cat conftest1`,dbegin=0,dbegin=0)
AC_DEFINE_UNQUOTED(DBEGIN,$dbegin \
/* where data begins */
)
AC_MSG_RESULT(got $dbegin)
LDFLAGS="$old_LDFLAGS"


AC_MSG_CHECKING("finding CSTACK_ADDRESS")
AC_TRY_RUN([#include <stdio.h>
main()
{
  char *b ;
  FILE *fp = fopen("conftest1","w");
  fprintf(fp,"%ld",((long) &b));
  fclose(fp);
  return 0;
}],cstack_address=`cat conftest1`,cstack_address=0,cstack_address=0)
AC_DEFINE_UNQUOTED(CSTACK_ADDRESS,$cstack_address \
)
AC_MSG_RESULT(got $cstack_address)



AC_MSG_CHECKING("sizeof long long int")
AC_TRY_RUN([#include <stdio.h>
main()
{
  if (sizeof(long long int) == 2*sizeof(long)) return 0;
  return 1;
}
],[AC_DEFINE(HAVE_LONG_LONG)
AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no),
AC_MSG_RESULT(no)
)

AC_SUBST(HAVE_LONG_LONG)

# readline
AC_ARG_ENABLE(readline,
	[--enable-readine    enables command line completion via the readline library ],,
	enable_readline="yes")

# ansi lisp
AC_ARG_ENABLE(ansi,[--enable-ansi builds a large gcl aiming for ansi compliance, 
		    --disable-ansi builds the smaller traditional CLtL1 image],,enable_ansi="no")

if test "$enable_ansi" = "yes" ; then
	SYSTEM=ansi_gcl
	AC_DEFINE(ANSI_COMMON_LISP)
	CLSTANDARD=ANSI
else
	SYSTEM=gcl
	CLSTANDARD=CLtL1
fi

FLISP="saved_$SYSTEM"
AC_SUBST(FLISP)
AC_SUBST(SYSTEM)
AC_SUBST(CLSTANDARD)

# pagewidth
AC_MSG_CHECKING(for pagewidth)
AC_TRY_RUN([#include <stdio.h>
            #include <unistd.h>
int main() {size_t i=getpagesize(),j; 
	    FILE *fp=fopen("conftest1","w");
	    for (j=0;i>>=1;j++);
	    fprintf(fp,"%u",j);
	    return 0;}],PAGEWIDTH=`cat conftest1`,PAGEWIDTH=0,PAGEWIDTH=0)
AC_MSG_RESULT($PAGEWIDTH)
AC_DEFINE_UNQUOTED(PAGEWIDTH,$PAGEWIDTH)
AC_SUBST(PAGEWIDTH)

# Maximum number of pages



# Check if Posix compliant getcwd exists, if not we'll use getwd.
AC_CHECK_FUNCS(getcwd)
AC_CHECK_FUNCS(getwd)
AC_CHECK_FUNC(uname, , AC_DEFINE(NO_UNAME))
AC_CHECK_FUNC(gettimeofday, , AC_DEFINE(NO_GETTOD))


AC_CHECK_HEADERS(sys/ioctl.h)

# OpenBSD has elf_abi.h instead of elf.h
AC_CHECK_HEADERS(elf.h elf_abi.h)


#--------------------------------------------------------------------
#	The code below deals with several issues related to gettimeofday:
#	1. Some systems don't provide a gettimeofday function at all
#	   (set NO_GETTOD if this is the case).
#	2. SGI systems don't use the BSD form of the gettimeofday function,
#	   but they have a BSDgettimeofday function that can be used instead.
#	3. See if gettimeofday is declared in the <sys/time.h> header file.
#	   if not, set the GETTOD_NOT_DECLARED flag so that tclPort.h can
#	   declare it.
#--------------------------------------------------------------------

AC_CHECK_FUNC([BSDgettimeofday],
              [AC_DEFINE(HAVE_BSDGETTIMEOFDAY)],
	      [AC_CHECK_FUNC([gettimeofday], ,
	                     [AC_DEFINE([NO_GETTOD])])])

AC_MSG_CHECKING([for gettimeofday declaration])

AC_EGREP_HEADER([gettimeofday],
                [sys/time.h],
		[AC_MSG_RESULT([present])],
		[AC_MSG_RESULT([missing])
    		 AC_DEFINE(GETTOD_NOT_DECLARED)])


AC_CHECK_LIB(m,sin,LIBS="${LIBS} -lm",true)
AC_CHECK_LIB(mingwex,main,LIBS="${LIBS} -lmingwex",true)

EXTRA_LOBJS=
if test "$try_japi" = "yes" ; then
   AC_CHECK_HEADERS(japi.h,[AC_DEFINE(HAVE_JAPI_H)
				 EXTRA_LOBJS="${EXTRA_LOBJS} gcl_japi.o"
			 	 LIBS="${LIBS} -ljapi -lwsock32"] )
fi
if test "$use" = "mingw" ; then
 if test "$try_xdr" = "yes" ; then
   AC_CHECK_HEADERS(rpc/rpc.h,[AC_DEFINE(HAVE_XDR)
			 	 LIBS="${LIBS} -loncrpc"] )
 fi
else
 if test "$try_xdr" = "yes" ; then
   AC_CHECK_HEADERS(rpc/rpc.h,[AC_DEFINE(HAVE_XDR)
			 	 LIBS="${LIBS} -lrpc"] )
 fi
fi

# Should really find a way to check for prototypes, but this 
# basically works for now.  CM
#
AC_CHECK_HEADERS(math.h,AC_DEFINE(HAVE_MATH_H))

#
# For DBL_MAX et. al. on (only) certain Linux arches, apparently CM
#
AC_CHECK_HEADERS(values.h,AC_DEFINE(HAVE_VALUES_H))

#
# Sparc solaris keeps this in float.h, rework either/or with values.h later
#
AC_CHECK_HEADERS(float.h,AC_DEFINE(HAVE_FLOAT_H))

#
# The second alternative is for solaris.  This needs to be
# a more comprehensive later, i.e. checking that the fpclass
# test makes sense.  CM
#
AC_MSG_CHECKING([for isnormal])
AC_TRY_RUN([#define _GNU_SOURCE
	    #include <math.h>
	    int main() {
		float f;
		return isnormal(f) || !isnormal(f) ? 0 : 1;
		}],
		AC_DEFINE(HAVE_ISNORMAL) AC_MSG_RESULT(yes),
		AC_MSG_CHECKING([for fpclass in ieeefp.h])
		AC_TRY_RUN([#include <ieeefp.h>
			    int main() {
				float f;
				return fpclass(f)>=FP_NZERO || fpclass(f)<FP_NZERO ? 0 : 1;
				}],
		AC_DEFINE(HAVE_IEEEFP) AC_MSG_RESULT(yes),
		HAVE_IEEEFP=0 AC_MSG_RESULT(no),HAVE_IEEEFP=0 AC_MSG_RESULT(no))
	,HAVE_ISNORMAL=0 AC_MSG_RESULT(no),HAVE_ISNORMAL=0 AC_MSG_RESULT(no))

AC_MSG_CHECKING([for isfinite])
AC_TRY_RUN([#define _GNU_SOURCE
	    #include <math.h>
	    int main() {
		float f;
		return isfinite(f) || !isfinite(f) ? 0 : 1;
		}],
		AC_DEFINE(HAVE_ISFINITE) AC_MSG_RESULT(yes),
		AC_MSG_CHECKING([for finite()])
		AC_TRY_RUN([#include <math.h>
	                    #include <ieeefp.h>
			    int main() {
				float f;
				return finite(f) || !finite(f) ? 0 : 1;
				}],
		AC_DEFINE(HAVE_FINITE) AC_MSG_RESULT(yes),
		HAVE_FINITE=0 AC_MSG_RESULT(no),HAVE_FINITE=0 AC_MSG_RESULT(no))
	,HAVE_ISFINITE=0 AC_MSG_RESULT(no),HAVE_ISFINITE=0 AC_MSG_RESULT(no))



#--------------------------------------------------------------------
#	Check for the existence of the -lsocket and -lnsl libraries.
#	The order here is important, so that they end up in the right
#	order in the command line generated by make.  Here are some
#	special considerations:
#	1. Use "connect" and "accept" to check for -lsocket, and
#	   "gethostbyname" to check for -lnsl.
#	2. Use each function name only once:  can't redo a check because
#	   autoconf caches the results of the last check and won't redo it.
#	3. Use -lnsl and -lsocket only if they supply procedures that
#	   aren't already present in the normal libraries.  This is because
#	   IRIX 5.2 has libraries, but they aren't needed and they're
#	   bogus:  they goof up name resolution if used.
#	4. On some SVR4 systems, can't use -lsocket without -lnsl too.
#	   To get around this problem, check for both libraries together
#	   if -lsocket doesn't work by itself.
#--------------------------------------------------------------------
AC_MSG_CHECKING([for sockets])
tcl_checkBoth=0
AC_CHECK_FUNC(connect, tcl_checkSocket=0, tcl_checkSocket=1)
if test "$tcl_checkSocket" = 1; then
    AC_CHECK_LIB(socket, main, TLIBS="$TLIBS -lsocket", tcl_checkBoth=1)
fi


if test "$tcl_checkBoth" = 1; then
    tk_oldLibs=$TLIBS
    TLIBS="$TLIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, tcl_checkNsl=0, [TLIBS=$tk_oldLibs])
fi
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [TLIBS="$TLIBS -lnsl"]))

RL_OBJS=""
RL_LIB=""
if test "$enable_readline" = "yes" ; then
	AC_CHECK_HEADERS(readline/readline.h,
		AC_CHECK_LIB(readline,main,
			AC_DEFINE(HAVE_READLINE) 
			TLIBS="$TLIBS -lreadline -lncurses"
			RL_OBJS=gcl_readline.o
# Readline support now initialized automatically when compiled in, this lisp
# object no longer needed -- 20040102 CM
#			RL_LIB=lsp/gcl_readline.o
			,,-lncurses))

# These tests discover differences between readline 4.1 and 4.3
	AC_CHECK_LIB(readline,rl_completion_matches,
			AC_DEFINE(HAVE_DECL_RL_COMPLETION_MATCHES)
			AC_DEFINE(HAVE_RL_COMPENTRY_FUNC_T),,)
fi

AC_SUBST(RL_OBJS)
AC_SUBST(RL_LIB)

AC_MSG_CHECKING(For network code for nsocket.c)
AC_TRY_LINK([
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>

/************* for the sockets ******************/ 
#include <sys/socket.h>		/* struct sockaddr, SOCK_STREAM, ... */
#ifndef NO_UNAME
#   include <sys/utsname.h>	/* uname system call. */
#endif
#include <netinet/in.h>		/* struct in_addr, struct sockaddr_in */
#include <arpa/inet.h>		/* inet_ntoa() */
#include <netdb.h>		/* gethostbyname() */
],[ connect(0,(struct sockaddr *)0,0);
    gethostbyname("jil");
    socket(AF_INET, SOCK_STREAM, 0);
	],
[AC_DEFINE(HAVE_NSOCKET)
 AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))


AC_MSG_CHECKING(check for listen using fcntl)
AC_TRY_COMPILE([#include <stdio.h>
#include <fcntl.h>
],
[FILE *fp=fopen("configure.in","r");
  int orig;
  orig = fcntl(fileno(fp), F_GETFL);
  if (! (orig & O_NONBLOCK )) return 0;
],
[AC_DEFINE(LISTEN_USE_FCNTL)
 AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))




AC_CHECK_FUNC(profil, ,[AC_DEFINE(NO_PROFILE)])
AC_SUBST(NO_PROFILE)
AC_CHECK_FUNC(setenv,[AC_DEFINE(HAVE_SETENV)],no_setenv=1 )
AC_SUBST(HAVE_SETENV)
if test "$no_setenv" = "1" ; then
AC_CHECK_FUNC(putenv,[AC_DEFINE(HAVE_PUTENV)],)
AC_SUBST(HAVE_PUTENV)
fi

AC_CHECK_FUNC(_cleanup, [AC_DEFINE(USE_CLEANUP)],)
AC_SUBST(USE_CLEANUP)
gcl_ok=no

AC_HEADER_EGREP(LITTLE_ENDIAN, ctype.h, gcl_ok=yes, gcl_ok=noo)
if test $gcl_ok = yes ; then
AC_DEFINE(ENDIAN_ALREADY_DEFINED)
fi

AC_SUBST(ENDIAN_ALREADY_DEFINED)




# if test "x$enable_machine" = "x" ; then
AC_MSG_CHECKING([FIONBIO vs. O_NONBLOCK for nonblocking I/O])

case $system in
       OSF*)
	AC_DEFINE(USE_FIONBIO)
	AC_MSG_RESULT(FIONBIO)
	;;
    SunOS-4*)
	AC_DEFINE(USE_FIONBIO)
	AC_MSG_RESULT(FIONBIO)
	;;
    ULTRIX-4.*)
	AC_DEFINE(USE_FIONBIO)
	AC_MSG_RESULT(FIONBIO)
	;;
    *)
	AC_MSG_RESULT(O_NONBLOCK)
	;;
esac


AC_MSG_CHECKING(check for SV_ONSTACK)
AC_TRY_COMPILE([#include <signal.h>
int joe=SV_ONSTACK;
],
[],
[AC_DEFINE(HAVE_SV_ONSTACK)
 AC_SUBST(HAVE_SV_ONSTACK)
 AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))

AC_MSG_CHECKING(check for SIGSYS)
AC_TRY_COMPILE([#include <signal.h>
int joe=SIGSYS;
],
[],
[AC_DEFINE(HAVE_SIGSYS)
 AC_SUBST(HAVE_SIGSYS)
 AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))


AC_MSG_CHECKING(check for SIGEMT)
AC_TRY_COMPILE([#include <signal.h>
int joe=SIGEMT;
],
[],
[AC_DEFINE(HAVE_SIGEMT)
 AC_SUBST(HAVE_SIGEMT)
 AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no))




#if test $use = "386-linux" ; then
	AC_CHECK_HEADERS(asm/sigcontext.h)
	AC_CHECK_HEADERS(asm/signal.h)
	AC_MSG_CHECKING([for sigcontext...])
        AC_TRY_COMPILE([#include <signal.h>
       ],
       [
        struct sigcontext foo;
       ],
       [
        sigcontext_works=1;
        AC_DEFINE(SIGNAL_H_HAS_SIGCONTEXT)
	AC_MSG_RESULT(sigcontext in signal.h)
       ],
        
	[sigcontext_works=0;
         AC_MSG_RESULT(sigcontext NOT in signal.h)]
       )
       if test "$sigcontext_works" = 0 ; then
       AC_MSG_CHECKING([for sigcontext...])
       AC_TRY_COMPILE([#include <signal.h>
             #ifdef HAVE_ASM_SIGCONTEXT_H     
             #include <asm/sigcontext.h>
             #endif
             #ifdef HAVE_ASM_SIGNAL_H          
             #include <asm/signal.h>
             #endif
          ],
        [ 
        struct sigcontext foo;
        ],
        [
        AC_DEFINE(HAVE_SIGCONTEXT)
	AC_MSG_RESULT(sigcontext in asm files)
        ],
        [
        AC_MSG_RESULT(no sigcontext found)
     ])


       fi
#	echo 'foo() {}' > conftest1.c
#	$CC -S conftest1.c
#	use_underscore=0	
#	if fgrep _foo conftest1.s ; then use_underscore=1 ; fi
#	if test $use_underscore = 0 ; then
#            MPI_FILE=mpi-386_no_under.o
#          else
#	     MPI_FILE=mpi-386d.o
#        fi
#	AC_SUBST(MPI_FILE)
#       GCC=$CC
#        if test -x  /usr/bin/i386-glibc20-linux-gcc ; then
#          GCC=/usr/bin/i386-glibc20-linux-gcc
#       fi
#	AC_SUBST(GCC)

#fi

AC_PATH_PROG(EMACS,emacs)


# check for where the emacs site lisp directory is.
rm -f conftest.el
cat >> conftest.el <<EOF
(let ((ans ".") (tem load-path) cur)
 (while (setq cur (car tem))
   (setq tem (cdr tem))
   (cond ((and (string-match "site-lisp/?$" cur) (file-directory-p cur))
          (setq ans cur)
           (if (string-match "-[0-9.]+$" cur) nil
               (setq tem nil)))))
  (message ans))
EOF

AC_MSG_CHECKING([emacs site lisp directory])
if [[ "$EMACS_SITE_LISP" = "unknown" ]] ; then
	if [[ "$EMACS" != "" ]] ; then
		EMACS_SITE_LISP=`$EMACS -q -batch --no-site-file -l conftest.el 2>&1 | sed -e /Loading/d | sed -e /load/d `
	else
		EMACS_SITE_LISP=""
	fi
fi
AC_MSG_RESULT($EMACS_SITE_LISP)
AC_SUBST(EMACS_SITE_LISP)

# check for where the emacs site lisp default.el is
rm -f conftest.el
cat >> conftest.el <<EOF
(let ((ans "./default.el") file (tem load-path))
 (while tem
   (cond ((file-exists-p
           (setq file (expand-file-name "/default.el" (car tem))))
	  (setq tem nil) (setq ans file)))
   (setq tem (cdr tem)))
  (message ans))
EOF

AC_MSG_CHECKING([emacs default.el])
if [[ "$EMACS" != "" ]] ; then
	EMACS_DEFAULT_EL=`$EMACS -q -batch --no-site-file -l conftest.el 2>&1 | sed -e /Loading/d | sed -e /load/d `
else
	EMACS_DEFAULT_EL=""
fi
if  test -f "${EMACS_DEFAULT_EL}"  ; then true;else
  if test -d $EMACS_SITE_LISP ; then
     EMACS_DEFAULT_EL=${EMACS_SITE_LISP}/default.el
  fi
fi
AC_MSG_RESULT($EMACS_DEFAULT_EL)
AC_SUBST(EMACS_DEFAULT_EL)



# check for where the emacs site lisp info/dir is
rm -f conftest.el
cat >> conftest.el <<EOF
(let ((ans "") file (tem Info-default-directory-list))
 (while tem
   (cond ((file-exists-p (setq file (expand-file-name "dir" (car tem))))
          (setq ans (file-name-as-directory (car tem)))		
	  (setq tem nil) ))
   (setq tem (cdr tem)))
  (message ans))
EOF

AC_MSG_CHECKING([emacs info/dir])
if test "$use" = "mingw" ; then
    INFO_DIR=\$\(prefix\)/lib/gcl-$VERSION/info/
else
    if [[ "$EMACS" != "" ]] && [[ "$INFO_DIR" = "unknown" ]] ; then
	INFO_DIR=`$EMACS -q -batch --no-site-file -l conftest.el 2>&1 | sed -e /Loading/d | sed -e /load/d `
    fi
fi
if  test -f "${INFO_DIR}dir"  ; then true;else
if test -f /usr/share/info/dir ; then
  INFO_DIR=/usr/share/info/
else true;
fi
fi
AC_MSG_RESULT($INFO_DIR)
AC_SUBST(INFO_DIR)

AC_MSG_CHECKING([for tcl/tk])


if test -d "${TCL_CONFIG_PREFIX}"  ; then true ; else
rm -f conftest.tcl
cat >> conftest.tcl <<EOF
[
set dir [set tcl_library]
if { [file exists [file join [set dir] tclConfig.sh]] } {
      puts  [set dir]
      exit	
  }
set dir [file dirname [set tcl_library]]
if { [file exists [file join [set dir] tclConfig.sh]] } {
      puts  [set dir]
  }
]
EOF
#cp conftest.tcl foo.tcl

AC_CHECK_PROG(TCLSH,tclsh,tclsh,${TCLSH})
#AC_CHECK_PROG(TCLSH,tclsh8.0,tclsh8.0,${TCLSH})

if test "${TCLSH}" = "" ; then true ; else
TCL_CONFIG_PREFIX=`${TCLSH} < conftest.tcl`
fi
fi
if test -f ${TCL_CONFIG_PREFIX}/tclConfig.sh  ; then . ${TCL_CONFIG_PREFIX}/tclConfig.sh ; fi


if test -d "${TK_CONFIG_PREFIX}"  ; then true ; else
if test -f ${TCL_CONFIG_PREFIX}/tkConfig.sh ; then
TK_CONFIG_PREFIX=${TCL_CONFIG_PREFIX}
else
#rm -f conftest.tcl
#cat >> conftest.tcl <<EOF
#[
#set dir [set tk_library]
#if { [file exists [file join [set dir] tkConfig.sh]] } {
#      puts  [set dir]
#      exit
#  }
#set dir [file dirname [set tk_library]]
#if { [file exists [file join [set dir] tkConfig.sh]] } {
#      puts  [set dir]
#  }
#exit 0
#]
#EOF
#cp conftest.tcl foo.tcl
#AC_CHECK_PROG(TKSH,wish,wish,${TKSH})
##AC_CHECK_PROG(TKSH,wish8.0,wish8.0,${TKSH})
#if test "${TKSH}" = "" ; then true ; else
#TK_CONFIG_PREFIX=`${TKSH} < conftest.tcl`
#fi

if test "${TCLSH}" = "" ; then true ; else
	TK_CONFIG_PREFIX=`echo tcl_findLibrary \"tk\" \"${TCL_VERSION}\" \"\" \"tkConfig.sh\" \"TK_LIBRARY\" tk_library | ${TCLSH} 2>&1 | ${AWK} '/"source / {if (i++) next;sub("/[[^/]]*$","",$2);print $2}'`
fi

fi
fi
#AC_MSG_CHECKING(TK_CONFIG_PREFIX=${TK_CONFIG_PREFIX})
if test -f ${TK_CONFIG_PREFIX}/tkConfig.sh  ; then . ${TK_CONFIG_PREFIX}/tkConfig.sh ; fi

if test -d ${TK_CONFIG_PREFIX}/tk${TK_VERSION} ; then
  TK_LIBRARY=${TK_CONFIG_PREFIX}/tk${TK_VERSION}
  else
  if test -d ${TK_CONFIG_PREFIX}/../tk${TK_VERSION} ; then
    TK_LIBRARY=${TK_CONFIG_PREFIX}/../tk${TK_VERSION}
  fi
fi
if test -d ${TK_CONFIG_PREFIX}/tcl${TCL_VERSION} ; then
  TCL_LIBRARY=${TK_CONFIG_PREFIX}/tcl${TCL_VERSION}
  else
  if test -d ${TK_CONFIG_PREFIX}/../tcl${TCL_VERSION} ; then
    TCL_LIBRARY=${TK_CONFIG_PREFIX}/../tcl${TCL_VERSION}
  fi
fi
if test -f ${TK_CONFIG_PREFIX}/../include/tk.h ; then
  TK_INCLUDE=-I${TK_CONFIG_PREFIX}/../include
  else
  if test -f /usr/include/tcl${TCL_VERSION}/tk.h ; then
    TK_INCLUDE=-I/usr/include/tcl${TCL_VERSION}
  fi	
fi
if test -f ${TCL_CONFIG_PREFIX}/../include/tcl.h ; then
  TCL_INCLUDE=-I${TCL_CONFIG_PREFIX}/../include
  else
  if test -f /usr/include/tcl${TCL_VERSION}/tcl.h ; then
    TCL_INCLUDE=-I/usr/include/tcl${TCL_VERSION}
  fi
fi
AC_CHECK_LIB(lieee,main,have_ieee=1,have_ieee=0)
if test "$have_ieee" = "0" ; then
 TCL_LIBS=`echo ${TCL_LIBS} | sed -e "s:-lieee::g" `
fi
if test "$have_dl" = "0" ; then
 TCL_LIBS=`echo ${TCL_LIBS} | sed -e "s:-ldl::g"`
fi


AC_SUBST(TK_CONFIG_PREFIX)
AC_SUBST(TK_LIBRARY)
AC_SUBST(TCL_LIBRARY)
AC_SUBST(TK_XINCLUDES)
AC_SUBST(TK_INCLUDE)
AC_SUBST(TCL_INCLUDE)
AC_SUBST(TK_LIB_SPEC)
AC_SUBST(TK_BUILD_LIB_SPEC)
AC_SUBST(TK_XLIBSW)
AC_SUBST(TK_XINCLUDES)
AC_SUBST(TCL_LIB_SPEC)
AC_SUBST(TCL_DL_LIBS)
AC_SUBST(TCL_LIBS)






if test -d "${TK_CONFIG_PREFIX}" ; then
AC_MSG_RESULT([using TK_VERSION=${TK_VERSION} in ${TK_CONFIG_PREFIX}])
else
AC_MSG_RESULT([not found])
fi

NOTIFY=$enable_notify
AC_SUBST(NOTIFY)

      


# for sgbc the mprotect capabilities.

# the time handling for unixtime, add timezone

AC_MSG_CHECKING([alloca])
AC_TRY_RUN([int main() { exit(alloca(500) != NULL ? 0 : 1);}],
  ,gcl_ok=yes, gcl_ok=no,gcl_ok=no)
if test $gcl_ok = yes ; then
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_ALLOCA)
else
  AC_TRY_RUN([#include <alloca.h>
  int main() { exit(alloca(500) != NULL ? 0 : 1)}],
  ,gcl_ok=yes, gcl_ok=no,gcl_ok=no)
 if test $gcl_ok = yes ; then
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_ALLOCA)
    AC_DEFINE(NEED_ALLOCA_H)
 fi
fi
if test $gcl_ok = no ; then     AC_MSG_RESULT(no) ; fi










# alloca

# dlopen etc
# idea make it so you do something dlopen(libX.so,RTLD_GLOBAL)
# then dlload("foo.o") a lisp file can refer to things in libX.so
# 

# what machine this is, and include then a machine specific hdr.
# and machine specific defs.

# check bzero,

# check getcwd, getwd etc..




# check socket stuff..

# getrlimit

# fionread or block

# redhat/cygnus released for some reason a buggy version of gcc,
# which no one else released.   Catch that here.
AC_MSG_CHECKING([Checking for buggy gcc version from redhat])
if  2>&1 $CC -v | fgrep "gcc version 2.96" > /dev/null 
   then 
        BROKEN_O4_OPT=1
        AC_DEFINE(BROKEN_O4_OPT)
        AC_SUBST(BROKEN_O4_OPT)
        echo ODIR_DEBUG=-O >> makedefsafter
	echo >> makedefsafter
	AC_MSG_RESULT([yes .. turning off -O4])
   else
	AC_MSG_RESULT([no])
fi 

LIBS="$LDFLAGS $TLDFLAGS $LIBS $TLIBS"
AC_SUBST(LIBS)
FINAL_CFLAGS="$CFLAGS $TCFLAGS $PROCESSOR_FLAGS"
AC_SUBST(FINAL_CFLAGS)
# Work around bug with gcc on ppc -- CM
NIFLAGS="$CFLAGS $TCFLAGS $TONIFLAGS $PROCESSOR_FLAGS -I\$(GCLDIR)/o"
AC_SUBST(NIFLAGS)
CFLAGS="$CFLAGS $TCFLAGS $TO3FLAGS $PROCESSOR_FLAGS -I\$(GCLDIR)/o"
AC_SUBST(CFLAGS)
O3FLAGS=$TO3FLAGS
AC_SUBST(O3FLAGS)
O2FLAGS=$TO2FLAGS
AC_SUBST(O2FLAGS)

AC_SUBST(EXTRA_LOBJS)
AC_SUBST(LEADING_UNDERSCORE)
AC_SUBST(GNU_LD)
if test -f h/$use.defs  ; then

  AC_SUBST(use)
  AC_OUTPUT(makedefc windows/gcl.iss windows/sysdir.bat windows/install.lsp )
  echo makedefc
  cat makedefc

  echo    add-defs1 $use
  CC=$CC ./add-defs1 $use

else
  echo "Unable to guess machine type"
  echo use configure --enable-machine=XXX,  for XXX such that h/XXX.defs exists, ie in h/*.defs
fi
