dnl Process this file with autoconf 2.x to produce a configure script.

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
dnl Autoconf does not seems to fit the needs of dynamic linking well -bri

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
dnl We might want AC_CHAR_UNSIGNED in the future.

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
dnl The -cckr (K&R) flag is for the IRIX C compiler.  If this is left
dnl out, scheme48vm.c breaks because the rather pedantic SGI compiler
dnl decides that a char is not the same thing as an unsigned char.
dnl - Bryan O'Sullivan 3/94

AC_DEFUN(S48_CFLAG_CKR, [
  AC_MSG_CHECKING(${CC} for -cckr)
  AC_CACHE_VAL(s48_cv_cflag_ckr,[
    if test ${CC} != gcc; then
      echo "main(){int i=1;}" > conftest.c
      if CFLAGS="-cckr ${CFLAGS}" eval $ac_compile
      then
        s48_cv_cflag_ckr=yes
      else
        s48_cv_cflag_ckr=no
      fi
    else
      s48_cv_cflag_ckr=no
    fi
    rm -f conftest*])
  AC_MSG_RESULT($s48_cv_cflag_ckr)
  if test $s48_cv_cflag_ckr = yes; then
    CFLAGS=$CFLAGS
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(S48_POSIX_LIBC, [
  AC_MSG_CHECKING(for RISC/OS POSIX library lossage)
  AC_CACHE_VAL(s48_cv_posix_libc,[
    if test -f /usr/posix/usr/lib/libc.a; then
      s48_cv_posix_libc=yes
    else
      s48_cv_posix_libc=no
    fi])
  AC_MSG_RESULT($s48_cv_posix_libc)
  if test $s48_cv_posix_libc = yes; then
    LIBS="${LIBS} /usr/posix/usr/lib/libc.a"
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(S48_CFLAG_O2, [
  AC_MSG_CHECKING(whether we can use -O2)
  AC_CACHE_VAL(s48_cv_cflag_o2,[
    if test "z$CFLAGS" = "z"; then 
      if test "z$GCC" = "z"; then 
        s48_cv_cflag_o2=yes
      else 
        s48_cv_cflag_o2=no
      fi
    else
      s48_cv_cflag_o2=preset
    fi])
  AC_MSG_RESULT($s48_cv_cflag_o2)
  case "$s48_cv_cflag_o2" in
    yes )
      CFLAGS="-O"
    ;;
    no )
      CFLAGS="-O2 -g"
    ;;
  esac
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(S48_USCORE, [
  AC_MSG_CHECKING(for underscore before symbols)
  AC_CACHE_VAL(s48_cv_uscore,[
    echo "main(){int i=1;}
    fnord(){int i=23; int ltuae=42;}" > conftest.c
    ${CC} -o a.out conftest.c > /dev/null
    if (nm a.out | grep _fnord) > /dev/null; then
      s48_cv_uscore=yes
    else
      s48_cv_uscore=no
    fi])
  AC_MSG_RESULT($s48_cv_uscore)
  rm -f conftest.c a.out

  if test $s48_cv_uscore = yes; then
    AC_DEFINE(USCORE)

    if test $ac_cv_func_dlopen = yes ; then
	AC_MSG_CHECKING(whether dlsym always adds an underscore for us)
	AC_CACHE_VAL(s48_cv_dlsym_adds_uscore,AC_TRY_RUN( [
#include <dlfcn.h>
#include <stdio.h>
fnord() { int i=42;}
main() { void *self, *ptr1, *ptr2; self=dlopen(NULL,RTLD_LAZY);
    if(self) { ptr1=dlsym(self,"fnord"); ptr2=dlsym(self,"_fnord");
    if(ptr1 && !ptr2) exit(0); } exit(1); } 
], [s48_cv_dlsym_adds_uscore=yes
	AC_DEFINE(DLSYM_ADDS_USCORE) ], s48_cv_dlsym_adds_uscore=no))

        AC_MSG_RESULT($s48_cv_dlsym_adds_uscore)
    fi
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(S48_NLIST,[
  AC_MSG_CHECKING(for n_name)
  AC_CACHE_VAL(s48_cv_nlist,[
    AC_TRY_COMPILE([#include <nlist.h>], 
                   [struct nlist name_list;
                    name_list.n_name = "foo";],
                   s48_cv_nlist=yes,
                   s48_cv_nlist=no)])
  AC_MSG_RESULT($s48_cv_nlist)
  if test $s48_cv_nlist = yes; then
    AC_DEFINE(NLIST_HAS_N_NAME)
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(S48__CNT,[
  AC_MSG_CHECKING(for _cnt)
  AC_CACHE_VAL(s48_cv__cnt,[
    AC_TRY_COMPILE([#include <stdio.h>], 
                   [return stdin->_cnt;],
                   s48_cv__cnt=yes,
                   s48_cv__cnt=no)])
  AC_MSG_RESULT($s48_cv__cnt)
  if test $s48_cv__cnt = yes; then
    AC_DEFINE(FILE_HAS__CNT)
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(SCSH_TZNAME,[
  AC_MSG_CHECKING(for tzname)
  AC_CACHE_VAL(scsh_cv_tzname,[
    AC_TRY_COMPILE([#include <time.h>], 
		   [return (int) tzname;],
                   scsh_cv_tzname=yes,
                   scsh_cv_tzname=no)])
  AC_MSG_RESULT($scsh_cv_tzname)
  if test $scsh_cv_tzname = yes; then
    AC_DEFINE(HAVE_TZNAME)
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(SCSH_GMTOFF,[
  AC_MSG_CHECKING(for gmtoff)
  AC_CACHE_VAL(scsh_cv_gmtoff,[
    AC_TRY_COMPILE([#include <time.h>], 
		   [struct tm time;
		    return time.tm_gmtoff;],
                   scsh_cv_gmtoff=yes,
                   scsh_cv_gmtoff=no)])
  AC_MSG_RESULT($scsh_cv_gmtoff)
  if test $scsh_cv_gmtoff = yes; then
    AC_DEFINE(HAVE_GMTOFF)
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(SCSH_ELF, [
  AC_MSG_CHECKING(for ELF)
  AC_CACHE_VAL(scsh_cv_elf,[
    touch conftest.c
    if ${CC} -v -o a.out conftest.c 2>&1 | grep -q __ELF__ ; then
      scsh_cv_elf=yes
    else
      scsh_cv_elf=no
    fi])
  AC_MSG_RESULT($scsh_cv_elf)
  if test $scsh_cv_elf = yes; then
    LDFLAGS=-rdynamic
  fi
  rm -f conftest.c a.out
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(SCSH_LINUX_STATIC_DEBUG, [
  case "$host" in
    *-*-linux* )
    AC_MSG_CHECKING(for broken Linux that needs -static with -g)
    AC_CACHE_VAL(scsh_cv_linux_static_debug,[
      AC_TRY_LINK([],
  		  [],
  		  scsh_cv_linux_static_debug=no,
  		  scsh_cv_linux_static_debug=yes)])
    AC_MSG_RESULT($scsh_cv_linux_static_debug)
    if test $scsh_cv_linux_static_debug = yes; then
      LDFLAGS="-static ${LDFLAGS}"
    fi
  ;;
  esac
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_DEFUN(SCSH_CONST_SYS_ERRLIST,[
  AC_MSG_CHECKING(for const sys_errlist)
  AC_CACHE_VAL(scsh_cv_const_sys_errlist,[
    AC_TRY_COMPILE([#include <errno.h>
                    #include <unistd.h>], 
		   [const extern char *sys_errlist[];],
                   scsh_cv_const_sys_errlist=yes,
                   scsh_cv_const_sys_errlist=no)])
  AC_MSG_RESULT($scsh_cv_const_sys_errlist)
  if test $scsh_cv_const_sys_errlist = yes; then
    AC_DEFINE(HAVE_CONST_SYS_ERRLIST)
  fi
])

dnl -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AC_INIT(scheme48vm.c)
AC_CONFIG_HEADER(sysdep.h)

AC_CANONICAL_HOST

AC_PROG_CC
AC_ISC_POSIX
SCSH_LINUX_STATIC_DEBUG
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_C_CONST

AC_C_BIGENDIAN
if test $ac_cv_c_bigendian = no ; then
  ENDIAN=little
else
  ENDIAN=big
fi

AR=${AR-"ar cq"}
TMPDIR=${TMPDIR-"/usr/tmp"}

case "$host" in

  ## CX/UX
  m88k-harris-cxux* )
    dir=cxux
    CC="cc -Xa"
    CFLAGS="-O"
    LDFLAGS="-O -Wl,-Bexport"
    AC_DEFINE(HAVE_HARRIS)
  ;;
    
  ## DEC Ultrix
  mips-dec-ultrix* )
    dir=ultrix
    if test ${CC} = cc; then
      LDFLAGS=-N
    fi
  ;;
  
  ## HP 9000 series 700 and 800, running HP/UX
  hppa*-hp-hpux* )
    dir=hpux
    LDFLAGS="-Wl,-E"
    if test ${CC} = cc; then
      CFLAGS="-Ae -O +Obb1800"
      AC_DEFINE(_HPUX_SOURCE)
      AC_DEFINE(hpux)
    fi
  ;;
  
  ## IBM AIX
  rs6000-ibm-aix*|powerpc-ibm-aix* )
    dir=aix
    LDFLAGS="-O"
    if test ${CC} = gcc; then
      LDFLAGS_AIX="-Xlinker -bexport:exportlist.aix"
    else
      LDFLAGS_AIX="-bexport:exportlist.aix"
    fi
    CFLAGS="-O"
    AIX_P="exportlist.aix"
  ;;
  
  ## Linux
  *-*-linux* )
    dir=linux
    # gross, but needed for some older a.out systems for 0.4.x
    LIBS=-lc
    SCSH_ELF
  ;;
  
  ## NetBSD and FreeBSD ( and maybe 386BSD also)
  *-*-*bsd* )
    dir=bsd
    SCSH_ELF
  ;;
  
  ## NeXT
  *-next-* )
    dir=next
    CC="$CC -posix"
    AC_DEFINE(HAVE_SIGACTION)
  ;;
  
  ## SGI IRIX
  mips-sgi-irix* )
    dir=irix
    S48_CFLAG_CKR
    INSTALL='$(srcdir)/install-sh'
  ;;
  
  ## SunOS
  sparc*-sun-sunos* )
   dir=sunos
  ;;
  
  ## Solaris - Sparc and i386
  *-*-solaris* )
    dir=solaris
    AC_DEFINE(HAVE_NLIST)
  ;;
  
  ## NT - cygwin32
  *-*-cygwin32* )
    dir=cygwin32
    EXEEXT=".exe"
  ;;
  
  ## Generic Configuration
  * )
    dir=generic
    echo "WARNING: "
    echo "WARNING: Using generic configuration."
    echo "WARNING: See doc/install.txt for more information."
    echo "WARNING: "
  ;;
esac

rm -f $srcdir/scsh/machine
ln -s $srcdir/$dir $srcdir/scsh/machine

AC_CHECK_LIB(m, sqrt)
AC_CHECK_LIB(gen, pathfind)
AC_CHECK_LIB(nsl, gethostbyaddr)
AC_CHECK_LIB(socket, socket)
dnl Solaris 2.3 seems to need -lelf for nlist().  (tnx Bryan O'Sullivan)
AC_CHECK_LIB(elf, nlist)
AC_CHECK_LIB(ld, ldopen)
AC_CHECK_LIB(dl, dlopen)
AC_CHECK_LIB(dld, shl_findsym)
dnl	AC_CHECK_LIB(mld, main)

AC_RETSIGTYPE
AC_HAVE_HEADERS(libgen.h sys/timeb.h posix/time.h sys/select.h nlist.h)
AC_HAVE_HEADERS(sys/un.h)
AC_HAVE_FUNCS(strerror)
AC_HAVE_FUNCS(dlopen gettimeofday ftime nlist select setitimer sigaction)
AC_HAVE_FUNCS(socket chroot)

S48_POSIX_LIBC
S48_CFLAG_O2
S48_USCORE
S48_NLIST
S48__CNT

SCSH_TZNAME
SCSH_GMTOFF
SCSH_CONST_SYS_ERRLIST

CFLAGS1=${CFLAGS}

AC_SUBST(AIX_P)
AC_SUBST(AR)
AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(CFLAGS1)
AC_SUBST(EXEEXT)
AC_SUBST(ENDIAN)
AC_SUBST(LDFLAGS)
AC_SUBST(LDFLAGS_AIX)
AC_SUBST(LIBS)
AC_SUBST(TMPDIR)

AC_OUTPUT(Makefile scsh/regexp/Makefile scsh/endian.scm scsh/static.scm)
chmod +x scsh/static.scm
