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

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

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


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


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

AC_SUBST(LIBS)

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("jim","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(_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;;

     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*cygwin32)
     use=gnuwin95;;

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


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))->cr2));
       ],
       [
        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))->cr2));
        ],
        [
        sigcontext_works=1 ;
        AC_DEFINE(HAVE_SIGCONTEXT)
	AC_MSG_RESULT("use struct sigcontext")
        ],
        [
        sigcontext_works=0 ; 
     ])


       fi
	echo 'foo() {}' > conftest1.c
	gcc -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=gcc
#        if test -x  /usr/bin/i386-glibc20-linux-gcc ; then
#          GCC=/usr/bin/i386-glibc20-linux-gcc
#       fi
	AC_SUBST(GCC)

fi

# 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
EMACS_SITE_LISP=`emacs -q -batch -l conftest.el 2>&1 | sed -e /Loading/d `
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])
EMACS_DEFAULT_EL=`emacs -q -batch -l conftest.el 2>&1 | sed -e /Loading/d `
if  test -f "${EMACS_DEFAULT_EL}"  ; then true;else
  EMACS_DEFAULT_EL=${EMACS_SITE_LISP}/default.el
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])
INFO_DIR=`emacs -q -batch -l conftest.el 2>&1 | sed -e /Loading/d `
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 [file dirname [set tcl_library]]
if { [file exists [file join [set dir] tclConfig.sh]] } {
      puts  [set dir]
  }
]
EOF
cp conftest.tcl foo.tcl
TCL_CONFIG_PREFIX=`tclsh8.0 conftest.tcl`
fi


if test -d "${TK_CONFIG_PREFIX}"  ; then true ; else
rm -f conftest.tcl
cat >> conftest.tcl <<EOF
[
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
TK_CONFIG_PREFIX=`wish8.0 -f conftest.tcl`
fi

#AC_MSG_CHECKING(TK_CONFIG_PREFIX=${TK_CONFIG_PREFIX})
if test -f ${TCL_CONFIG_PREFIX}/tclConfig.sh  ; then . ${TCL_CONFIG_PREFIX}/tclConfig.sh ; fi
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}
fi
if test -d ${TK_CONFIG_PREFIX}/tcl${TCL_VERSION} ; then
  TCL_LIBRARY=${TK_CONFIG_PREFIX}/tcl${TCL_VERSION}
fi
if test -f ${TK_CONFIG_PREFIX}/../include/tk.h ; then
  TK_INCLUDE=-I${TK_CONFIG_PREFIX}/../include
fi
if test -f ${TCL_CONFIG_PREFIX}/../include/tcl.h ; then
  TCL_INCLUDE=-I${TCL_CONFIG_PREFIX}/../include
fi
AC_CHECK_LIB(lieee,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,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

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

  AC_SUBST(use)
  AC_OUTPUT(makedefs)
  echo makedefs
  cat makedefs

  echo    add-defs1 $use
  sh 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








