#!/bin/sh

# $Id: autoconf,v 1.40 1997/08/01 18:26:19 phil Exp $
# figure out which snobol config file to use
# Phil Budne
# August 24, 1996

# on most system gcc is preferred if it can be found
WANTGCC=true

while [ $# -gt 0 ]; do
	case $1 in
	--add-cppflags=*)
		echo 'ADD_CPPFLAGS('`echo $1 | sed 's/.*=//'`')'
		;;
	--add-ldflags=*)
		echo 'ADD_LDFLAGS('`echo $1 | sed 's/.*=//'`')'
		;;
	--add-objs=*)
		echo 'ADD_OBJS('`echo $1 | sed 's/.*=//'`')'
		;;
	--add-opt=*)
		echo 'ADD_OPT('`echo $1 | sed 's/.*=//'`')'
		;;
	--add-srcs=*)
		echo 'ADD_SRCS('`echo $1 | sed 's/.*=//'`')'
		;;
	--force-gcc)
		FORCEGCC=true
		#WANTGCC=true
		;;
	--force-memmove)
		FORCE_MEMMOVE=true
		;;
	--no-dlopen|--no-dynlib)
		NO_DLOPEN=true
		;;
	--no-gcc)
		WANTGCC=
		#FORCEGCC=
		;;
	--opt=*)
		echo 'OPT='`echo $1 | sed 's/.*=//'`
		;;
	*)
		echo "options:" 1>&2
		echo "	--add-cppflags=STR --add-ldflags=STR --add-objs=STR" 1>&2
		echo "	--add-opt=STR --add-srcs=STR" 1>&2
		echo "	--force-gcc --force-memmove" 1>&2
		echo "	--no-dlopen --no-dynlib --no-gcc" 1>&2
		echo "	--opt=STR" 1>&2
		exit 1
		;;
	esac
	shift
done

GUESS=./config.guess
if [ ! -x $GUESS ]; then
	echo missing config.guess script 1>&2
	exit 1
fi

SYS=`$GUESS 2>/dev/null || echo unknown`

# run hostname from subshell to redirect shell "not found message"
echo "# config generated "`date` on `(hostname) 2>/dev/null || uname -n`" ($SYS)"

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

	# didn't find gcc in PATH; check if CC is gcc
	if [ ! "$GCCPATH" ]; then
		TMP=conf$$
	# vanilla bsd43 systems can't use <<- AND EOF must be in col 0!
		sed 's/^		//' << EOF > $TMP.c
		main() {
		#ifdef __gcc__
			exit(0);
		#else
			exit(1);
		#endif
		}
EOF
		if ${CC-cc} -o $TMP $TMP.c && ./$TMP; then
			echo 'regular C compiler is gcc' 1>&2
			echo "# cc is gcc"
			GCCPATH="${CC-cc}"
		fi
		rm -f $TMP $TMP.c
	fi
fi

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

case "$SYS" in
*-sun-sunos4*)
	# empty file! (but could have stuff for SUNpro?)
	CONF=sunos4
	case `arch -k` in
	sun4m)
		echo 'define([__SPARCV8__])dnl'
		;;
	esac
	# check for SUNpro compiler? where??
	;;

sparc-sun-solaris*)
	CONF=posix

	# output defines for sparc version number
	# could be used to determine compiler switches
	MACH=`(uname -m || echo 'unknown') 2>/dev/null`
	case $MACH in
	sun4[md])
		# not 100% true; "Ross" modules were sparc v7
		echo 'define([__SPARCV8__])dnl'
		;;
	sun4u)
		echo 'define([__SPARCV9__])dnl'
		;;
	esac

	# prefer SUNpro, if available
	if [ ! "$FORCEGCC" -a -f /opt/SUNWspro/bin/cc ]; then
		echo using sunpro C 1>&2
		CCPATH=/opt/SUNWspro/bin/cc
		WANTGCC=
		echo 'define([__SUNPRO__])dnl'
		CCVERS=-V
	fi
	;;

*-solaris*)
	# here for x86 (and ppc)
	CONF=posix

	# on x86 gcc beats SUNpro
	# so only use SUNpro if gcc not available
	if [ ! "$GCCPATH" ]; then
		if [ -f /opt/SUNWspro/bin/cc ]; then
			echo using sunpro C 1>&2
			CCPATH=/opt/SUNWspro/bin/cc
			WANTGCC=
			echo 'define([__SUNPRO__])dnl'
			CCVERS=-V
		fi
		# XXX else (warn? fatal err??) no compiler found????
	fi
	;;

*-irix*)
	CONF=posix
# does not work (yet);
#	if [ `uname` = IRIX64 ]; then
#		CONF=irix64
#	fi
	;;

*-linux*)
	# detect a.out systems? try bsd/load.c?
	# on alpha include config/alpha.m4???
	CONF=posix
	;;

alpha-dec-osf*)
	CONF=osf1-alpha
	;;

hppa*-hpux*)
	CONF=hpux-pa
	# hpux 9.05 m4 predefines hpux, which screws up include(hpux)!!
	echo 'undefine([hpux])'

	# look for unbundled ansi C; prefer it to gcc
	if [ ! "$FORCEGCC" ]; then
		if [ -x /bin/c89 -o -x /usr/bin/c89 ]; then
			echo 'using c89' 1>&2
			CCPATH=c89
			echo 'define([__C89__])dnl'
			WANTGCC=
		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
	;;

m68*-hpux*)
	# works on HP-UX 8.0 on 420T w/ gcc
	echo 'undefine([hpux])'
	CONF=hpux
	;;

rs6000-ibm-aix*|powerpc-ibm-aix*)
	CONF=aix-rs6000
	# prefer regular cc to gcc (only use gcc if forced)
	WANTGCC="$FORCEGCC"
	;;

################ BSD 4.4 variants

alpha-*-netbsd*)
	CONF=netbsd-alpha
	;;

*-freebsd*)
	# empty file!
	CONF=bsd44
	;;

*-netbsd*)
	# empty file!
	CONF=bsd44
	;;

*-bsdi*)
	# empty file!
	CONF=bsd44
	;;

*bsd4.4)
	# empty file!
	CONF=bsd44
	;;

*-openbsd*)
	# empty file!
	CONF=bsd44
	;;

################ BSD 4.3 variants

romp-ibm-aos)
	CONF=bsd43
	;;

m68k-hp-bsd)
	# Mt Xinu 4.3 on hp300
	echo 'ADD_OBJS([isnan.o])'
	echo 'ADD_SRCS([$(ISNAN_C)])'
	CONF=bsd43
	;;

*bsd*)
	CONF=bsd43
	;;

*-sony-newsos*)
	# empty file!
	CONF=sony_news
	;;

*-next-nextstep*)
	# only tested with nextstep3, but try it anyway!
	CONF=nextstep3
	;;

mips-dec-ultrix*)
	# 4.3BSD with a COFF
	CONF=ultrix-mips
	;;

vax-dec-ultrix*)
	CONF=bsd43
	;;

################ ADD NEW SYSTEMS HERE

*)
	echo 'unknown system; guessing...' 1>&2
	if [ -f /vmunix ]; then
		echo 'found /vmunix; using BSD 4.3 config' 1>& 2
		CONF=bsd43
	else
		if [ -f /usr/include/unistd.h ]; then
			echo 'found unistd.h; using POSIX.1 config' 1>&2
			CONF=posix
		else
			# XXXX try compiling ANSI lib files???
			echo 'defaulting to generic config' 1>&2
			CONF=generic
		fi
	fi
	;;
esac

if [ "$GCCPATH" -a "$WANTGCC" ]; then
	echo using gcc 1>&2
	CCPATH="$GCCPATH"
	echo 'include(config/gcc.m4)'
	CCVERS=-v
fi

if [ "$CCPATH" ]; then
	echo "CC=$CCPATH"
fi

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

if [ "$CONF" ]; then
	echo using $CONF config file 1>&2
	echo 'include(config/'$CONF'.m4)'
fi

if [ -f /usr/include/unistd.h ]; then
	echo 'using POSIX unistd.h' 1>&2
	echo 'HAVE_UNISTD_H=-DHAVE_UNISTD_H'
else
	echo 'TERM_C=$(SRCDIR)lib/generic/term.c'
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'
fi

# prefer uname(2)
if [ -f /usr/include/sys/utsname.h ]; then
	echo 'using uname(2) for system name information' 1>&2
else
	echo 'using "'$SYS'" for system name information' 1>&2
	echo 'SYS_C=$(SRCDIR)lib/generic/sys.c'
	echo "CONFIG_GUESS=-DCONFIG_GUESS='"'"'$SYS'"'"'"
fi

# prefer v7/berkeley string functions
if [ -f /usr/include/strings.h ] && \
    grep index /usr/include/strings.h >/dev/null; then
	echo 'using BSD string functions' 1>&2
else
	echo 'using ANSI string functions' 1>&2
	echo 'ADD_CPPFLAGS([-DANSI_STRINGS])'
	if grep bcopy /usr/include/string.h >/dev/null; then
		echo 'using system provided bcopy() function' 1>&2
	elif test "$FORCE_MEMMOVE" && \
	    grep memmove /usr/include/string.h >/dev/null; then
		# only system with no bcopy() and with memmove()
		# is Solaris 2.4, and our bcopy() beats memmove()?!
		echo 'using system provided memmove() function' 1>&2
		echo 'ADD_CPPFLAGS([-DUSE_MEMMOVE])'
	else
		echo 'using private bcopy() function' 1>&2
		echo 'ADD_OBJS([bcopy.o])'
		echo 'ADD_SRCS([$(BCOPY_C)])'
	fi
	if grep bzero /usr/include/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])'
		echo 'ADD_SRCS([$(BZERO_C)])'
	fi
fi

if [ -f /usr/include/sys/socket.h ]; then
	echo 'using sockets for TCP' 1>&2
	# add network libraries if needed
	if [ -f /usr/lib/libsocket.a ]; then
		# SCO, SunOS5
		echo 'using libsocket' 1>&2
		echo 'ADD_LDFLAGS([-lsocket])'
		if [ `uname` = SunOS ]; then
			echo 'using libnsl' 1>&2
			echo 'ADD_LDFLAGS([-lnsl])'
		fi
	fi
else
	echo 'no sockets; TCP not available' 1>&2
	echo 'INET_C=$(SRCDIR)lib/dummy/inet.c'
fi

if [ -f /usr/include/termios.h ]; then
	echo 'using termios.h for tty modes' 1>&2
elif [ -f /usr/include/termio.h ]; then
	echo 'using termio.h for tty modes' 1>&2
	echo 'ADD_CPPFLAGS([-DUSE_TERMIO])'
elif [ -f /usr/include/sgtty.h ]; 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'
elif [ -f /usr/include/sys/ttyio.h ]; then
	echo 'using sys/ttyio.h for tty modes' 1>&2
	echo 'TTY_C=$(SRCDIR)lib/bsd/tty.c'
	echo 'ADD_CPPFLAGS([-DUSE_TTYIO])'
else
	echo 'no tty support found' 1>&2
	echo 'TTY_C=$(SRCDIR)lib/dummy/tty.c'
fi

# Look for getopt definition in stdio.h include file which might cause
# compilation problems 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!

if grep getopt /usr/include/stdio.h >/dev/null 2>&1; then
	# avoid possible compilation problems!
	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])'
	echo 'ADD_SRCS([$(GETOPT_C)])'
fi

# look for dynamic library support for "load";
# overrides "dummy" and "bsd" style, unless given --no-dlopen
if [ ! "$NO_DLOPEN" ]; then
	# look for sun/unix98 style dlopen()
	if [ -f /usr/include/dlfcn.h ]; then
		echo 'using dlopen() for LOAD() support' 1>&2
		echo 'LOAD_C=$(SRCDIR)lib/unix98/load.c'
		echo 'SNOLIB_FILE=snolib.so'

		# on suns (both SunOS 4 & 5) must explicitly link with -ldl
		# libdl present, but not needed on IRIX 5
		# safer to include it than to have link fail
		for x in /usr/lib/libdl* /lib/libdl*; do
			if [ -f $x ]; then
				echo 'using -ldl' 1>&2
				echo 'ADD_LDFLAGS(-ldl)'
				break
			fi
		done
	else
		case "$SYS" in
		*-hp-hpux*)
			if [ -f /usr/include/dl.h ]; then
				echo 'using shl_load() for LOAD() support' 1>&2
				echo 'LOAD_C=$(SRCDIR)lib/hpux/load.c'
				echo 'SNOLIB_FILE=snolib.sl'
				echo 'ADD_LDFLAGS(-ldld)'
			fi
		esac
	fi
fi

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

if [ -f local-config ]; then
	echo 'using local-config' 1>&2
	echo '# local-config:'
	cat local-config
	echo '# end local-config'
else
	echo '# if the file local-config exists it will be included here'
fi
