AC_INIT()
AC_CONFIG_HEADER(h/gclincl.h)
# some parts of this configure script are taken from the tcl configure.in

# 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(AWK,gawk nawk awk,"")

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

# 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_PROG_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
AC_SUBST(GCC)
AC_CHECK_SIZEOF(long *,0)
AC_CHECK_HEADERS(endian.h,
	AC_MSG_CHECKING("endianness")
	AC_TRY_RUN([#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)

AC_MSG_CHECKING("finding DBEGIN")
AC_TRY_RUN([#include <stdio.h>
            #include <stdlib.h>
main()
{
  char *b = (void *) malloc(1000);
  FILE *fp = fopen("conftest1","w");
  fprintf(fp,"0x%lx",((unsigned long) b) & ~(unsigned long)0xffffff);
  fclose(fp);
  return 0;
}],dbegin=`cat conftest1`,dbegin=0,dbegin=0)
AC_DEFINE_UNQUOTED(DBEGIN,$dbegin \
/* where data begins */
)
AC_MSG_RESULT(got $dbegin)


AC_MSG_CHECKING("finding CSTACK_ADDRESS")
AC_TRY_RUN([#include <stdio.h>
main()
{
  char *b ;
  FILE *fp = fopen("conftest1","w");
  fprintf(fp,"%d",((int) &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
	FLISP=saved_ansi_gcl;
else
	FLISP=saved_gcl
fi

AC_SUBST(FLISP)

# 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)

# bfd probe

AC_ARG_ENABLE(bfd,
	[ --disable-bfd prevents gcl from using libbfd.a for fast object loading and symbol table lookups ] ,,
	enable_bfd="yes")


# Maximum number of pages

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(vssize,[ --enable-vssize=XXXX will compile in a value stack of size XXX] ,
[AC_DEFINE_UNQUOTED(VSSIZE,$enable_vssize)]
)

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"])

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"])




# 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)


#--------------------------------------------------------------------
#	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)

# Should really find a way to check for prototypes, but this 
# basically works for now.  CM
#
AC_CHECK_HEADERS(math.h,AC_DEFINE(NEED_MATH_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>
			    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, LIBS="$LIBS -lsocket", tcl_checkBoth=1)
fi


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

if test "$enable_readline" = "yes" ; then
	AC_CHECK_LIB([readline],
		     [main],
		     [AC_DEFINE(HAVE_READLINE)
	              LIBS="$LIBS -lreadline -lncurses"
	              RL_OBJS=readline.o
	              RL_LIB=lsp/readline.o],,
		     [-lncurses])
fi

if test "$enable_bfd" = "yes" ; then
	AC_CHECK_HEADER(bfd.h,
		AC_CHECK_LIB(bfd,bfd_init,
			if $CC -v 2>&1 | fgrep ming > /dev/null ; then
				BFDLIB="-lbfd"
				IBRLIB="-liberty"
			else
				echo 'int main() {bfd_init();bfd_openr("/dev/null",0);return 0;}' >foo.c
				MP=`$GCC [[ -Wl,-M ]] -static -o foo foo.c -lbfd -liberty 2>&1 | tr '()' '\012\012' | $AWK '{print $NF}' | sort | uniq`
				rm -f foo.c foo
				BFDLIB=`echo $MP | tr ' ' '\012' | grep libbfd.a`
				IBRLIB=`echo $MP | tr ' ' '\012' | grep libiberty.a`
			fi
#
# 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_DEFINE(HAVE_LIBBFD)
				LIBS="$LIBS  $BFDLIB $IBRLIB",
				AC_TRY_RUN([#define IN_GCC
					    #include <bfd.h>
                                            #define CONST const
					    int main() {symbol_info t; return 0;}],
					AC_MSG_RESULT(yes) 
					AC_DEFINE(NEED_CONST)
					AC_DEFINE(HAVE_LIBBFD)
					LIBS="$LIBS  $BFDLIB $IBRLIB",
					AC_MSG_RESULT(cannot use bfd),AC_MSG_RESULT(cannot use bfd)),
				AC_MSG_RESULT(cannot use bfd))
					
			,,-liberty))
fi

AC_SUBST(LIBS)
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_CANONICAL_HOST
canonical=$host
## host=CPU-COMPANY-SYSTEM
AC_MSG_RESULT(host=$host)

use=unknown

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

     *86-*-linux*)
     use=386-linux; ln -snf linux.defs h/$use.defs;;

     m68k-*-linux*)
     use=m68k-linux; ln -snf linux.defs h/$use.defs;;

     alpha*-*-linux*)
     use=alpha-linux; ln -snf linux.defs h/$use.defs;;

     mips-*-linux*)
     use=mips-linux; ln -snf linux.defs h/$use.defs;;

     mipsel-*-linux*)
     use=mipsel-linux; ln -snf linux.defs h/$use.defs;;

     sparc*-*-linux*)
     use=sparc-linux; ln -snf linux.defs h/$use.defs;;

     arm*-*-linux*)
     use=arm-linux; ln -snf linux.defs h/$use.defs;;

     s390-*-linux*)
     use=s390-linux; ln -snf linux.defs h/$use.defs;;

     ia64-*-linux*)
     use=ia64-linux; ln -snf linux.defs h/$use.defs;;

     hppa-*-linux*)
     use=hppa-linux; ln -snf linux.defs h/$use.defs;;

     powerpc-*-linux*)
     use=powerpc-linux; ln -snf linux.defs h/$use.defs;;

     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;;

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

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

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_TRY_COMPILE([#include <signal.h>
                        long code;  
       ],
       [
        void *p = ((void *)(((struct sigcontext_struct *)(&code))));
       ],
       [
        sigcontext_struct_works=1;
        AC_DEFINE(SIGNAL_H_HAS_SIGCONTEXT)
	AC_MSG_RESULT("sigcontext in signal.h")
       ],
        
	[sigcontext_struct_works=0;
         AC_MSG_RESULT("sigcontext NOT in signal.h")]
       )
       if test "$sigcontext_struct_works" = 0 ; then
       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
             long code;
          ],
        [ 
        void *p = ((void *)(((struct sigcontext *)(&code))));
        ],
        [
        sigcontext_works=1 ;
        AC_DEFINE(HAVE_SIGCONTEXT)
	AC_MSG_RESULT("use struct sigcontext")
        ],
        [
        sigcontext_works=0 ; 
     ])


       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])
#EMACS_SITE_LISP=unknown
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
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 [[ "$EMACS" != "" ]] ; then
	INFO_DIR=`$EMACS -q -batch --no-site-file -l conftest.el 2>&1 | sed -e /Loading/d | sed -e /load/d `
fi
if  test -f "${INFO_DIR}dir"  ; then true;else
if test -f /usr/info/dir ; then
  INFO_DIR=/usr/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
AC_CHECK_LIB(dl,dlopen,have_dl=1,have_dl=0)
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)





echo enable_machine=$enable_machine
if test "x$enable_machine" != "x" ; then
  use=$enable_machine
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)	

      


# 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

rm -f makedefsafter

MP_INLCUDE=""
if test $use_gmp = yes ; then
 AC_MSG_CHECKING([use_gmp=yes, doing configure in gmp directory])
 case "${canonical}" in
#   i[[5-9]]86* | pentium* | k6* | athlon*)
#      (cd gmp ; ./configure --target=i486) ;;
   *)
      (cd gmp ; ./configure) ;;
 esac
 [[ "`ls -1 gmp/mpn/add_n.* 2>/dev/null`" != "" ]] || cp gmp/mpn/generic/*.c gmp/mpn/
 AC_MSG_CHECKING("for size of gmp limbs")
 AC_TRY_RUN([#include <stdio.h>
	#include "h/gmp.h"
	
	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) 
 GMP=1
 AC_DEFINE(GMP)
 AC_SUBST(GMP)
 MP_INCLUDE=h/gmp.h
 echo > makedefsafter
 echo 'MPFILES=${GMP_DIR}libgmp.a' >> makedefsafter
 echo >> makedefsafter
fi
AC_SUBST(MP_INCLUDE)
 
# 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 


if test -f h/$use.defs  ; then

  AC_SUBST(use)
  AC_OUTPUT(makedefc)
  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
