#!/bin/sh

ID='$Id: configure,v 1.177 2012/01/20 04:11:57 phil Exp $'

# Create config.m4 and config.h for SNOBOL4 compilation
#
# Phil Budne
# August 24, 1996

OPTIONS="$*"

# needed in Makefile2 and config.h
VERSION=1.4.1
VERSION_DATE="January 19, 2012"

if [ ! "$CC" ]; then
    # on most system gcc is preferred if it can be found
    WANTGCC=true
fi

# largefile support OK
LARGEFILE_OK=true

# inline by default
INLINE_OK=true

# debug this script
DEBUG=

PREFIX=/usr/local

OS=`(uname -s || echo unknown) 2>/dev/null`
OSREL=`(uname -r || echo unknown) 2>/dev/null`
OSVERS=`(uname -v || echo unknown) 2>/dev/null`
MACHINE=`(uname -m || echo unknown) 2>/dev/null`
# Linux/GNU:
INST=`(uname -i || uname -m || echo unknown) 2>/dev/null`

case "$OS" in
MINGW*)
    echo "MinGW detected; run make -f config/win32/mingw.mak"
    exit 1
    ;;
esac

DATE=`date`

# run hostname from subshell to redirect shell "not found message"
HOST=`(hostname) 2>/dev/null || uname -n`

VERSION_H=version.h.tmp
CONFIG_H=config.h.tmp
CONFIG_M4=config.m4.tmp

# DANGER: It's no longer safe to try to grep include files
# (too many systems move them around), use sparingly!!
INCDIR=/usr/include

rm -f $CONFIG_H $CONFIG_M4 $VERSION_H
trap "rm -f $CONFIG_H $CONFIG_M4; exit 1" 1 2 15

echo "# config generated "$DATE			>> $CONFIG_M4
echo "# on $HOST ($OS)"				>> $CONFIG_M4
echo "# by $ID"					>> $CONFIG_M4
if [ "x$OPTIONS" != x ]; then
   echo "# options: $OPTIONS"			>> $CONFIG_M4
fi
echo 'undefine([include])'			>> $CONFIG_M4

################
# most defines now in config.h so seperately compiled
# (LOAD()able) files can get the config information!!

echo 'ADD_CPPFLAGS([-DHAVE_CONFIG_H])'		>> $CONFIG_M4
echo '# see config.h for more defines'		>> $CONFIG_M4

echo '/*'					>> $VERSION_H
echo ' * generated '$DATE			>> $VERSION_H
echo ' * on '"$HOST ($OS)"			>> $VERSION_H
echo ' * by '"$ID"				>> $VERSION_H
echo ' */'					>> $VERSION_H
echo ''						>> $VERSION_H
echo '#define VERSION "'$VERSION'"'		>> $VERSION_H
echo '#define VERSION_DATE "'$VERSION_DATE'"'	>> $VERSION_H
echo "VERS=$VERSION"				>> $CONFIG_M4

echo '/*'					>> $CONFIG_H
echo ' * generated '$DATE			>> $CONFIG_H
echo ' * on '"$HOST ($OS)"			>> $CONFIG_H
echo ' * by '"$ID"				>> $CONFIG_H
echo ' */'					>> $CONFIG_H
echo ''						>> $CONFIG_H

if [ "x$OPTIONS" != x ]; then
    echo '#define CONFIG_OPTIONS "'"$OPTIONS"'"' >> $CONFIG_H
fi
if [ "x$HOST" != x ]; then
    echo '#define CONFIG_HOST "'"$HOST"'"'	>> $CONFIG_H
fi
echo '#define CONFIG_DATE "'$DATE'"'		>> $CONFIG_H

ADD_DEFINE_USED=
while [ $# -gt 0 ]; do
    case "$1" in
    --add-cflags=*)
	STUFF=`echo "$1" | sed 's/--add-cflags=//'`
	echo 'ADD_CFLAGS(['"$STUFF"'])'		>> $CONFIG_M4
	;;
    --add-cppflags=*)
	STUFF=`echo "$1" | sed 's/--add-cppflags=//'`
	echo 'ADD_CPPFLAGS(['"$STUFF"'])'	>> $CONFIG_M4
	;;
    --add-define=*)
	if [ ! "$ADD_DEFINE_USED" ]; then
	    echo '/* from command line: */'	>> $CONFIG_H
	    ADD_DEFINE_USED=true
	fi
	STUFF=`echo "$1" | sed -e 's/--add-define=//' -e 's/=/ /'`
	echo '#define '"$STUFF"			>> $CONFIG_H
	;;
    --add-ldflags=*)
	STUFF=`echo "$1" | sed 's/--add-ldflags=//'`
	echo 'ADD_LDFLAGS(['"$STUFF"'])'	>> $CONFIG_M4
	;;
    --add-objs=*)
	STUFF=`echo "$1" | sed 's/--add-objs=//'`
	echo 'ADD_OBJS(['"$STUFF"'])'		>> $CONFIG_M4
	;;
    --add-opt=*)
	STUFF=`echo "$1" | sed 's/--add-opt=//'`
	echo 'ADD_OPT(['"$STUFF"'])'		>> $CONFIG_M4
	;;
    --add-srcs=*)
	STUFF=`echo "$1" | sed 's/--add-srcs=//'`
	echo 'ADD_SRCS(['"$STUFF"'])'		>> $CONFIG_M4
    	;;
    --add-warn=*)
	# deprecated: exactly the same as --add-flags!
	STUFF=`echo "$1" | sed 's/--add-warn=//'`
	echo 'ADD_CFLAGS(['"$STUFF"'])'		>> $CONFIG_M4
	;;
    --bindir=*)
	STUFF=`echo "$1" | sed 's/--bindir=//'`
	echo "BINDIR=$STUFF"			>> $CONFIG_M4
    	;;
    --snolibdir=*)
	SNOLIB_DIR=`echo "$1" | sed 's/--snolibdir=//'`
    	;;
    --debug)
	DEBUG=true
	set -x
	;;
    --double)
	# sizeof(real_t) must be <= sizeof(int_t),
	#  or IDENT(REAL,REAL) won't work
	# This probably ought to be removed.  Use --longlong instead!
	DOUBLE=true
	;;
    --fast)
	FAST=true
	;;
    --force-gcc)
	FORCEGCC=true
	#WANTGCC=true
	CC=
	;;
    --force-memmove)
	FORCE_MEMMOVE=true
	;;
    --force-sdbm)
	FORCE_SDBM=true
	;;
    --longlong)
	LONGLONG=true
	;;
    --lp64)
	LP64=true
	;;
    --mandir=*)
	MANDIR=`echo "$1" | sed 's/--mandir=//'`
    	;;
    --no-bitfields)
	NO_BITFIELDS=true
	;;
    --no-dlopen)
	NO_DLOPEN=true
	;;
    --no-dynlib)
	NO_DYNLIB=true
	;;
    --no-gcc)
	WANTGCC=
	#FORCEGCC=
	CC=
	;;
    --no-inline)
	INLINE_OK=
	;;
    --no-largefile)
	LARGEFILE_OK=
	;;
    --no-opt)
	NO_OPT=true
	;;
    --opt=*)
	# NOTE!! just sets shell variable
	UOPT=`echo "$1" | sed 's/--opt=//'`
	if [ "x$UOPT" = x ]; then
		NO_OPT=true
	else
		NO_OPT=
	fi
	;;
    --prefix=*)
	PREFIX=`echo "$1" | sed 's/--prefix=//'`
	;;
    --universal)
	UNIVERSAL=true
	;;
    --without-com)
	WITHOUT_COM=1
	;;
    --without-readline)
	WITHOUT_READLINE=1
	;;
    --with-tcl)
	WITH_TCL=1
	;;
    --with-tk)
	echo "NOTE: --with-tk is obsolete" 1>&1
	WITH_TK=1
	;;
    --without-ndbm)
	WITHOUT_NDBM=1
	;;
    '')
	;;
    *)
	cat 1>&2 <<EOF
See INSTALL for documentation on options;

Makefile additions;
	--add-cflags=STR	--add-cppflags=STR	
	--add-define=NAME[=VALUE] --add-ldflags=STR
	--add-objs=STR	--add-srcs=STR

C Compiler selection;
	--force-gcc		--no-gcc

C Compiler settings;
	--add-cflags=STR	--opt=STR
	--add-warn=STR		--fast
	--no-inline		--universal

SNOBOL4 Datatype selection;
	--double		--longlong
	--lp64			--no-bitfields

C Library support selection;
	--force-memmove		--no-dlopen
	--no-dynlib		--no-largefile

Install location;
	--prefix=PATH		[default: /usr/local]
	--bindir=PATH		[default: PREFIX/bin]
	--mandir=PATH		[default: PREFIX/man]
	--snolibdir=PATH	[default: PREFIX/lib/snobol4]

Optional items;
	--with-tcl		--with-tk (obsolete)
	--without-ndbm		--force-sdbm
	--without-readline	--without-com
EOF

	rm -f $CONFIG_M4 $CONFIG_H
	exit 1
	;;
    esac
    shift
done

BINDIR="$PREFIX/bin"
if [ "x$MANDIR" = x ]; then
	MANDIR="$PREFIX/man"
fi
MAN1DIR="$MANDIR/man1"
MAN3DIR="$MANDIR/man3"
if [ "x$SNOLIB_DIR" = x ]; then
    # PREFIX/lib/snobol4/VERSION
    SNOLIB_DIR="$PREFIX/lib/snobol4/$VERSION"
fi

echo 'BINDIR=$(DESTDIR)'"$BINDIR"		>> $CONFIG_M4
echo 'MANDIR=$(DESTDIR)'"$MANDIR"		>> $CONFIG_M4
echo 'MAN1DIR=$(DESTDIR)'"$MAN1DIR"		>> $CONFIG_M4
echo 'MAN3DIR=$(DESTDIR)'"$MAN3DIR"		>> $CONFIG_M4
echo 'SNOLIB_DIR=$(DESTDIR)'"$SNOLIB_DIR"	>> $CONFIG_M4

if [ "$ADD_DEFINE_USED" ]; then
    echo '/* end defines from command line */'	>> $CONFIG_H
fi

# Unix seperators
echo '#define DIR_SEP "/"'			>> $CONFIG_H
echo '#define PATH_SEP ":"'			>> $CONFIG_H

# safe on all Unix systems (requires "fork")
echo '#define PML_SERV'				>> $CONFIG_H

# should work on all Unix systems
echo '#define PML_RANDOM'			>> $CONFIG_H

# Unix Makefile2 created build.c
echo '#define HAVE_BUILD_VARS'			>> $CONFIG_H

if [ "$CC" ]; then
    echo 'using CC environment variable' 1>&2
    CCPATH="$CC"
fi

if [ "$WANTGCC" ]; then
    # look for gcc up front (for Solaris/x86)
    echo 'looking for gcc' 1>&2
    for DIR in `echo $PATH | tr ':' ' '`; do
	if [ -x $DIR/gcc ]; then
	    echo "found gcc in $DIR" 1>&2
	    GCCPATH=gcc
	    break
	fi
    done
fi


# didn't find gcc in PATH; check if CC is gcc
if [ ! "$GCCPATH" ]; then
    echo 'checking if cc is gcc' 1>&2
    TMP=conf$$
    # vanilla bsd43 systems can't use <<- AND EOF must be in col 0!
    cat << EOF > $TMP.c
int
main() {
#if defined(__gcc__) || defined(__GNUC__)
    return 0;
#else
    return 1;
#endif
}
EOF
    if [ "x$CC" = x ]; then
	XCC=cc
    else
	XCC="$CC"
    fi
    if $XCC -o $TMP $TMP.c && ./$TMP; then
	echo 'yes it is' 1>&2
	echo "# cc is gcc"			>> $CONFIG_M4
	GCCPATH="$XCC"
    fi
    rm -f $TMP $TMP.c $TMP.exe

    # if cc is gcc, disregard --no-gcc
    WANTGCC=true
fi

if [ "$FORCEGCC" -a ! "$GCCPATH" ]; then
    echo ' gcc not available' 1>&2
    exit 1
fi

# nearly generic gcc3 option for LP64
GCC64=-m64

# machine, os, release, version
MORV="${MACHINE}:${OS}:${OSREL}:${OSVERS}"

# library support, C compiler choice, flags
case "$MORV" in
9000/[678]??:HP-UX:*:*)
    # dlfcn.h present on hpux 11 systems,
    # but no library on 32-bit systems
    # (this will be reset below for 64-bit compilation)
    NO_DLOPEN=true

    # look for unbundled C; prefer it to gcc
    if [ ! "$FORCEGCC" ]; then
	if [ -x /bin/c89 -o -x /usr/bin/c89 ]; then
	    # ASSume presense of c89 means cc is unbundled compiler
	    # used to use c89 with -D_HPUX_SOURCE
	    echo 'using unbundled cc' 1>&2
	    CCPATH=cc
	    WANTGCC=

	    # LARGEFILE requires cc -Ae or c89 +e -D__STDC_EXT__
	    echo 'ADD_CFLAGS([-Ae])'	>> $CONFIG_M4
	else
	    # gcc is preferable to bundled C, if it works,
	    # but often gives trouble, so don't use if if
	    # not forced!
	    WANTGCC=
	fi
    fi

    # hpux9 and hpux10 have finite(). hpux11 has isfinite().
    if grep '[^s]finite' $INCDIR/math.h >/dev/null; then
	echo 'using system finite() function' 1>&2
    else
	echo 'using system isfinite() macro' 1>&2
        echo '#define finite isfinite'		>> $CONFIG_H
    fi
    ;;

ia64:HP-UX:*:*)
    # default on HP-UX/ia64 is ILP32!
    # different option to get LP64!
    GCC64=-mlp64

    # ia64 has dlopen(), regardless of data model

    # look for unbundled C; prefer it to gcc
    if [ ! "$FORCEGCC" ]; then
	if [ -x /bin/c89 -o -x /usr/bin/c89 ]; then
	    # ASSume presense of c89 means cc is unbundled compiler
	    # used to use c89 with -D_HPUX_SOURCE
	    echo 'using unbundled cc' 1>&2
	    CCPATH=cc
	    WANTGCC=

	    # LARGEFILE requires cc -Ae or c89 +e -D__STDC_EXT__
	    echo 'ADD_CFLAGS([-Ae])'	>> $CONFIG_M4
	elif [ "$LP64" ]; then
	    # gcc version 3.2 20020329 (experimental) beats
	    # (Bundled) cc: HP ANSI C++/C B3910B A.05.36 [Apr 18 2002]
	    # for ILP32 compilations (but doesn't support LP64?!)
	    WANTGCC=

	    # compiler ignores -O (complains)
	    #NO_OPT=true
	fi
    fi

    # hpux11 has isfinite() macro
    if grep '[^s]finite' $INCDIR/math.h >/dev/null; then
	echo 'using system finite() function' 1>&2
    else
	echo 'using system isfinite() macro' 1>&2
        echo '#define finite isfinite'		>> $CONFIG_H
    fi
    ;;

9000/[34]??:4.3bsd:*:*)
    # Mt Xinu 4.3 on hp300
    echo 'using dummy isnan() and finite() functions' 1>&2
    echo 'ADD_OBJS([isnan.o finite.o])'		>> $CONFIG_M4
    echo 'ADD_SRCS([$(ISNAN_C) $(FINITE_C)])'	>> $CONFIG_M4
    ;;

# POWER(RS6k)/PowerPC AIX
*:AIX:2:3|*:AIX:*:[45])
    # prefer regular cc to gcc (only use gcc if forced)
    WANTGCC="$FORCEGCC"
    ;;

*:Darwin:*:*)
    # includes MacOS X
    # libm integrated into libc
    echo 'MATHLIB='				>> $CONFIG_M4
    # symbol conflict with curses (also in libc)
    echo '#define GT GTX'			>> $CONFIG_H
    ;;

*:CYGWIN*:*:*)
    # Cygnus Unix emulation under Win32
    echo 'EXT=.exe'				>> $CONFIG_M4
    CYGWIN=1
    ;;

*:SunOS:*:*)
    if grep 'finite(' /usr/include/ieeefp.h > /dev/null 2>&1; then
	echo '#define FINITE_IN_IEEEFP_H'	>> $CONFIG_H
    fi
esac

# compiler options
if [ "$GCCPATH" -a "$WANTGCC" ]; then
    echo using gcc 1>&2
    CCPATH="$GCCPATH"
    CCVERS=-v
    echo 'CCM=$(CC) -M'				>> $CONFIG_M4

    # w/ gcc v3 -O3 turns on things OTHER than -finline-functions,
    # so make -O3 the default, and use -fno-inline-functions
    OPT=-O3

    if [ ! "$INLINE_OK" ]; then
	echo 'ADD_OPT([-fno-inline-functions])'	>> $CONFIG_M4
    fi
    if [ ! "$NO_OPT" ]; then
	# only works when optimizing
	# (will give warnings for --opt=-g)
	echo 'ADD_CFLAGS([-Wuninitialized])'	>> $CONFIG_M4
    fi
    echo 'ADD_CFLAGS([-Wunused])'		>> $CONFIG_M4

    case "$MORV" in
    *:Darwin:*:*)
        if cc -v 2>&1 | grep 19991024 >/dev/null; then
	    # "Apple gcc-934.3, based on gcc version 2.95.2 19991024"
	    # screws up data_init.c when compiled with -O2
	    echo 'DATA_INIT_CFLAGS=[]_CFLAGS $(MYCPPFLAGS) -O' >> $CONFIG_M4
	fi

	# MacOS has different file types for shared libraries (dylibs)
	# and dynamicly loaded extensions (bundles)
	# -fPIC is the default on OS X

	SO_EXT=.dylib
	SO_LD=$CCPATH
	SO_LDFLAGS=-dynamiclib

	DL_EXT=.bundle
	DL_LD=$CCPATH
	DL_LDFLAGS=-bundle

	UNIVERSAL_SDK_DIR=/Developer/SDKs/MacOSX10.4u.sdk
	if [ "$UNIVERSAL" -a -d $UNIVERSAL_SDK_DIR ]; then
	    echo "ADD_CFLAGS([-arch ppc])"	>> $CONFIG_M4
	    echo "ADD_CFLAGS([-arch i386])"	>> $CONFIG_M4
	    echo "ADD_CFLAGS([-isysroot $UNIVERSAL_SDK_DIR])"	>> $CONFIG_M4
	fi
	;;

    *:SunOS:4*:*)
	SO_EXT=.so
	SO_CFLAGS=-fpic
	SO_LD=/usr/bin/ld
	SO_LDFLAGS='-assert nodefinitions'
	;;

    *)
	SO_EXT=.so
	SO_CFLAGS=-fpic
	SO_LD=$CCPATH
	SO_LDFLAGS=-shared
	;;
    esac

    # -fstrength-reduce is no gain?
    # -fomit-frame-pointer can make debugging harder, so put it here
    #	(on systems where it's harmless, it's turned on by -O)
    FASTFLAGS=-fomit-frame-pointer

    USING_GCC=true
else
    # required options for native compilers
    case "$MORV" in
    9000/[34]*:HP-UX:*:*)
	# HP-UX 68K
	# bundled C compiler lacks -O flag??
	NO_OPT=true
	;;
    9000/[678]*:HP-UX:*:*)
	# HP-UX PA-RISC
	KERNEL_BITS=`(/usr/bin/getconf SC_KERNEL_BITS || echo 32) 2>/dev/null`
	if [ "$KERNEL_BITS" = 64 -a "$LP64" ]; then
	    ARCH_OPT=`getconf XBS5_LP64_OFF64_CFLAGS`
	    # dlopen available in 64-bit world!
	    NO_DLOPEN=
	fi
	if [ -x /bin/c89 -o -x /usr/bin/c89 ]; then
	    SO_EXT=.sl
	    # or +Z
	    SO_CFLAGS=+z
	    SO_LD=ld
	    SO_LDFLAGS=-b
	fi
	FASTFLAGS=-fast
	;;
    ia64:HP-UX:*:*)
	if [ "$LP64" ]; then
	    # +DA2.0W seen under B.11.22!!
	    ARCH_OPT=`getconf XBS5_LP64_OFF64_CFLAGS | sed 's/+DA2.0W/+DD64/'`
	fi
	if [ -x /bin/c89 -o -x /usr/bin/c89 ]; then
	    SO_EXT=.sl
	    # or +Z
	    SO_CFLAGS=+z
	    SO_LD=ld
	    SO_LDFLAGS=-b
	fi
	FASTFLAGS=-fast
	# works for unbundled compiler
	CCVERS=-V
	;;
    *:IRIX*:*:*)
	echo 'CCM=$(CC) -M'			>> $CONFIG_M4
	FASTFLAGS=-Ofast=$MACHINE
	CCVERS=-version

	# Kill warnings: only needed with n32 and 64bit ABI
	# compilers (but harmless with old compiler)?
	echo 'ADD_CFLAGS([-w])'			>> $CONFIG_M4

	# optional 64-bit world;
	# (only allow if "$OS" = IRIX64 ??
	if [ "$LP64" ]; then
	    ARCH_OPT=`getconf XBS5_LP64_OFF64_CFLAGS`
	    # XXX check if non-null?

	    ARCH_LDFLAGS=`getconf XBS5_LP64_OFF64_LDFLAGS`
	    if [ "x$ARCH_LDFLAGS" != x ]; then
		echo 'ADD_LDFLAGS(['$ARCH_LDFLAGS'])'>> $CONFIG_M4
	    fi
	    OPT=-O3
	else
	    # o32 compiler's -O3 wants all files on one line
	    # (to do interprocedural optimization).
	    # n32 compiler's -O3 beats o32 -O2
	    # To override this, use "configure --opt=whatever"
	    OPT='-n32 -O3'
	fi

	SO_EXT=.so
	# -KPIC is default
	SO_CFLAGS=
	# use LD?
	SO_LD=cc
	SO_LDFLAGS=-shared
	;;
    *:AIX:2:3)
	# AIX3 (not tested)
	# for data_init.o
	echo 'ADD_CFLAGS([-qmaxmem=32000])'	>> $CONFIG_M4
	;;
    *:AIX:*:[45])
	# AIX4/xlc
	# -M creates .u files
	# -v outputs man page!!
	# -qlonglong enables "long long"?
	# -Q! turns off inlining (C++ only?)
	if [ "$FAST" ]; then
	    OPT=-O4
	else
	    OPT=-O3
	fi
	# for data_init.o
	echo 'ADD_CFLAGS([-qmaxmem=32000])'	>> $CONFIG_M4

	# XXX check if available? (how?) check for getconf error??
	if [ "$LP64" ]; then
	    ARCH_OPT=`getconf XBS5_LP64_OFF64_CFLAGS`
	    echo 'ARXFLAGS=-X64'		>> $CONFIG_M4
	else
	    # XBS5 model to try if "getconf LFS_CFLAGS" fails
	    XBS5_MODEL=XBS5_ILP32_OFFBIG_CFLAGS
	    # on Sun this assumes native compiler, undefines stuff,
	    # and fiddles with ANSI mode (sets __STDC__ to zero), so
	    # we only use it when the obvious fails!
	fi

	# "AIX 4.3 and later"
	SO_EXT=.so
	SO_LDFLAGS=-bM:SRE
	;;
    *:OSF1:*:*)
	OPT=-O4
	if [ ! "$INLINE_OK" ]; then
	    echo 'ADD_OPT([-no_inline])'	>> $CONFIG_M4
	fi
	echo 'CCM=$(CC) -M'			>> $CONFIG_M4

	case "$OSREL" in
	V[1-3].*) ;;
	*) OPT="$OPT -fast" ;;
	esac

	CCVERS=-V

	# no cc flags needed
	SO_EXT=.so
	SO_LD=cc
	# NOTE! quoting make it useless in Makefile!
	SO_LDFLAGS="-shared -expect_unresolved '\\*'"
	;;

    *:SunOS:4*:*)
	# SunOS 4 cc
	echo 'CCM=$(CC) -M'			>> $CONFIG_M4
	OPT=-O2
	# cc can't optimize data_init.c at ALL
	echo 'DATA_INIT_CFLAGS=[]_CFLAGS $(MYCPPFLAGS)'	>> $CONFIG_M4

	SO_EXT=.so
	SO_CFLAGS=-pic
	SO_LD=/bin/ld
	SO_LDFLAGS='-assert nodefinitions'
	;;
    *:SunOS:5*:*)
	# SunPRO;
	if [ "$INLINE_OK" ]; then
	    OPT=-xO4
	else
	    OPT=-xO3
	fi
	echo 'CCM=$(CC) -xM'			>> $CONFIG_M4
	FASTFLAGS=-fast
	CCVERS=-V
	if [ "$LP64" -a -x /usr/bin/isainfo ] && \
	   /usr/bin/isainfo -v | grep '^64' >/dev/null; then
	    ARCH_OPT=`getconf XBS5_LP64_OFF64_CFLAGS`
	fi

	SO_EXT=.so
	SO_CFLAGS=-Kpic
	SO_LDFLAGS=-G
	;;
    esac
fi

if [ "$CCPATH" ]; then
    echo "CC=$CCPATH"				>> $CONFIG_M4
    # for "check" script:
    CC=$CCPATH
    export CC
fi

# include C compiler version output in comments
if [ "x$CCVERS" != x ]; then
    echo '################'			>> $CONFIG_M4
    echo '# C Compiler:'			>> $CONFIG_M4
    echo '#' ${CCPATH-cc} $CCVERS		>> $CONFIG_M4
    ${CCPATH-cc} $CCVERS 2>&1 | sed 's/^/# /'	>> $CONFIG_M4
    echo '################'			>> $CONFIG_M4
fi

echo checking sizes of C types 1>&2
# detect compilers/architectures where LP64 is the default
# MUST be after compiler choice
# check for alignment as well (for NO_BITFIELDS)?
# use SDOUB for "DOUBLE" (check vs. int_t)??
# see if "long long" available (need to compile seperately)???
TMP=conf$$
cat << EOF > $TMP.c
#include <stdio.h>

int
main() {
    printf("%d %d %d %d\n", (int)sizeof(void *), (int)sizeof(long),
			    (int)sizeof(double), (int)sizeof(int));
    return 0;
}
EOF
if ${CCPATH-cc} $CFLAGS -o $TMP $TMP.c && ./$TMP > $TMP.out &&
  read SPTR SLONG SDOUB SINT < $TMP.out; then
    echo 'sizeof(int) = '$SINT		1>&2
    echo '# sizeof(int) = '$SINT	>> $CONFIG_M4

    echo 'sizeof(void *) = '$SPTR	1>&2
    echo '# sizeof(void *) = '$SPTR	>> $CONFIG_M4

    echo 'sizeof(long) = '$SLONG	1>&2
    echo '# sizeof(long) = '$SLONG	>> $CONFIG_M4

    echo 'sizeof(double) = '$SDOUB	1>&2
    echo '# sizeof(double) = '$SDOUB	>> $CONFIG_M4

    # XXX ASSume sizeof(char) == 1 (8 bits)
    if [ "$SPTR" = 8 -a "$SLONG" = 8 ]; then
	echo 'LP64 is native programming model' 1>&2
	LP64=true
	LP64_NATIVE=true
    fi
fi
rm -f $TMP $TMP.c $TMP.out $TMP.exe

# after size checks;
if [ "$USING_GCC" ]; then
    # avoid trying option where LP64 is the default, and the compiler could
    # be gcc2 -- there was no generic option back then!
    if [ "$LP64" -a ! "$LP64_NATIVE" ]; then
	ARCH_CFLAGS="$GCC64"
	# for "check" script
	CFLAGS="$GCC64"
	export CFLAGS
    fi
fi

# after size checks;
if [ "$LP64" ]; then
    # In 64-bit environments, alignment restrictions on int_t/pointer
    # data is likely to pad descriptors out to 16 bytes, so don't pack
    # DESCR "v" field into 24 bits.
    NO_BITFIELDS=true

    # make real_t same size as int_t
    DOUBLE=true

    # make --longlong a noop
    LONGLONG=
fi

if [ "$ARCH_CFLAGS" ]; then
    echo 'ADD_CFLAGS(['$ARCH_CFLAGS'])'		>> $CONFIG_M4
fi

if [ "$NO_OPT" ]; then
    echo OPT=					>> $CONFIG_M4
else
    if [ "x$UOPT" != x ]; then
	echo "OPT=$UOPT"			>> $CONFIG_M4
    elif [ "x$OPT" != x ]; then
	echo "OPT=$OPT"				>> $CONFIG_M4
    fi
    if [ "$FAST" -a "x$FASTFLAGS" != x ]; then
	echo 'ADD_OPT(['"$FASTFLAGS"'])'	>> $CONFIG_M4
    fi
fi

if [ ! "$INLINE_OK" ]; then
    # use non-reordered file
    echo 'SNOBOL4=snobol4'			>> $CONFIG_M4
fi

if [ "$LONGLONG" ]; then
    # Force use of 64-bit int and real types even if native pointers
    # are smaller.  gcc generates TONS of compiler warnings!!

    # make real_t same size as int_t
    DOUBLE=true

    # Not tested in all environments (some compilers
    # may require options to enable "long long" data type)

    echo using long long for SNOBOL4 INTEGER type 1>&2
    echo '#define INT_T long long'		>> $CONFIG_H

    if config/check stdlib.h 'strtoll("", 0, 0)'; then
	echo 'using C99 printf %lld' 1>&2
	echo 'SPCINT_C=$(SRCDIR)lib/c99/spcint.c' >> $CONFIG_M4
	echo 'using C99 strtoll()' 1>&2
	echo 'INTSPC_C=$(SRCDIR)lib/c99/intspc.c' >> $CONFIG_M4
    else
	# here with gcc on SunOS4, for example
	echo 'using private string to integer' 1>&2
	echo 'SPCINT_C=$(SRCDIR)lib/longlong/spcint.c' >> $CONFIG_M4
	echo 'using private integer to string' 1>&2
	echo 'INTSPC_C=$(SRCDIR)lib/longlong/intspc.c' >> $CONFIG_M4
    fi
fi

if [ "$NO_BITFIELDS" ]; then
    echo '#define NO_BITFIELDS'			>> $CONFIG_H

    if [ "$LONGLONG" -o "$LP64" ]; then
	# int_t is wide, so should be safe to use all 32-bits of "v" field
	# (can't be confused for a negative int_t)
	echo '#define SIZLIM (~(VFLD_T)0)'	>> $CONFIG_H
    fi
    # otherwise snotypes uses 2^31-1 if NO_BITFIELDS defined.
fi

if [ "$DOUBLE" ]; then
    # sizeof(real_t) must be <= sizeof(int_t), or IDENT(REAL,REAL) won't work
    echo using double for SNOBOL4 REAL type 1>&2
    echo '#define REAL_T double'		>> $CONFIG_H
fi

if config/check stdlib.h; then
    echo 'using ANSI stdlib.h' 1>&2
    echo '#define HAVE_STDLIB_H'		>> $CONFIG_H
    HAVE_STDLIB_H=1
fi

if config/check unistd.h; then
    echo 'using POSIX unistd.h' 1>&2
    echo '#define HAVE_UNISTD_H'		>> $CONFIG_H
    HAVE_UNISTD_H=1
fi

# Berkeley rusage() preferable, since it returns a timeval,
# which gives both a large range of return times, and possible
# resolution down to microseconds.

# grep needed for Solaris 2.4 which has resource.h but not defines
# (it's in /usr/ucbinclude/rusage.h!), but it's just simulated via times()
# anyhow).  Later versions of Solaris (8 and up) implement getrusage by
# opening /proc/pid/usage, which contains a timestruc_t, with possible
# nanosecond resolution.

# Linux has getrusage() system call, "getrusage()" in resource.h.
# struct "rusage" is in a "bits" include file.  Most implementations
# keep runtimes in HZ ticks, and default HZ value is 100, so times()
# is just as good.

# SunOS4 resource.h has "struct rusage", but no prototype,
# so we grep for just "rusage", which catches either struct
# or "getrusage()" prototype

# Cygwin has getrusage(), but it only works under NT, so use the
# Win32 native mstime.c, which will fall back to Win9x compatibility.

if [ "$CYGWIN" ]; then
    echo 'using Win32 native calls for runtimes' 1>&2
    echo 'MSTIME_C=$(SRCDIR)lib/win32/mstime.c'	>> $CONFIG_M4
elif config/check "sys/time.h sys/resource.h" \
    "struct rusage r; getrusage(&r, RUSAGE_SELF)"; then
    echo 'using getrusage() for runtimes' 1>&2
    echo 'MSTIME_C=$(SRCDIR)lib/bsd/mstime.c'	>> $CONFIG_M4

    # user time can run backwards for processes with small
    # runtimes on BSD44 based systems; time deltas in &TRACE can
    # appear negative!!
    case "$OS" in
    FreeBSD|NetBSD|BSD/OS|BSD/386|Darwin|4.4BSD|OpenBSD)
	echo '#define GETRUSAGE_BUG'		>> $CONFIG_H
	;;
    esac
elif [ "$HAVE_UNISTD_H" ]; then
    echo 'using times() + sysconf() for runtimes' 1>&2
    echo 'MSTIME_C=$(SRCDIR)lib/posix/mstime.c'	>> $CONFIG_M4
elif config/check sys/times.h "struct tms tt; times(&tt)"; then
    # V7 had times(), but not times.h!!!
    echo 'using times() for runtimes' 1>&2
    # XXX warning????
    # XXX message about presumed HZ value??
    echo 'MSTIME_C=$(SRCDIR)lib/generic/mstime.c' >> $CONFIG_M4
    case "$MORV" in
    9000/[34]??:HP-UX:*:*)
	# HPUX on 68040 (hp4xx) and HPUX 9
	echo '#define HZ 50'			>> $CONFIG_H
	;;
    esac
elif [ "$HAVE_STDLIB_H" ]; then
    echo 'using clock() for runtimes' 1>&2
    # least preferable; includes system time (or may be wall clock time)
    # may need to guess tick size
    echo 'MSTIME_C=$(SRCDIR)lib/ansi/mstime.c'	>> $CONFIG_M4
else
    echo 'using dummy mstime.c' 1>&2	
    echo 'MSTIME_C=$(SRCDIR)lib/dummy/mstime.c'	>> $CONFIG_M4
fi

# prefer ranlib
if [ -f /usr/bin/ranlib -o -f /bin/ranlib ]; then
    echo 'using ranlib' 1>&2
else
    # dummy out RANLIB
    echo 'RANLIB=true'				>> $CONFIG_M4
fi

# prefer uname(2/3)
# NeXT/Open Step have $INCDIR/bsd/sys/utsname.h?? transparent??
if config/check sys/utsname.h "struct utsname u; uname(&u)"; then
    echo 'using uname() for system name information' 1>&2
else
    OSNAME="$OS $OSREL"
    if [ "$OSNAME" = 'unknown unknown' ]; then
	OSNAME='unknown'
    fi
    echo 'using "'$MACHINE'" for hardware name' 1>&2
    echo '#define HWNAME "'$MACHINE'"'		>> $CONFIG_H
    echo 'using "'$OSNAME'" for system name' 1>&2
    echo '#define OSNAME "'$OSNAME'"'		>> $CONFIG_H
    echo 'SYS_C=$(SRCDIR)lib/generic/sys.c'	>> $CONFIG_M4
fi

# prefer v7/berkeley string functions
# These days strings.h may be just the "bstring" functions (bzero, bcopy)
# and the index/rindex functions, and not include the basic str* functions.
# Or, it may be indentical to string.h (one just includes the other), or
# it might be a superset of string.h.  I haven't (yet) seen a situation
# where it's fatal to include both.

if config/check strings.h 'rindex("", 65)'; then
    echo '#define HAVE_STRINGS_H'		>> $CONFIG_H
    echo 'using BSD strings.h' 1>&2
    HAVE_STRINGS_H=1
fi

if config/check string.h 'strchr("", 65)'; then
    echo 'using ANSI string.h' 1>&2
    echo '#define HAVE_STRING_H'		>> $CONFIG_H
    if [ "$HAVE_STRINGS_H" ]; then
	true
    elif grep bcopy $INCDIR/string.h >/dev/null; then
	echo 'using system provided bcopy() function' 1>&2
    elif test "$FORCE_MEMMOVE" && \
        grep memmove $INCDIR/string.h >/dev/null; then
	# The only system with no bcopy() which has memmove()
	# is Solaris 2.4, and our bcopy() beats memmove()?!
	echo 'using system provided memmove() function' 1>&2
	echo '#define USE_MEMMOVE'		>> $CONFIG_H
    else
	echo 'using private bcopy() function' 1>&2
	echo 'ADD_OBJS([bcopy.o])'		>> $CONFIG_M4
	echo 'ADD_SRCS([$(BCOPY_C)])'		>> $CONFIG_M4
	echo '#define NEED_BCOPY'		>> $CONFIG_H
    fi

    # memset may be optimized for zeroing, or may not!
    if [ "$HAVE_STRINGS_H" ] || grep bzero $INCDIR/string.h >/dev/null; then
	echo 'using system provided bzero() function' 1>&2
    else
	echo 'using private bzero() function' 1>&2
	echo 'ADD_OBJS([bzero.o])'		>> $CONFIG_M4
	echo 'ADD_SRCS([$(BZERO_C)])'		>> $CONFIG_M4
	echo '#define NEED_BZERO'		>> $CONFIG_H
    fi
fi

if config/check sys/socket.h ""; then
    echo 'using sockets for TCP/IP support' 1>&2

    # check if network libraries needed
    SOCKINC='sys/socket.h netdb.h'
    SOCKFRAG='int s = socket(AF_INET, SOCK_STREAM, 0); struct hostent *hp = gethostbyname("")'
    if config/check "$SOCKINC" "$SOCKFRAG"; then
	NETLIBS=
    elif config/check "$SOCKINC" "$SOCKFRAG" -lsocket; then
	echo 'using libsocket' 1>&2
	echo 'ADD_LDFLAGS([-lsocket])'		>> $CONFIG_M4
	NETLIBS=-lsocket
    elif config/check "$SOCKINC" "$SOCKFRAG" '-lsocket -lnsl'; then
	echo 'using libsocket' 1>&2
	echo 'ADD_LDFLAGS([-lsocket])'		>> $CONFIG_M4
	echo 'using libnsl' 1>&2
	echo 'ADD_LDFLAGS([-lnsl])'		>> $CONFIG_M4
	NETLIBS='-lsocket -lnsl'
    fi

    if config/check "$SOCKINC" \
	'struct addrinfo aa[2], *ap; getaddrinfo("", "", aa, &ap)' \
	"$NETLIBS"; then
	echo 'using getaddrinfo for TCP/IPv6 compatibility' 1>&2
	echo 'INET_O=inet6.o'			>> $CONFIG_M4
	if grep bindresvport_sa $INCDIR/rpc/rpc.h >/dev/null 2>&1; then
	    # FreeBSD 4, NetBSD
	    echo 'using system bindresvport_sa() function (with rpc/rpc.h)' 1>&2
	    echo '#define BINDRESVPORT_IN_RPC_H' >> $CONFIG_H
	elif grep bindresvport_sa $INCDIR/netinet/in.h $INCDIR/unistd.h>/dev/null 2>&1; then
	    echo 'using system bindresvport_sa() function' 1>&2
	else
	    echo 'using private bindresvport_sa() function' 1>&2
	    echo '#define NEED_BINDRESVPORT_SA'	>> $CONFIG_H
	    AUX_BINDRESVPORT=true
	fi
    else # no getaddrinfo/IPv6
	if grep bindresvport $INCDIR/rpc/rpc.h >/dev/null 2>&1; then
	    # FreeBSD 3.x
	    echo 'using system bindresvport() function (with rpc/rpc.h)' 1>&2
	    echo '#define BINDRESVPORT_IN_RPC_H' >> $CONFIG_H
	elif grep bindresvport $INCDIR/netinet/in.h $INCDIR/unistd.h >/dev/null 2>&1; then
	    echo 'using system bindresvport() function' 1>&2
	else
	    echo 'using private bindresvport() function' 1>&2
	    echo '#define NEED_BINDRESVPORT' >> $CONFIG_H
	    AUX_BINDRESVPORT=true
	fi
    fi
    if [ "$AUX_BINDRESVPORT" ]; then
	echo 'ADD_OBJS([bindresvport.o])'	>> $CONFIG_M4
	echo 'ADD_SRCS([$(BINDRESVPORT_C)])'	>> $CONFIG_M4
    fi

    if config/check netinet/in.h 'struct sockaddr_in6 in6' || \
	config/check netinet6/in.h 'struct sockaddr_in6 in6'; then
	echo '#define HAVE_SOCKADDR_IN6' >> $CONFIG_H
    fi

    # use socklen_t if avaialble
    if config/check sys/socket.h 'socklen_t len'; then
	echo '#define SOCKLEN_T socklen_t'	>> $CONFIG_H
    else
	echo '#define SOCKLEN_T int'		>> $CONFIG_H
    fi

    # use BSD bidirectional popen if socketpair available
    # (could try to use two pipes??)
    if config/check sys/socket.h \
	'int s[2]; socketpair(AF_INET, SOCK_STREAM, 0, s)'; then
	echo 'using private popen() function' 1>&2
	echo 'ADD_OBJS([popen.o])'		>> $CONFIG_M4
	echo 'ADD_SRCS([$(POPEN_C)])'		>> $CONFIG_M4
	if config/check paths.h 'char *foo = _PATH_BSHELL'; then
	    echo 'using paths.h for popen() shell path' 1>&2
	    echo '#define HAVE_PATHS_H'		>> $CONFIG_H
	else
	    # XXX outer loop for sh, ksh, bash, csh, tcsh?
	    for DIR in /usr/bin /bin `echo $PATH | tr ':' ' '`; do
		if [ -x $DIR/sh ]; then
		    BSHELL=$DIR/sh
		    break
		fi
	    done
	    if [ "$BSHELL" ]; then
		echo "using $BSHELL for popen() shell" 1>&2
		echo '#define _PATH_BSHELL "'$BSHELL'"'	>> $CONFIG_H
	    else
		echo 'WARNING: could not find shell for popen()' 1>&2
	    fi
	fi

	if config/check vfork.h 'vfork()'; then
	    echo 'using vfork() from vfork.h' 1>&2
	    echo '#define HAVE_VFORK_H'		>> $CONFIG_H
	elif config/check unistd.h 'vfork()'; then
	    echo 'using vfork() (with unistd.h)' 1>&2
	    echo '/* vfork() in unistd.h */'	>> $CONFIG_H
	elif config/check '' 'vfork()'; then
	    echo 'using vfork()' 1>&2
	    echo '/* vfork() */'		>> $CONFIG_H
	else
	    NO_VFORK=1
	fi
	if [ "$NO_VFORK" ]; then
	    echo 'using fork() for vfork()' 1>&2
	    echo '#define vfork fork'		>> $CONFIG_H
	fi
    fi
else
    echo 'no sockets; TCP not available' 1>&2
    echo 'INET_C=$(SRCDIR)lib/dummy/inet.c'	>> $CONFIG_M4
fi

if config/check termios.h 'struct termios t; tcgetattr(0, &t)'; then
    echo 'using termios.h for tty modes' 1>&2
    echo 'TTY_C=$(SRCDIR)lib/posix/tty.c'	>> $CONFIG_M4
elif config/check termio.h 'struct termio t; ioctl(0, TCGETA, &t)'; then
    echo 'using termio.h for tty modes' 1>&2
    echo '#define USE_TERMIO'			>> $CONFIG_H
    echo 'TTY_C=$(SRCDIR)lib/posix/tty.c'	>> $CONFIG_M4
elif config/check sgtty.h 'struct sgtty s; gtty(0, &s)'; then
    # systems with both termio/termios and sgtty usually
    # are simulating sgtty, so it's the last check.
    echo 'using sgtty.h for tty modes' 1>&2
    echo 'TTY_C=$(SRCDIR)lib/bsd/tty.c'		>> $CONFIG_M4
elif config/check sys/ttyio.h 'struct sgtty s; gtty(0, &s)'; then
    # Bell Research Unix
    echo 'using sys/ttyio.h for tty modes' 1>&2
    echo 'TTY_C=$(SRCDIR)lib/bsd/tty.c'		>> $CONFIG_M4
    echo '#define USE_TTYIO'			>> $CONFIG_H
else
    echo 'no tty support found' 1>&2
    echo 'TTY_C=$(SRCDIR)lib/dummy/tty.c'	>> $CONFIG_M4
fi

# Look for getopt definition in stdio.h include file which might
# conflict with our K&R getopt.c causing warnings, and even compile
# failure.  If none found compile our own getopt.o into snolib.a.
# An alternative would be to try compiling a small program which uses
# getopt() (using $CCPATH as compiler), but grep seems faster!

# getopt() now in POSIX.2; check stdlib.h and unistd.h (our getopt.c
# doesn't include them, but avoid compiling our own if system version
# available).

if config/check stdio.h 'getopt(argc, argv, "")' || \
   config/check stdlib.h 'getopt(argc, argv, "")' || \
   config/check unistd.h 'getopt(argc, argv, "")'; then
    echo 'using system provided getopt() function' 1>&2
else
    # may not be needed, but should compile without error!
    echo 'using private getopt() function' 1>&2
    echo 'ADD_OBJS([getopt.o])'			>> $CONFIG_M4
    echo 'ADD_SRCS([$(GETOPT_C)])'		>> $CONFIG_M4
fi

# look for dynamic library support for "load";
# overrides "dummy" and "bsd" style, unless given --no-dynlib
if [ ! "$NO_DYNLIB" ]; then
    # look for sun/unix98 style dlopen()
    if [ ! "$NO_DLOPEN" ]; then
      if config/check dlfcn.h 'dlopen("", 0)' || \
	  config/check dlfcn.h 'dlopen("", 0)' -ldl; then
	LOAD=unix98
	echo 'using dlopen() for LOAD() support' 1>&2
	echo 'LOAD_C=$(SRCDIR)lib/unix98/load.c' >> $CONFIG_M4
	# XXX use .dll under Cygwin??
	SNOLIB_FILE=snolib.so

	case "${OS}${OSREL}" in
	Darwin*)
	    # darwin dlopen() support in cctools/libdyld??
	    if [ -f /usr/lib/libdl.dylib ]; then
		# Darwin 7.9 (OS X 10.3) and later??
		echo 'ADD_LDFLAGS([-ldl])'	>> $CONFIG_M4
	    elif [ -f /sw/lib/libdl.dylib ]; then
		echo 'ADD_LDFLAGS([-L/sw/lib -ldl])' >> $CONFIG_M4
	    fi
	    TRY_UNDERSCORE=true
	    SNOLIB_FILE=snolib.bundle
	    ;;
	FreeBSD*)
	    if [ -x /usr/bin/objformat ]; then
		case `/usr/bin/objformat` in
		elf)
		    echo 'ADD_LDFLAGS([-export-dynamic])' >> $CONFIG_M4
		    ;;
		*)
		    TRY_UNDERSCORE=true
		    ;;
		esac
	    else
		TRY_UNDERSCORE=true
	    fi
	    ;;
	Linux*)
	    # XXX detect a.out systems?
	    echo 'ADD_LDFLAGS([-ldl])'		>> $CONFIG_M4
	    echo 'ADD_LDFLAGS([-export-dynamic])' >> $CONFIG_M4
	    ;;
	NetBSD*)
	    if echo __ELF__ | cc -E - | grep __ELF__ >/dev/null; then
		echo 'ADD_LDFLAGS([-export-dynamic])' >> $CONFIG_M4
	    else
		# a.out system; need underscore
		# COFF may not, but this should be mostly harmless
		TRY_UNDERSCORE=true
	    fi
	    ;;
	OpenBSD*)
	    # XXX detect ELF??
	    TRY_UNDERSCORE=true
	    ;;
	SunOS4*)
	    echo 'ADD_LDFLAGS([-ldl])'		>> $CONFIG_M4
	    # a.out system; need underscore
	    TRY_UNDERSCORE=true
	    ;;
	SunOS5*)
	    echo 'ADD_LDFLAGS([-ldl])'		>> $CONFIG_M4
	    ;;
	esac
	if [ "$TRY_UNDERSCORE" ]; then
	    echo '#define TRY_UNDERSCORE'	>> $CONFIG_H
	fi
      fi
    elif config/check dl.h 'shl_load("", BIND_DEFERRED, 0L)'; then
	LOAD=hpux
	echo 'using HP-UX shl_load() for LOAD() support' 1>&2
	echo 'undefine([hpux])'			>> $CONFIG_M4
	echo 'LOAD_C=$(SRCDIR)lib/hpux/load.c'	>> $CONFIG_M4
	SNOLIB_FILE=snolib.sl
	# export all symbols
	echo 'ADD_LDFLAGS([-Wl,-E])'		>> $CONFIG_M4
	echo 'ADD_LDFLAGS([-ldld])'		>> $CONFIG_M4
    elif config/check mach-o/dyld.h 'NSObjectFileImage ofi'; then
	LOAD=next
	echo 'using NextStep/Darwin LOAD() support' 1>&2
	echo 'LOAD_C=$(SRCDIR)lib/next/load.c'	>> $CONFIG_M4
	if [ "$OS" = Darwin ]; then
	    echo '#define TRY_UNDERSCORE'	>> $CONFIG_H
	    SNOLIB_FILE=snolib.bundle
	else
	    SNOLIB_FILE=snolib.dyld
	fi
    elif config/check a.out.h 'struct exec a; int x = OMAGIC'; then
	LOAD=aout
	echo 'using a.out ld for LOAD() support' 1>&2
	echo 'LOAD_C=$(SRCDIR)lib/bsd/load.c'	>> $CONFIG_M4
	if [ -f /usr/bin/ld ]; then
	    echo 'found /usr/bin/ld' 1>&2
	elif [ -f /bin/ld ]; then
	    echo 'found /bin/ld' 1>&2
	    echo '#define LD_PATH "/bin/ld"'	>> $CONFIG_H
	fi
	SNOLIB_FILE=snolib.a
    fi
fi

# used for includes, and by load functions
echo '#define SNOLIB_DIR "'"$SNOLIB_DIR"'"'	>> $CONFIG_H

if [ "$SNOLIB_FILE" ]; then
    echo '# NOTE: SNOLIB_FILE in config.h too!'	>> $CONFIG_M4
    echo 'SNOLIB_FILE='$SNOLIB_FILE		>> $CONFIG_M4
    echo '/* NOTE: SNOLIB_FILE in config.m4 too! */' >> $CONFIG_H
    echo '#define SNOLIB_FILE "'$SNOLIB_FILE'"' >> $CONFIG_H
fi

if [ ! "$LOAD" ]; then
    echo 'using dummy LOAD() support' 1>&2
    echo 'LOAD_C=$(SRCDIR)lib/dummy/load.c'	>> $CONFIG_M4
fi

# Seperate from LARGEFILE;  BSD4.4 systems have large file
# support by default, without any LARGEFILE define chicanery.

# fseeko isn't needed if "long" (the offset type used by fseek)
# is 64 bits, but should be safe in any case (famous last words).
if config/check stdio.h 'fseeko(stdout, 0, 0)'; then
    echo 'using fseeko for file positioning' 1>&2
    echo '#define HAVE_FSEEKO'			>> $CONFIG_H
fi

# detect GNU libc (glibc) -- run in subshell to redirect error output
# if --version not a valid option.  Future versions might not print
# version as last item on line (currently it's the 4th), but at this
# moment, I only care about detecting version 2.1;
GLIBC=`(getconf --version)2>&1 | awk '/GNU libc/ || /EGLIBC/ { print $NF }'`

if [ "x$GLIBC" != x ]; then
    echo "# GLIBC version $GLIBC"		>> $CONFIG_M4
    echo '#define HAVE_GLIBC'			>> $CONFIG_H
    echo "detected GNU libc version $GLIBC" 1>&2

    # glibc 2.1 is broken; fseeko() cannot be used
    case "$GLIBC" in
    2.1*)
	LARGEFILE_OK=
	;;
    esac
fi

# if using a native 64bit data model no options should be needed
# for large file support
if [ "$LP64" ]; then
    LARGEFILE_OK=
fi

if [ "$LARGEFILE_OK" ] &&
   egrep 'LARGEFILE|LARGE_FILE' $INCDIR/stdio.h >/dev/null 2>&1; then
    echo 'using large file support' 1>&2
    if [ "x$GLIBC" = x -a -f /usr/bin/getconf ]; then
	if getconf LFS_CFLAGS >/dev/null 2>&1; then
	    USE_FLAGS=LFS_CFLAGS
	elif [ "$XBS5_MODEL" ]; then
	    if getconf $XBS5_MODEL >/dev/null 2>&1; then
		USE_FLAGS="$XBS5_MODEL"
	    fi
	fi
    fi
    if [ "$USE_FLAGS" ]; then
	FLAGS=`getconf $USE_FLAGS`
	echo "using getconf ${USE_FLAGS}: $FLAGS" 1>&2
	for X in $FLAGS; do
	    case "$X" in
	    -D*)
		echo "X$X" | \
		    sed -e 's/^X-D/#define /' -e 's/=/ /' >> $CONFIG_H
		;;
	    -U*)
		echo "X$X" | \
		    sed -e 's/^X-U/#undef /' -e 's/=/ /' >> $CONFIG_H
		;;
	    *)
		echo 'ADD_CFLAGS(['$X'])'		>> $CONFIG_M4
		;;
	    esac
	done
    else
	# gnu libc getconf LFS_CFLAGS leaves out _LARGEFILE_SOURCE!!
	# so does HP-UX getconf XBS5_ILP32_OFFBIG_CFLAGS
	echo 'using default LARGEFILE defines' 1>&2
	echo '#define _LARGEFILE_SOURCE'	>> $CONFIG_H
	echo '#define _FILE_OFFSET_BITS 64'	>> $CONFIG_H
    fi
fi

# RedHat 6.0 has mman.h, but not madvise()
if config/check sys/mman.h 'madvise(0, 0, MADV_RANDOM)' || \
    config/check sys/mman.h 'posix_madvise(0, 0, POSIX_MADV_RANDOM)'; then
    echo 'using madvise() during GC' 1>&2
    echo 'DYNAMIC_C=$(SRCDIR)lib/posix/dynamic.c' >> $CONFIG_M4
elif config/check sys/vadvise.h 'vadvise(VA_ANOM)'; then
    echo 'using vadvise() during GC' 1>&2
    echo 'DYNAMIC_C=$(SRCDIR)lib/bsd/dynamic.c'	>> $CONFIG_M4
else
    echo 'using generic dynamic.c' 1>&2
    echo 'DYNAMIC_C=$(SRCDIR)lib/generic/dynamic.c' >> $CONFIG_M4
fi

# just give in & compile a little program that uses SIGTSTP??
if config/check signal.h 'int i = SIGTSTP'; then
    if config/check signal.h 'sigset_t ss; sigemptyset(&ss)'; then
	echo 'using POSIX suspend.c' 1>&2
    elif config/check signal.h 'sigmask(0)'; then
	echo 'using BSD suspend.c' 1>&2
	echo 'SUSPEND_C=$(SRCDIR)lib/bsd/suspend.c'	>> $CONFIG_M4
    else
	# can't figure out how to unblock SIGTSTP in handler; use SIGSTOP
	echo 'using generic suspend.c' 1>&2
	echo 'SUSPEND_C=$(SRCDIR)lib/generic/suspend.c' >> $CONFIG_M4
    fi
else
    echo 'using dummy suspend.c' 1>&2
    echo 'SUSPEND_C=$(SRCDIR)lib/dummy/suspend.c' >> $CONFIG_M4
fi

if config/check sys/time.h \
    'struct timeval tv; int x; gettimeofday(&tv,0); x = tv.tv_sec'; then
    echo 'using gettimeofday()' 1>&2
    echo '#define HAVE_GETTIMEOFDAY'		>> $CONFIG_H
fi
if config/check time.h 'struct tm tm; int i = tm.tm_gmtoff'; then
    echo 'using tm_gmtoff' 1>&2
    echo '#define HAVE_TM_GMTOFF'		>> $CONFIG_H
fi

echo '#define HAVE_SLEEP'			>> $CONFIG_H
SELECT_FRAG='fd_set x; struct timeval tv; select(0,&x,&x,&x,&tv);'
if config/check time.h 'struct timespec ts; nanosleep(&ts,0)'; then
    echo 'using posix nanosleep()' 1>&2
    echo 'SLEEP_C=$(SRCDIR)lib/posix/sleep.c' >> $CONFIG_M4
elif config/check sys/select.h "$SELECT_FRAG"; then
    echo 'using posix select() for sleep' 1>&2
    echo '#define HAVE_SELECT_H'		>> $CONFIG_H
elif config/check 'sys/time.h sys/types.h' "$SELECT_FRAG"; then
    echo 'using select() for sleep' 1>&2
else
    echo 'using generic sleep()' 1>&2
    echo 'SLEEP_C=$(SRCDIR)lib/generic/sleep.c' >> $CONFIG_M4
fi
if config/check time.h 'struct tm tm; strptime("", "", &tm)'; then
    echo 'using strptime()' 1>&2
    echo '#define HAVE_STRPTIME'		>> $CONFIG_H
fi
echo '#define PML_TIME'				>> $CONFIG_H

# output SO/DL defines

# on everything except Darwin, one format suffices
if [ "x$SO_EXT" != x -a "x$DL_EXT" = x ]; then
    DL_EXT="$SO_EXT"
    DL_CFLAGS="$SO_CFLAGS"
    DL_LD="$SO_LD"
    DL_LDFLAGS="$SO_LDFLAGS"
fi

if [ "x$SO_EXT" != x ]; then
    echo '# Shared Object Libraries'	>> $CONFIG_M4
    echo "SO_EXT=$SO_EXT"		>> $CONFIG_M4
    echo "SO_CFLAGS=$SO_CFLAGS"		>> $CONFIG_M4
    echo "SO_LD=$SO_LD"			>> $CONFIG_M4
    echo "SO_LDFLAGS=$SO_LDFLAGS"	>> $CONFIG_M4

    echo '/* Shared Object Libraries (in config.m4 too) */' >> $CONFIG_H
    echo "#define SO_EXT \"$SO_EXT\""	>> $CONFIG_H
    echo "#define SO_CFLAGS \"$SO_CFLAGS\"" >> $CONFIG_H
    echo "#define SO_LD \"$SO_LD\""	>> $CONFIG_H
    echo "#define SO_LDFLAGS \"$SO_LDFLAGS\"" >> $CONFIG_H
fi

if [ "x$DL_EXT" != x ]; then
    echo '# Dynamicly Loaded Extensions' >> $CONFIG_M4
    echo "DL_EXT=$DL_EXT"		>> $CONFIG_M4
    echo "DL_CFLAGS=$DL_CFLAGS"		>> $CONFIG_M4
    echo "DL_LD=$DL_LD"			>> $CONFIG_M4
    echo "DL_LDFLAGS=$DL_LDFLAGS"	>> $CONFIG_M4
    echo '/* Dynamicly Loaded Extensions (in config.m4 too) */' >> $CONFIG_H
    echo "#define DL_EXT \"$DL_EXT\""	>> $CONFIG_H
    echo "#define DL_CFLAGS \"$DL_CFLAGS\"" >> $CONFIG_H
    echo "#define DL_LD \"$DL_LD\""	>> $CONFIG_H
    echo "#define DL_LDFLAGS \"$DL_LDFLAGS\"" >> $CONFIG_H
fi

# Include STCL (tcl/tk) support, if requested
if [ "$WITH_TCL" ]; then
    LIBDIR=/usr/lib
    # XXX check LP64 / LP64_NATIVE
    if [ "$MACHINE" = x86_64 -a -d /usr/lib64 ]; then
	LIBDIR=/usr/lib64
    fi
    # lib32 can exist as well, but we don't have a --lp32 option!

    TCL_CONF_DIRS="/usr/local/lib $LIBDIR /usr/share/tcltk /System/Library/Frameworks/Tcl.framework"
    TCL_MAJOR_VERSION=0
    TCL_MINOR_VERSION=0
    TCONF=

    for CONF in `find $TCL_CONF_DIRS -name tclConfig.sh 2>/dev/null`; do
	MAJ=`. $CONF; echo $TCL_MAJOR_VERSION`
	MIN=`. $CONF; echo $TCL_MINOR_VERSION`
	if [ "$MAJ" -gt "$TCL_MAJOR_VERSION" -o \
	     "$MAJ" -eq "$TCL_MAJOR_VERSION" -a \
	     "$MIN" -gt "$TCL_MINOR_VERSION" ]; then
	    TCONF=$CONF
	    # XXX validate paths in $CONF file?
	    TCL_MAJOR_VERSION=$MAJ
	    TCL_MINOR_VERSION=$MIN
	fi
    done

    if [ "$TCONF" ]; then
	echo "using Tcl version $MAJ.$MIN" 1>&2
	# Tk (loads as package in 8.4+)
	if [ "$WITH_TK" -a \( $MAJ -lt 8 -o $MAJ -eq 8 -a $MIN -lt 4 \) ]; then
	    echo 'use Tcl 8.4 or later for Tk' 1>&2
	    exit 1
	fi

	echo '#define PML_STCL'			>> $CONFIG_H
	(
	    . $TCONF
	    echo "ADD_LDFLAGS([$TCL_LIB_SPEC])"
	    echo 'ADD_OBJS([stcl.o])'
	    echo 'ADD_SRCS([$(STCL_C)])'
	    echo "ADD_CPPFLAGS([$TCL_INCLUDE_SPEC])"
	) >> $CONFIG_M4
    else
	echo "Tcl not found (may be due to no 'tclX.Y-dev' package)" 1>&2
	exit 1
    fi
fi

if [ ! "$WITHOUT_NDBM" ]; then
    DBM_FRAG='DBM *db = dbm_open("", 0, 0)'
    if [ "$FORCE_SDBM" ]; then
	# bypass checks
	USE_SDBM=1
    elif config/check ndbm.h "$DBM_FRAG"; then
	echo "using ndbm.h for DBM support" 2>&1
	echo '#define HAVE_NDBM_H'		>> $CONFIG_H
    elif config/check ndbm.h "$DBM_FRAG" -ldb; then
	echo "using ndbm.h for DBM support (with -ldb)" 2>&1
	echo '#define HAVE_NDBM_H'		>> $CONFIG_H
	echo 'ADD_LDFLAGS([-ldb])'		>> $CONFIG_M4
    elif config/check db.h "$DBM_FRAG" '-ldb -DDB_DBM_HSEARCH'; then
	# works with libdb4.8
	echo "using db.h for DBM support (with -ldb)" 2>&1
	echo '#define DB_DBM_HSEARCH 1'		>> $CONFIG_H
	echo '#define HAVE_DB_H'		>> $CONFIG_H
	echo 'ADD_LDFLAGS([-ldb])'		>> $CONFIG_M4
    else
	for COMPAT in '' ' -lgdbm_compat'; do
	    LIBS="-lgdbm$COMPAT"
	    if config/check ndbm.h "$DBM_FRAG" "$LIBS"; then
		# cygwin
		USE_GDBM=1
		echo "using ndbm.h for DBM support (with $LIBS)" 2>&1
		echo '#define HAVE_NDBM_H'	>> $CONFIG_H
		echo "ADD_LDFLAGS([$LIBS])"	>> $CONFIG_M4
		break
	    elif config/check gdbm/ndbm.h "$DBM_FRAG" "$LIBS"; then
		USE_GDBM=1
		echo "using gdbm/ndbm.h for DBM support (with $LIBS)" 1>&2
		echo '#define HAVE_GDBM_SLASH_NDBM_H'	>> $CONFIG_H
		echo "ADD_LDFLAGS([$LIBS])"	>> $CONFIG_M4
		break
	    elif config/check gdbm-ndbm.h "$DBM_FRAG" "$LIBS"; then
		USE_GDBM=1
		echo "using gdbm-ndbm.h for DBM support (with $LIBS)" 1>&2
		echo '#define HAVE_GDBM_DASH_NDBM_H'	>> $CONFIG_H	
		echo "ADD_LDFLAGS([$LIBS])"	>> $CONFIG_M4
		break
	    fi
	done
	if [ ! "$USE_GDBM" ]; then
	    USE_SDBM=1
	fi
    fi

    echo '#define PML_NDBM'		>> $CONFIG_H
    echo 'ADD_OBJS([ndbm.o])'		>> $CONFIG_M4
    echo 'ADD_SRCS([$(NDBM_C)])'	>> $CONFIG_M4

    if [ "$USE_SDBM" ]; then
	echo "using SDBM for DBM support" 1>&2
	echo 'ADD_CPPFLAGS([-Ilib/sdbm])' >> $CONFIG_M4
	echo 'ADD_OBJS([sdbm.o])'	>> $CONFIG_M4
	echo 'ADD_SRCS([$(SDBM_C)])'	>> $CONFIG_M4
	echo 'ADD_OBJS([sdbm_hash.o])'	>> $CONFIG_M4
	echo 'ADD_SRCS([$(SDBM_HASH_C)])' >> $CONFIG_M4
	echo 'ADD_OBJS([sdbm_pair.o])'	>> $CONFIG_M4
	echo 'ADD_SRCS([$(SDBM_PAIR_C)])' >> $CONFIG_M4
	echo '#define HAVE_SDBM_H'	>> $CONFIG_H
	echo '#define DUFF'		>> $CONFIG_H
    fi
fi

# Include GNU readline support, if found
if [ ! "$WITHOUT_READLINE" ]; then
    RL_INC='stdio.h readline/readline.h readline/history.h'
    RL_FRAG='readline("?")'
    if config/check "$RL_INC" "$RL_FRAG" -lreadline; then
	RL_LIBS="-lreadline"
    elif config/check "$RL_INC" "$RL_FRAG" '-lreadline -lncurses'; then
	RL_LIBS="-lreadline -lncurses"
    fi
    if [ "x$RL_LIBS" != x ]; then
	echo "using readline (with $RL_LIBS)" 1>&2
	echo 'ADD_OBJS([readline.o])'		>> $CONFIG_M4
	echo 'ADD_SRCS([$(READLINE_C)])'	>> $CONFIG_M4
	echo "ADD_LDFLAGS([$RL_LIBS])"		>> $CONFIG_M4
	echo '#define PML_READLINE'		>> $CONFIG_H
    fi
fi

if [ -c /dev/random ]; then
    echo 'using /dev/random for SRANDOMDEV()' 1>&2
    echo '#define HAVE_DEV_RANDOM'		>> $CONFIG_H
fi

# Include COM support under Cygwin!
# XXX missing symbols under Windows 7
if [ "$CYGWIN" -a ! "$WITHOUT_COM" -a -f $INCDIR/w32api/objbase.h ]; then
    echo 'using Win32 COM support' 1>&2
    echo 'ADD_OBJS([com.o])'			>> $CONFIG_M4
    echo 'ADD_SRCS([$(COM_CPP)])'		>> $CONFIG_M4
    if grep wcschr $INCDIR/wchar.h >/dev/null 2>&1; then
	echo '#define USE_WCHAR_H'		>> $CONFIG_H
    else
	# XXX look in $INCDIR/w32api??
	echo '#define NEED_WCSCHR'		>> $CONFIG_H
    fi
    if grep CoGetObject $INCDIR/w32api/objbase.h >/dev/null; then
	echo '/* found CoGetObject in w32api/objbase.h */' >> $CONFIG_H
    else
	echo '#define NEED_COGETOBJECT'		>> $CONFIG_H
    fi
    echo '#define PML_COM'			>> $CONFIG_H
    # -L/usr/lib/w32api needed on Windows 7 (thanks to Robert Shanley!)
    echo 'ADD_LDFLAGS([-L/usr/lib/w32api -lole32 -luuid -loleaut32])' >> $CONFIG_M4
    # needed w/ gcc 3.3.1 cygwin 1.5.9 (0.122/4/2)
    echo 'ADD_LDFLAGS([-lstdc++])'		>> $CONFIG_M4
fi

if [ "$CFLAGS" ]; then
     # stolen from getconf handling... make into function?
     echo 'using CFLAGS environment variable' 1>&2
     echo '/* from CFLAGS: */' >> $CONFIG_H
     for X in $CFLAGS; do
	 case "$X" in
	 -D*)
	     echo "X$X" | \
		 sed -e 's/^X-D/#define /' -e 's/=/ /' >> $CONFIG_H
	     ;;
	 -U*)
	     echo "X$X" | \
		 sed -e 's/^X-U/#undef /' -e 's/=/ /' >> $CONFIG_H
	     ;;
	 *)
	     echo 'ADD_CFLAGS(['$X'])'		>> $CONFIG_M4
	     ;;
	 esac
     done
fi
if [ "$CPPFLAGS" ]; then
    # stolen from getconf handling... make into function?
    echo 'using CPPFLAGS environment variable' 1>&2
    echo '/* from CPPFLAGS: */' >> $CONFIG_H
    for X in $CPPFLAGS; do
	case "$X" in
	-D*)
	    echo "X$X" | \
		sed -e 's/^X-D/#define /' -e 's/=/ /' >> $CONFIG_H
	    ;;
	-U*)
	    echo "X$X" | \
		sed -e 's/^X-U/#undef /' -e 's/=/ /' >> $CONFIG_H
	    ;;
	*)
	    echo 'ADD_CPPFLAGS(['$X'])'		>> $CONFIG_M4
	    ;;
	esac
    done
fi
if [ "$LDFLAGS" ]; then
	echo 'using LDFLAGS environment variable' 1>&2
	echo 'ADD_LDFLAGS(['$LDFLAGS'])'	>> $CONFIG_M4
fi

## check if pow() available? use dummy/expops.c if not?

# avoid:
# Solaris 8 /usr/sbin/install
# AIX /bin/install
# SysV /etc/install, /usr/sbin/install
# SunOS /usr/etc/install
# IRIX /sbin/install

# should always find ./install-sh
for DIR in /usr/bin /usr/ucb /usr/local/bin /bin .; do
    for INSTALL in install ginstall scoinst install-sh; do
	IPATH=$DIR/$INSTALL
	if [ -d $IPATH ]; then
	    continue
	fi
	if [ -x $IPATH ]; then
	    # avoid broken AIX installer
	    # this test blows on cygwin, where -x /usr/bin/install
	    # succeeds for /usr/bin/install.exe, but grep on
	    # /usr/bin/install fails, so send stderr to /dev/null
	    if grep dspmsg $IPATH >/dev/null 2>&1; then
		continue
	    fi
	    echo "using $IPATH for make install" 1>&2
	    echo "INSTALL=$IPATH"		>> $CONFIG_M4
	    break 2
	fi
    done
done

################################
# should be last thing done;

if [ -f local-config ]; then
    echo 'using local-config' 1>&2
    echo '################'			>> $CONFIG_M4
    echo '# local-config:'			>> $CONFIG_M4
    cat local-config				>> $CONFIG_M4
    echo '# end local-config'			>> $CONFIG_M4
    echo '################'			>> $CONFIG_M4
else
    echo '# if the file local-config existed it would have been incorporated here' >> $CONFIG_M4
fi

################

if [ -f local-defines ]; then
    echo 'using local-defines' 1>&2
    echo '/********************'		>> $CONFIG_H
    echo ' * local-defines:'			>> $CONFIG_H
    echo ' */'					>> $CONFIG_H
    cat local-defines				>> $CONFIG_H
    echo '/*'					>> $CONFIG_H
    echo ' * end local-defines'			>> $CONFIG_H
    echo ' ********************/'		>> $CONFIG_H
else

    echo ''					>> $CONFIG_H
    echo '/* if the file local-defines existed it would have been incorporated here */' >> $CONFIG_H
fi

if [ ! "$DEBUG" ]; then
    if [ $CONFIG_H != config.h ]; then
	mv -f $CONFIG_H config.h
    fi
    if [ $CONFIG_M4 != config.m4 ]; then
	mv -f $CONFIG_M4 config.m4
    fi
    if [ $VERSION_H != version.h ]; then
	mv -f $VERSION_H version.h
    fi
fi
