#!/bin/sh
########################################################################
#  TinyFugue - programmable mud client
#  Copyright (C) 1994 Ken Keys
#
#  TinyFugue (aka "tf") is protected under the terms of the GNU
#  General Public License.  See the file "COPYING" for details.
#
#  DO NOT EDIT THIS FILE.
#  Any configuration changes should be made to the Config file.
########################################################################

#
# TinyFugue autoconfiguration
# Descriptor 4 is config.h, descriptor 5 is mf.vars.
# 

### A few portability notes.
# Some old shells don't support the ':' in ${VAR:-default} syntax.
# Some old shells don't support shell functions.
# Some versions of 'test' have the wrong precedence for unary operators,
#   so we use the shell's '&&' and '||' operators instead of '-a' and '-o'.
# 4.3 BSD 'test' does not have '-x'.  Use '-f' or '-r' instead.
# Some stupid compilers return 0 even if a child ld returns nonzero,
#   so we explicitly test for the existance of the executable.

### Some notes on predefined preprocessor identifiers.
# M_UNIX	SCO UNIX
# hpux		HP/UX
# sequent	Dynix
# _SEQUENT_	Dynix/ptx


rm -f Makefile

### Initialize variables, just in case they're already defined.

ANSI_C=0
BINDIR=''
# CC=''		;# Use the value from the environment if there is one.
CCFLAGS='-O'
# CPP=''	;# Use the value from the environment if there is one.
TFVER="$*"
EXE=''
FLAGS=''
GNU_C=0
LIBC=''
LIBDIR=''
LIBS=''
MAILDIR=''
# MAKE=''	;# Use the value from the environment if there is one.
MANPAGE=''
MANTYPE='cat'
MODE=''
POSIX=0
QUIET=''
REGEXP_ERR='&1'
SOCKS=''
STRIP='strip'
SYMLINK=''
SYSTEM=''
TERMINAL='TERMCAP'
TTYDRIVER=''

TFVERSION=`egrep 'TinyFugue version' main.c | sed 's/^.*\"\(.*\)\".*$/\1/'`
if [ -z "$USER" ]; then USER=$LOGNAME; fi

### Read in user definitions.
if [ -r ../Config ]; then . ../Config; fi

### I keep development definitions in dev/config so I don't have to keep
### editing the default version.
if [ -r ../dev/config ]; then . ../dev/config; fi

echo '#### This file was automatically generated by autoconfig.' >&5
echo >&5
echo "TFVERSION  = \"${TFVERSION}\"" >&5

cat >&4 <<EOF
/* config.h
 * This file was automatically generated by autoconfig.
 */
EOF
echo "#define UNAME      \"`{ uname -sr || uname -a ; } 2>/dev/null`\"" >&4

echo "Configuring $TFVERSION"
echo

if [ -z "$QUIET" ] && echo $TFVERSION | egrep "alpha" >/dev/null 2>&1; then
cat <<EOF
This is an alpha version, and so may not be fully tested on some platforms.
With the new features comes the possibility of new bugs.  If you don't want
to deal with this, get the latest beta version.

EOF
fi

if [ -z "$QUIET" ]; then
cat <<EOF
TinyFugue is the result of several years of voluntary work.  If you use it,
please send a donation to the author.  A reasonable amount might be \$10, but
send whatever you think is appropriate.  Your donation will cover past time and
effort put into TF, as well as possible future support and maintainance.
Be sure to include your name and email address with your donation sent to:

  Ken Keys
  1820 Cottonwood Ave.
  Carlsbad, CA 92009
  USA

EOF
fi


### Figure out installation locations.

if [ -z "$BINDIR" ]; then
    if [ -w /usr/local/bin ]; then
        BINDIR=/usr/local/bin
    else
        [ -d $HOME/bin ] && BINDIR="~$USER/bin" || BINDIR="~$USER"
    fi
fi

if [ -z "$LIBDIR" ]; then
    if [ -w /usr/local/lib ]; then
        LIBDIR=/usr/local/lib/tf.lib
    else
        [ -d $HOME/lib ] && LIBDIR="~$USER/lib/tf.lib" || LIBDIR="~$USER/tf.lib"
    fi
fi

[ -z "$MODE" ]      && MODE=755
[ -z "$EXE" ]       && EXE=${BINDIR}/tf


### Sanity check for filenames (some people do pretty stupid things)
# The "${SYMLINK}/" is just to make the grep return false if SYMLINK is empty.

if echo "$EXE"         | egrep "^[^/~]" >/dev/null 2>&1 ||
   echo "$LIBDIR"      | egrep "^[^/~]" >/dev/null 2>&1 ||
   echo "${SYMLINK}/"  | egrep "^[^/~]" >/dev/null 2>&1
then
   echo "Don't install files with relative paths."
   echo "Edit Config and try again."
   exit 2
fi

# Don't allow installation in the build tree (people have actually tried this).
# The cd;pwd is needed to normalize the directory name in case of links, and
# the csh is needed to expand '~'.
DIR1=`csh -f -c "echo $EXE"     | sed 's;/[^/]*$;;'`
DIR1=`cd $DIR1; pwd`
DIR2=`csh -f -c "echo $LIBDIR"  | sed 's;/[^/]*$;;'`
DIR2=`cd $DIR2; pwd`
DIR3=`csh -f -c "echo $SYMLINK" | sed 's;/[^/]*$;;'`
DIR3=`cd $DIR3; pwd`
BUILDTREE=`cd ..; pwd`
if echo "${DIR1}/" | egrep "^${BUILDTREE}/" >/dev/null 2>&1 ||
   echo "${DIR2}/" | egrep "^${BUILDTREE}/" >/dev/null 2>&1 ||
   echo "${DIR3}/" | egrep "^${BUILDTREE}/" >/dev/null 2>&1
then
    echo
    echo "You can not install files in the build tree,"
    echo "${BUILDTREE}."
    echo "Edit Config and try again."
    exit 2
fi

### Find mail directory by looking at $MAIL, then looking in the usual places.

if [ -z "$MAILDIR" ]; then
    if [ -n "$MAIL" ]; then
#       Not everybody has dirname.  sigh.
        MAILDIR=`echo $MAIL | sed 's;/[^/]*$;;'`
    elif [ -d /usr/spool/mail ]; then
        MAILDIR="/usr/spool/mail"
    elif [ -d /var/spool/mail ]; then
        MAILDIR="/var/spool/mail"
    elif [ -d /usr/mail ]; then
        MAILDIR="/usr/mail"
    elif [ -d /var/mail ]; then
        MAILDIR="/var/mail"
    else
        MAILDIR=""
    fi
fi



### Confirm.

echo "TF binary will be installed as     $EXE"
if [ -n "$SYMLINK" ]; then
    echo "    with a symbolic link:          $SYMLINK"
fi
echo "Library, help, and utilities:      ${LIBDIR}/"
if [ -n "$MAILDIR" ]; then
    echo "Default mail directory:            ${MAILDIR}/"
else
    echo "Mail directory unknown.  TF will not check mail if MAIL is unset."
fi
if [ -n "$MANPAGE" ]; then
    echo "Man page will be installed in      $MANPAGE in $MANTYPE format"
else
    echo "Man page will not be installed."
fi
echo

echo 'To change these locations type "n" now and edit the Config file.'
while [ -z "$ans" ]; do
    echo 'Continue? (y/n)'
    read ans;
    case "$ans" in
    y|Y)  break ;;
    n|N)  rm -f Makefile config.h
          exit 1 ;;
    *)    ans=
          echo 'Please answer "y" or "n".'
          ;;
    esac
    done
echo; echo

echo "#define LIBDIR     \"${LIBDIR}\"" >&4
echo "#define MAILDIR    \"${MAILDIR}\"" >&4

echo "MAKE       = `csh -f -c \"echo ${MAKE}\"`" >&5
echo "EXE        = `csh -f -c \"echo ${EXE}\"`" >&5
echo "SYMLINK    = `csh -f -c \"echo ${SYMLINK}\"`" >&5
echo "LIBDIR     = `csh -f -c \"echo ${LIBDIR}\"`" >&5
echo "MAILDIR    = `csh -f -c \"echo ${MAILDIR}\"`" >&5
echo "MANPAGE    = `csh -f -c \"echo ${MANPAGE}\"`" >&5
echo "MANTYPE    = ${MANTYPE}" >&5
echo "MODE       = ${MODE}" >&5


### Compiler.
### We'll find extra needed libs later, so we don't need /usr/ucb/cc
### or /usr/ucb/cc-bsd like we used to.
### Use GNU C if it's available.
### Note that GNU C's -ansi option does not guarantee existance of ANSI
### headers or libraries, so we must still verify them seperately.

if [ -z "${CC}" ]; then
    if { gcc -v; } > /dev/null 2>&1; then
        CC='gcc'
    else
        CC='cc'
    fi
fi

echo "Will compile with: ${CC} ${CCFLAGS}"
echo "CC         = ${CC}" >&5

COM="${CC} ${CCFLAGS}"

### Figure out how to run preprocessor.
# Most compilers have an -E flag to run the preprocessor only.
# If -E doesn't work, try some common paths.

if [ -z "$CPP" ]; then
    echo "main() { exit(0);}" > test.c
    if ${CC} ${CCFLAGS} -E test.c >/dev/null; then
        CPP="${CC} -E"
    elif { cpp test.c; } >/dev/null 2>&1; then
        CPP='cpp'
    elif { /lib/cpp test.c; } >/dev/null 2>&1; then
        CPP='/lib/cpp'
    elif { /usr/lib/cpp test.c; } >/dev/null 2>&1; then
        CPP='/usr/lib/cpp'
    elif { /usr/ccs/bin/cpp test.c; } >/dev/null 2>&1; then
        CPP='/usr/ccs/bin/cpp'
    else
        echo "I can't figure out how to run the preprocessor."
        exit 1
    fi
fi
echo "Preprocessor: ${CPP}"

### Test for ANSI.

cat > test.c <<EOP
#ifdef __GNUC__
    extern gnuc;
#endif
#if __STDC__ - 0
    extern ansi;
#endif
EOP
${CPP} test.c >test.out
if [ $? != 0 ] ; then
    echo
    echo "Ack!  Your compiler doesn't work.  Maybe you used invalid flags."
    echo "I give up."
    exit 2
elif egrep 'gnuc' test.out >/dev/null 2>&1; then
    echo "Ah ha, ${CC} is GNU C."
    GNU_C=1
elif egrep 'ansi' test.out >/dev/null 2>&1; then
    echo "Cool, you have an ANSI compiler."
    ANSI_C=1
else
    echo "Your compiler doesn't appear to be ANSI."
fi


### Test for POSIX.
### If test.c fails to preprocess, unistd.h must be missing, and system is not
### POSIX.  Otherwise, test.out contains 'posix' if posix.
### Note: in general, it is usually better to test a specific feature than
### to rely on the system's claim of POSIX compliance.

cat > test.c <<EOP
#include <unistd.h>
#ifdef _POSIX_VERSION
    extern posix;
#else
#endif
EOP
if ${CPP} test.c > test.out 2>/dev/null; then
    echo "#define UNISTD_H <unistd.h>" >&4
    if egrep 'posix' test.out >/dev/null 2>&1; then
        echo "Right on, your system is POSIX."
        POSIX=1
    else
        echo "This doesn't appear to be a POSIX system."
        POSIX=0
    fi
else
    echo "#undef UNISTD_H" >&4
    echo "I can't find <unistd.h>.  The compiler may produce a lot of"
    echo "warnings, but you can probably ignore them."
    echo "This doesn't appear to be a POSIX system."
    POSIX=0
fi


### Make sure the PATH is sane.
# Ideally, this should be done before the compiler check.  But a compiler
# found now in the extended PATH won't be found in src/Makefile, because it
# won't inherit this PATH.
PATH="$PATH:/bin:/usr/bin:/usr/local/bin:/usr/ucb:/usr/local:/usr/lbin:/etc:/usr/new:/usr/new/bin:/usr/nbin:/usr/ccs/bin"


### This isn't required, but it helps.
if echo "#include <stdlib.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define STDLIB_H <stdlib.h>" >&4
    echo "Found <stdlib.h>."
else
    echo "#undef STDLIB_H" >&4
    echo "I can't find <stdlib.h>.  The compiler may produce a lot of"
    echo "    warnings, but you can probably ignore them."
fi

### Figure out which string header to include.
if echo "#include <string.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define STRING_H <string.h>" >&4
    echo "Found <string.h>."
elif echo "#include <strings.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define STRING_H <strings.h>" >&4
    echo "Found <strings.h>."
else
    echo "I can't find <string.h> or <strings.h>.  I give up."
    exit 1
fi

### SunOS doesn't define mem... in string.h (even though ANSI requires it).
if echo "#include <memory.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define MEMORY_H <memory.h>" >&4
    echo "Found <memory.h>."
else
    echo "#undef MEMORY_H" >&4
fi


### Find internet headers

if cat > test.c <<EOP
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
EOP
${CPP} test.c >/dev/null 2>&1; then
    echo "#define NETINET_IN_H <netinet/in.h>" >&4
    echo "Found <netinet/in.h>."

elif cat > test.c <<EOP
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/in.h>
EOP
${CPP} test.c >/dev/null 2>&1; then
    echo "#define NETINET_IN_H <sys/in.h>" >&4
    echo "Found <sys/in.h>."

elif cat > test.c <<EOP
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/netinet/in.h>
EOP
${CPP} test.c >/dev/null 2>&1; then
    echo "#define NETINET_IN_H <sys/inet/in.h>" >&4
    echo "Found <sys/netinet/in.h>."

else
    echo "I can't find <netinet/in.h>, <sys/in.h>, or <sys/netinet/in.h>."
    echo "  I'll use my own definitions.  Good luck."
    echo "#undef NETINET_IN_H" >&4
fi

### Find internet headers
if echo "#include <arpa/inet.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define ARPA_INET_H <arpa/inet.h>" >&4
    echo "Found <arpa/inet.h>."
elif echo "#include <sys/inet.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define ARPA_INET_H <sys/inet.h>" >&4
    echo "Found <sys/inet.h>."
else
    echo "#undef ARPA_INET_H" >&4
    echo "I can't find <arpa/inet.h> or <sys/inet.h>, but we should be okay."
fi

### Find netdb header
if echo "#include <netdb.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define HAVE_NETDB_H" >&4
    echo "Found <netdb.h>."
else
    echo "#define NO_NETDB" >&4
    echo "I can't find <netdb.h>."
    echo "    Hostname resolution and port service names will not be available."
fi

### Find pwd.h
if echo "#include <pwd.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo "#define HAVE_PWD_H" >&4
    echo "Found <pwd.h>."
else
    echo "/* #define HAVE_PWD_H */" >&4
    echo "I can't find <pwd.h>.  Filename '~user' expansion won't be supported."
fi

### Figure out how to do varadic args.
if [ "$ANSI_C" = "1" ] || [ "$GNU_C" = "1" ]; then
    if echo "#include <stdarg.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
        echo "#define HAVE_STDARG" >&4
        echo "Found <stdarg.h>."
    elif [ "$ANSI_C" = "1" ]; then
#       Workaround for AOS (IBM RT), possibly Domain/OS (Apollo).
#       Not tested very well; but, this case is rare.
        echo "Your compiler says it's ANSI, but <stdarg.h> fails.  Tsk tsk."
        echo "    I'm going to undefine __STDC__.  Cross your fingers."
        echo "#undef __STDC__" >&4
        echo "/* #define HAVE_STDARG */" >&4
    fi
else
    echo "/* #define HAVE_STDARG */" >&4
fi

### Figure out return type of signal handlers.
echo "Testing type of signal handlers..."
cat >test.c <<EOF
#include <sys/types.h>
#include <signal.h>
#ifdef signal
#undef signal
#endif
extern void (*signal())();
main() { exit(0); }
EOF
if ${COM} -c test.c >/dev/null 2>&1; then
    echo "Signal handlers return void."
    echo "#define RETSIG void" >&4
else
    echo "Assuming signal handlers return int."
    echo "#define RETSIG int" >&4
fi


### Test for time_t.
echo "Testing for time_t..."
cat >test.c <<EOF
#include <time.h>
extern time_t time();
main() { exit(0); }
EOF
if ${COM} -c test.c >/dev/null 2>&1; then
    echo "time() returns time_t."
    echo "#define TIME_T time_t" >&4
else
    echo "Assuming time() returns long."
    echo "#define TIME_T long" >&4
fi


### Test LIBS, to make sure user didn't screw it up.
if [ -n "$LIBS" ]; then
    if ${COM} libtest.o ${LIBS} >/dev/null && test -f a.out; then
        : okay
    else
        echo
        echo "Above problem may be due to Config: LIBS=\"$LIBS\""
        exit 1
    fi
fi


### Create a trivial object with which to test library linking.
### Much faster than starting from a .c each time.

echo "Creating linker test..."
echo "main(){ exit(0); }" > libtest.c
${COM} -c libtest.c >/dev/null 2>&1
rm libtest.c


### Look through the contents of the C library to see if certain functions
### are available.  Compiling test programs that use the functions would
### be "more correct", but also much slower.

if [ -z "$LIBC" ] || [ ! -f "$LIBC" ]; then
    if [ -r /lib/libc.a ]; then
        LIBC='/lib/libc.a'
    elif [ -r /lib/libc_s.a ]; then
        LIBC='/lib/libc_s.a'
    elif [ -r /usr/lib/libc.a ]; then
        LIBC='/usr/lib/libc.a'
    elif [ -r /usr/lib/libc_s.a ]; then
        LIBC='/usr/lib/libc_s.a'
    elif [ -r /usr/ccs/lib/libc.a ]; then
        LIBC='/usr/ccs/lib/libc.a'
    elif [ -r /usr/lib/libNeXT_s.a ]; then
        LIBC='/usr/lib/libNeXT_s.a'
    else
        LIBC=''
    fi
fi

if [ -n "$LIBC" ]; then
    echo "Examining ${LIBC}..."
    { nm $LIBC; } >libc.list 2>/dev/null
    if [ ! -s libc.list ]; then ar t $LIBC >libc.list 2>/dev/null; fi
fi
if [ ! -s libc.list ]; then
    echo "I can't read your C library!"
    echo "    I'll need to make some assumptions.  If I'm wrong, edit Config."
    rm -f libc.list; touch libc.list
fi

### Look for rand() or random().

if egrep '(^|[^a-z_])_?_?srandom([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "#define HAVE_RANDOM" >&4
    echo "Found random() in your C library."
elif egrep '(^|[^a-z_])_?_?srand([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "#define HAVE_RAND" >&4
    echo "Found rand() in your C library."
else
    echo "/* #define HAVE_RAND */" >&4
    echo "I can't find rand() or random().  Assuming you have rand()."
fi

### Look for bsearch().
if egrep '(^|[^a-z_])_?_?bsearch([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found bsearch() in your C library."
    echo "#define HAVE_BSEARCH" >&4
else
    echo "I can't find bsearch().  That's okay, I'll use my own."
    echo "/* #define HAVE_BSEARCH */" >&4
fi

### See if getcwd(), getwd(), or neither is available.  This isn't vital.

if egrep '(^|[^a-z_])_?_?getcwd([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found getcwd() in your C library."
    echo "#define HAVE_GETCWD" >&4
elif egrep '(^|[^a-z_])_?_?getwd([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found getwd() in your C library."
    echo "#define HAVE_GETWD" >&4
else
    echo "I can't find getwd() or getcwd().  No big deal."
    echo "/* #define HAVE_GETCWD */" >&4
fi

### Look for memcpy() or bcopy().

if egrep '(^|[^a-z_])_?_?memcpy([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found memcpy() in your C library."
    echo "#define HAVE_MEMCPY" >&4
elif egrep '(^|[^a-z_])_?_?bcopy([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found bcopy() in your C library."
    echo "#define HAVE_BCOPY" >&4
else
    echo "I can't find memcpy() or bcopy().  Good luck."
    echo "    I'll assume you have memcpy()."
    echo "/* #define HAVE_MEMCPY */" >&4
fi

### Look for strchr() or index().
### Note:  Solaris brain damage:  libtermcap uses [r]index() instead of
### str[r]chr().  So we check for index() even if we find strchr().

if egrep '(^|[^a-z_])_?_?strchr([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found strchr() in your C library."
    echo "#define HAVE_STRCHR" >&4
else
    echo "/* #define HAVE_STRCHR */" >&4
fi
if egrep '(^|[^a-z_])_?_?index([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found index() in your C library."
    echo "#define HAVE_INDEX" >&4
else
    echo "/* #define HAVE_INDEX */" >&4
fi

### Look for strstr().

if egrep '(^|[^a-z_])_?_?strstr([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found strstr() in your C library."
    echo "#define HAVE_STRSTR" >&4
else
    echo "I can't find strstr().  That's okay, I'll use my own."
    echo "/* #define HAVE_STRSTR */" >&4
fi

### Look for strcasecmp().

if egrep '(^|[^a-z_])_?_?strcasecmp([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found strcasecmp() in your C library."
    echo "#define HAVE_STRCASECMP" >&4
    { egrep strcasecmp /usr/include/string.h; } >/dev/null 2>&1 ||
        echo "extern int strcasecmp();" >&4
else
    echo "I can't find strcasecmp().  That's okay, I'll use my own."
    echo "/* #define HAVE_STRCASECMP */" >&4
fi

### Look for strerror().

if egrep '(^|[^a-z_])_?_?strerror([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found strerror() in your C library."
    echo "#define HAVE_STRERROR" >&4
else
    echo "I can't find strerror().  That's okay, I'll use my own."
    echo "/* #define HAVE_STRERROR */" >&4
fi

### Look for strtol().

if egrep '(^|[^a-z_])_?_?strtol([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found strtol() in your C library."
    echo "#define HAVE_STRTOL" >&4
else
    echo "I can't find strtol().  That's okay, I'll use my own."
    echo "/* #define HAVE_STRTOL */" >&4
fi

### Look for strftime().

if egrep '(^|[^a-z_])_?_?strftime([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found strftime() in your C library."
    echo "#define HAVE_STRFTIME" >&4
elif ${COM} libtest.o -lintl >/dev/null 2>&1 && test -f a.out; then
    # SCO keeps strftime() in -lintl.
    echo "Will link -lintl, for strftime()."
    echo "#define HAVE_STRFTIME" >&4
    LIBS="$LIBS -lintl"
else
    echo "I can't find strftime().  time_format will not be supported."
    echo "/* #define HAVE_STRFTIME */" >&4
fi


### Library testing.
# If libfoo.a make references to libbar.a, the order must be "-lfoo -lbar";
# it's okay to test -lbar alone, but to test -lfoo we must use "-lfoo -lbar".
# So we test incrementally, building the list from right to left.
# For Dynix/ptx, order must be: -lsocket -linet -lnsl
# For SysV, -lsocket test needs -lnsl.

### If -lbsd exists, we might need it (and it shouldn't make a difference if
### we don't need it).

if ${COM} libtest.o -lbsd ${LIBS} >/dev/null 2>&1 && test -f a.out; then
    echo "Will link with -lbsd."
    LIBS="-lbsd $LIBS"
fi

### If gethostbyname() isn't in libc, look for -lnsl_s or -lnsl.
# note: can't assume existance of libs implies need; must check libc first. (?)

if egrep '(^|[^a-z_])_?_?gethostbyname([^a-z]|$)' libc.list >/dev/null 2>&1; then
    echo "Found gethostbyname() in your C library."
elif ${COM} libtest.o -lnsl_s ${LIBS} >/dev/null 2>&1 && test -f a.out; then
    echo "Will link with -lnsl_s."
    LIBS="-lnsl_s $LIBS"
elif ${COM} libtest.o -lnsl ${LIBS} >/dev/null 2>&1 && test -f a.out; then
    echo "Will link with -lnsl."
    LIBS="-lnsl $LIBS"
else
    echo "Warning: can't find gethostbyname() or a library that might have it."
    echo "/* warning: autoconfig couldn't find gethostbyname() */" >&4
fi

### If -linet exists, assume we need it.
# note: -linet may require -lnsl (Dynix/ptx), so ordering is important.

if ${COM} libtest.o -linet ${LIBS} >/dev/null 2>&1 && test -f a.out; then
    echo "Will link with -linet."
    LIBS="-linet $LIBS"
fi

### If -lnet exists, assume we need it.

if ${COM} libtest.o -lnet ${LIBS} >/dev/null 2>&1 && test -f a.out; then
    echo "Will link with -lnet."
    LIBS="-lnet $LIBS"
fi

## If -lsocket exists, assume we need it (for socket(), connect(), etc)
if ${COM} libtest.o -lsocket ${LIBS} >/dev/null 2>&1 && test -f a.out; then
    echo "Will link with -lsocket."
    LIBS="-lsocket $LIBS"
fi

### Test for SOCKS firewall proxy server.
if [ -n "$SOCKS" ]; then
    # SOCKS uses res_init(), so we need -lresolv if there is one.
    if ${COM} libtest.o -lresolv ${LIBS} >/dev/null 2>&1 && test -f a.out; then
        echo "Will link with -lresolv."
        LIBS="-lresolv $LIBS"
    fi
    if ${COM} libtest.o -lsocks ${LIBS} >/dev/null 2>&1 && test -f a.out; then
        echo "Will link with -lsocks."
        LIBS="-lsocks $LIBS"
        echo "#define SOCKS" >&4
        if [ -n "$SOCKS_NONBLOCK" ]; then
            echo "#define SOCKS_NONBLOCK" >&4
        fi
    else
        echo "Can't find SOCKS library.  You may need to specify its location"
        echo "  with an -L linker option in LIBS."
        exit 1
    fi
fi


### test termcap.

if [ "$TERMINAL" = "TERMCAP" ]; then
    if ${COM} libtest.o -ltermcap ${LIBS} >/dev/null 2>&1 && test -f a.out; then
        echo "Will link with -ltermcap."
        LIBS="-ltermcap $LIBS"
    else
        echo "WARNING:  Can't link with -ltermcap.  Using HARDCODE instead."
        echo "    TF will only work on a vt100 or similar terminal."
        TERMINAL="HARDCODE"
    fi
fi
echo "#define $TERMINAL" >&4



### Figure out which terminal driver to use.

if [ -z "$TTYDRIVER" ] && [ "$POSIX" = 1 ]; then
    if echo "#include <termios.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
        TTYDRIVER="USE_TERMIOS"
        echo "Using <termios.h>."
    else
        echo "Your system says it's POSIX, but I can't find <termios.h>.  Tsk."
    fi
fi
if [ -z "$TTYDRIVER" ]; then
    if echo "#include <termio.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
        HAVE_TERMIO=1
    fi
    if echo "#include <sgtty.h>" >test.c; ${CPP} test.c >/dev/null 2>&1; then
        HAVE_SGTTY=1
    fi

    if [ "$HAVE_TERMIO" = 1 ] && [ "$HAVE_SGTTY" = 1 ]; then
        echo "Your system has both <termio.h> and <sgtty.h>..."
#       They should both work, but let's try to use the "native" driver.
#       Make a semi-educated guess, and warn the user.
#       Many USG systems have uname, many BSD systems don't.
        if { uname; } >/dev/null 2>&1; then
            TTYDRIVER="USE_TERMIO"
            echo "    I'm going to use <termio.h>."
            echo "    To change it, set TTYDRIVER='USE_SGTTY' in Config."
        else
            TTYDRIVER="USE_SGTTY"
            echo "    I'm going to use <sgtty.h>."
            echo "    To change it, set TTYDRIVER='USE_TERMIO' in Config."
        fi
    elif [ "$HAVE_TERMIO" = 1 ]; then
        echo "Your system has <termio.h>."
        TTYDRIVER="USE_TERMIO"
    elif [ "$HAVE_SGTTY" = 1 ]; then
        echo "Your system has <sgtty.h>."
        TTYDRIVER="USE_SGTTY"
    else
        echo "I can't find any terminal driver headers on your system!"
        echo "I give up."
        exit 3
    fi
fi
echo "#define $TTYDRIVER" >&4

### Some brain damaged systems (Xenix) need <sys/ptem.h> for struct winsize.
if { egrep 'winsize' /usr/include/sys/ptem.h; } >/dev/null 2>&1; then
    echo "#define NEED_PTEM_H" >&4
fi

FLAGS="${CCFLAGS} ${FLAGS}"

OBJS="command.o dstring.o expand.o help.o history.o keyboard.o \
macro.o main.o malloc.o output.o process.o search.o signals.o socket.o \
tfio.o tty.o util.o variable.o world.o regexp/regexp.o"

if [ -n "$DMALLOC" ]; then
    FLAGS="$FLAGS -DDMALLOC"
    OBJS="$OBJS dmalloc.o"
fi

### write variables

echo "STRIP      = $STRIP" >&5
echo "FLAGS      = $FLAGS" >&5
echo "LIBS       = $LIBS" >&5
echo "OBJS       = $OBJS" >&5
echo "REGEXP_ERR = $REGEXP_ERR" >&5
echo >&5

### Clean up.

rm -f test.c test.out test.o libtest.o a.out libc.list

### Create Makefile.

cat > Makefile <<EOF
#### This src/Makefile was automatically generated by src/autoconfig.
#### The recommended precedure is to run the Makefile in the top directory.
#### You should not edit this file -- all configuration changes should
#### be made to the Config file in the top directory.

EOF

cat mf.vars mf.tail >> Makefile

