#!/bin/sh
########################################################################
#  TinyFugue - programmable mud client
#  Copyright (C) 1994, 1995, 1996 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 unix/Config file.
########################################################################

# TinyFugue automatic configuration
# Descriptor 4 is ../src/config.h, descriptor 5 is vars.mak.

### 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 linking fails, 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

TFVERSION=`sed -n -e '/Fugue version/s/^.*"\(.*\)".*$/\1/p' ../src/main.c`

echo "Configuring $TFVERSION"; echo

rm -f ../src/Makefile a.out

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

BINDIR=''
# CC=''		;# Use the value from the environment if there is one.
CCFLAGS=${TFDEV_CCFLAGS-'-O'}
# CPP=''	;# Use the value from the environment if there is one.
DMALLOC="${TFDEV_DMALLOC}"
EXE="${TFDEV_EXE}"
FLAGS=''
GNU_C=0
LIBDIR="${TFDEV_LIBDIR}"
LIBS=''
MAILDIR=''
# MAKE=''	;# Use the value from the environment if there is one.
MANPAGE=''
MANTYPE='cat'
MODE=''
POSIX=0
QUIET="${TFDEV_QUIET}"
SOCKS=''
STD_C=0
STRIP="${TFDEV_STRIP-strip}"
SYMLINK="${TFDEV_SYMLINK}"
TERMINAL=''
TFVER="$*"
TTYDRIVER=''
USE_FLOAT=''	;# incomplete

if [ -z "$USER" ]; then USER=$LOGNAME; fi
export USER

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

echo '#### DO NOT EDIT THIS FILE.' >&5
echo '#### This file was automatically generated by unix/tfconfig.' >&5
echo '#### Any configuration changes should be made in unix/Config.' >&5
echo >&5
echo "TFVERSION  = \"${TFVERSION}\"" >&5
echo "O          = o" >&5

cat >&4 <<EOF
/* config.h
 * This file was automatically generated by unix/tfconfig.
 */
#define PLATFORM_UNIX
EOF

UNAME=`{ uname -s && uname -v && uname -r || uname -a; } 2>/dev/null`
echo "#define UNAME     " \"$UNAME\" >&4
[ "$UNAME" = "SunOS 5.4" ] && echo "#define SUNOS_5_4" >&4

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


### Figure out installation locations.

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

if [ -z "$LIBDIR" ]; then
    if [ -w /usr/local/lib ]; then
        LIBDIR=/usr/local/lib/tf-lib
    else
        [ -d $HOME/lib ] && LIBDIR="$HOME/lib/tf-lib" || LIBDIR="$HOME/tf-lib"
    fi
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


[ -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 or '~'."
   echo "Edit unix/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, etc.

DIR1=`echo $EXE     | sed 's;/[^/]*$;;'`
DIR1=`cd $DIR1 && pwd`
DIR2=`echo $LIBDIR  | sed 's;/[^/]*$;;'`
DIR2=`cd $DIR2 && pwd`
DIR3=`echo $SYMLINK | sed 's;/[^/]*$;;'`
DIR3=`cd $DIR3 && pwd`

if [ -z "$DIR1" ] || [ -z "$DIR2" ] || [ -z "$DIR3" ]; then
    echo "Error in directory."
    exit 1;
fi

DIR1=`cd $DIR1 && pwd || { echo "Error in directory $DIR1."; false; }`

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 unix/Config and try again.'
    exit 2
fi

# Too many people have renamed the distribution directory to "$HOME/tf", and
# then wondered why they couldn't install the executable as "$HOME/tf".
if [ -d "${EXE}" ]; then
    echo "\"${EXE}\" is a directory; you can not install the executable"
    echo 'with the same name.  Either rename the directory, or choose a'
    echo 'different name for the executable.'
    exit 1
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 unix/Config file.'
while [ -z "$ans" ]; do
    echo 'Continue? (y/n)'
    read ans;
    case "$ans" in
    y|Y)  break ;;
    n|N)  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       = ${MAKE}" >&5
echo "EXE        = ${EXE}" >&5
echo "SYMLINK    = ${SYMLINK}" >&5
echo "LIBDIR     = ${LIBDIR}" >&5
echo "MAILDIR    = ${MAILDIR}" >&5
echo "MANPAGE    = ${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: GNU C's -ansi option does not guarantee existance of standard
### headers or libraries, so we must still verify them seperately.
### Note: many Solaris systems have a "cc" that does nothing.

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

### 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} -E test.c >/dev/null; then
        CPP="${CC} ${CCFLAGS} -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 stdc;
#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 'stdc' test.out >/dev/null 2>&1; then
    echo "Cool, you have a standard (ANSI) compiler."
    STD_C=1
else
    echo "Your compiler doesn't appear to be standard (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 reduces warnings.
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 4.x doesn't define mem... in string.h.
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

### This isn't required, but it reduces warnings.
if echo '#include <sys/select.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define SYS_SELECT_H <sys/select.h>' >&4
    echo "Found <sys/select.h>."
else
    echo '#undef  SYS_SELECT_H' >&4
    echo "I can't find <sys/select.h>, but that's ok."
fi

### For optional language support.
if echo '#include <locale.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define LOCALE_H <locale.h>' >&4
    echo "Found <locale.h>."
else
    echo '#undef  LOCALE_H' >&4
    echo "I can't find <locale.h>, but that's ok."
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/netinet/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 ok."
fi

### Find netdb header
if echo '#include <netdb.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define NETDB_H <netdb.h>' >&4
    echo "Found <netdb.h>."
elif echo '#include <sys/netdb.h>' >test.c; ${CPP} test.c >/dev/null 2>&1; then
    echo '#define NETDB_H <sys/netdb.h>' >&4
    echo "Found <sys/netdb.h>."
else
    echo '#define NO_NETDB' >&4
    echo "I can't find <netdb.h> or <sys/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 '#undef  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 [ "$STD_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 [ "$STD_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 '#undef  HAVE_STDARG' >&4
    fi
else
    echo '#undef  HAVE_STDARG' >&4
fi


### Test for function availability.
### We try to compile a program that calls the functions we're testing.
### If a function name appears in the stderr of the linking phase, we
### conclude it is unresolved or otherwise unavailable; if not, we conclude
### it's safe to use the function in tf.
### Testing each function in a seperate program might be slightly more
### reliable, but also much slower.

echo 'Testing for library symbols...'
cat > symtest.c <<EOF

#include "../src/config.h"    /* for header file macros */
#include <stdio.h>
#ifdef UNISTD_H
# include UNISTD_H
#endif
#ifdef STDLIB_H
# include STDLIB_H
#endif
#include STRING_H
#include <math.h>

int main() {
    int i;
    char *s;
    extern int h_errno;

    bcopy(NULL, NULL, 0);
    bsearch((void*)NULL, (void*)NULL, 0, 0, NULL);
    bzero(NULL, 0);
    connect(0, (void*)NULL, 0);
    fileno(stdout);
    getcwd(NULL, 0);
    gethostbyname(NULL);
    getpwnam(NULL);
    getwd(NULL);
    i = h_errno;
    hstrerror(i);
    index(NULL, 0);
    memcpy(NULL, NULL, 0);
    memset(NULL, 0, 0);
    sigaction(0, NULL, NULL);
    srand(0);
    srandom(0);
    strcasecmp(NULL, NULL);
    strchr(NULL, 0);
    strcmpi(NULL, NULL);
    strcspn(NULL, NULL);
    strerror(0);
    strftime(NULL, 0, NULL, NULL);
    stricmp(NULL, NULL);
    strstr(NULL, NULL);
    strtod(NULL, NULL);
    strtol(NULL, NULL, 0);

    nonexistant_function();
    return 0;
}
EOF

# We don't care about compiler warnings, as long as an object file is created.
${CC} ${CCFLAGS} -c symtest.c >symtest.out 2>&1 || {
    cat symtest.out
    echo
    echo "## The messages above indicate one of these configuration problems:"
    echo "##   1. there is no compiler;"
    echo "##   2. the compiler is not installed correctly;"
    echo "##   3. you do not have permission to execute the compiler; or"
    echo "##   4. CCFLAGS='${CCFLAGS}' in unix/Config is invalid."
    echo "## In any case, this is a local problem that can only be"
    echo "## solved by you or a system administrator, so don't ask me."
    echo
    exit 1
}

${CC} ${CCFLAGS} symtest.o >symtest.out 2>&1

# If "nonexistant_function" didn't appear in stderr, something else is wrong.
if egrep nonexistant_function symtest.out >/dev/null; then
    : expected error
else
    cat symtest.out
    echo
    echo "$0:  don't know how to test for symbols."
    exit 1;
fi

for sym in \
    bcopy bsearch bzero connect fileno getcwd gethostbyname getpwnam getwd \
    h_errno hstrerror index memcpy memset setlocale sigaction srand srandom \
    strcasecmp strchr strcmpi strcspn strerror strftime stricmp strstr strtod \
    strtol
do
    if egrep "(^|[^a-z_])_?_?${sym}([^a-z]|\$)" symtest.out >/dev/null; then
        echo "Did not find ${sym}, but that's ok."
        echo "#undef  HAVE_${sym}" >&4
    else
        echo "Found ${sym}."
        echo "#define HAVE_${sym}" >&4
    fi
done

### Figure out return type of signal handlers.
echo "Testing type of signal handlers..."
cat >test.c <<EOF
#include <sys/types.h>
#include <signal.h>
#undef signal
extern void (*signal())();
main() { exit(0); }
EOF
if ${CC} ${CCFLAGS} -c test.c >/dev/null 2>&1; then
    echo "Signal handlers return void, as they should."
    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 ${CC} ${CCFLAGS} -c test.c >/dev/null 2>&1; then
    echo "time() returns time_t, as it should."
    echo '#define TIME_T time_t' >&4
else
    echo "Assuming time() returns long."
    echo '#define TIME_T long' >&4
fi


rm -f symtest.out a.out symtest.o symtest.c

### 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.

### 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
${CC} ${CCFLAGS} -c libtest.c >/dev/null 2>&1
rm libtest.c
LIBTEST="rm -f a.out; ${CC} ${CCFLAGS} libtest.o"

### Test LIBS, to make sure user didn't screw it up.

if [ -n "$LIBS" ]; then
    if eval "${LIBTEST} ${LIBS} >/dev/null" && test -f a.out; then
        : okay
    else
        echo
        echo "Above problem may be due to unix/Config: LIBS=\"$LIBS\""
        exit 1
    fi
fi

### If -lbsd exists, we might need it (and it shouldn't make a difference if
### we don't need it, especially since it's the last library we link).

if eval "${LIBTEST} -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 '^#define HAVE_gethostbyname$' ../src/config.h >/dev/null 2>&1; then
    : Already found it.
elif eval "${LIBTEST} -lnsl_s ${LIBS} >/dev/null 2>&1" && test -f a.out; then
    echo "Will link with -lnsl_s for gethostbyname()."
    LIBS="-lnsl_s $LIBS"
elif eval "${LIBTEST} -lnsl ${LIBS} >/dev/null 2>&1" && test -f a.out; then
    echo "Will link with -lnsl for gethostbyname()."
    LIBS="-lnsl $LIBS"
else
    echo "Warning: can't find gethostbyname() or a library that might have it."
    echo "  Host name resolution will not be available."
    echo "/* warning: tfconfig 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 eval "${LIBTEST} -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 eval "${LIBTEST} -lnet ${LIBS} >/dev/null 2>&1" && test -f a.out; then
    echo "Will link with -lnet."
    LIBS="-lnet $LIBS"
fi

## If we haven't found connect(), look for -lsocket.
## Note: on IRIX 5, -lsocket exists, but we must not use its gethostbyname().
if egrep '^#define HAVE_connect$' ../src/config.h >/dev/null 2>&1; then
    : Already found it.
elif eval "${LIBTEST} -lsocket ${LIBS} >/dev/null 2>&1" && test -f a.out; then
    echo "Will link with -lsocket for connect(), etc."
    echo "#define HAVE_connect" >&4
    LIBS="-lsocket $LIBS"
else
    echo "Warning: can't find connect() or a library that might have it."
    echo "/* warning: tfconfig couldn't find connect() */" >&4
fi

### Test for SOCKS firewall proxy server.
if [ -n "$SOCKS" ]; then
    # SOCKS uses res_init(), so we need -lresolv if there is one.
    if eval "${LIBTEST} -lresolv ${LIBS} >/dev/null 2>&1" && test -f a.out; then
        echo "Will link with -lresolv."
        LIBS="-lresolv $LIBS"
    fi
    if eval "${LIBTEST} -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

### SCO keeps strftime() in -lintl, but others have -lintl without strftime().

if egrep '^#define HAVE_strftime$' ../src/config.h >/dev/null 2>&1; then
    : Already found it.
else
    echo "Testing for strftime() in -lintl."
    cat >test.c <<EOF
#include <time.h>
#include "../src/config.h"
main() { char s[9]; TIME_T t; strftime(s, sizeof(s), "%H", localtime(&t)); }
EOF
    if ${CC} ${CCFLAGS} test.c -lintl ${LIBS} >/dev/null 2>&1; then
        echo "Will link with -lintl for strftime()."
        LIBS="-lintl $LIBS"
        echo "#define HAVE_strftime" >&4
    else
        echo "strftime() not found.  Time formatting will not be supported."
    fi
    rm -f test.c test.o a.out
fi


### test termcap.
# At least one system (Red Hat Linux) has a broken ncurses and a working
# termcap, so we try termcap first.

if [ "$TERMINAL" != "HARDCODE" ]; then
  if eval "$LIBTEST -l$TERMINAL $LIBS >/dev/null 2>&1" && test -f a.out; then
    : okay
  elif eval "$LIBTEST -ltermcap $LIBS >/dev/null 2>&1" && test -f a.out; then
    TERMINAL=termcap
  elif eval "$LIBTEST -lncurses $LIBS >/dev/null 2>&1" && test -f a.out; then
    TERMINAL=ncurses
  elif eval "$LIBTEST -lcurses $LIBS >/dev/null 2>&1" && test -f a.out; then
    TERMINAL=curses
  else
    echo ''
    echo "WARNING:  Can't link with ${TERMINAL-term library}.  Using HARDCODE."
    echo "    TF will only work on a vt100 or similar terminal."
    TERMINAL="HARDCODE"
    echo ''
  fi
  if [ "$TERMINAL" != "termcap" ] &&
    test -n "/usr/doc/debian*" && test -d /usr/i486-linuxaout/bin; then
    echo ''
    echo 'WARNING: this looks like Debian Linux without an ELF term library.'
    echo 'Get one, or try setting PATH to "/usr/i486-linuxaout/bin:$PATH"'
    echo 'before calling unixmake.'
    echo ''
  fi
fi
if [ "$TERMINAL" != "HARDCODE" ]; then
    echo "Will link with -l${TERMINAL}."
    LIBS="-l${TERMINAL} $LIBS"
    echo "#define TERMCAP" >&4
else
    echo "#define HARDCODE" >&4
fi


#if [ -n "$USE_FLOAT" ]; then
#    LIBS="-lm $LIBS"
#    echo "#define USE_FLOAT" >&4
#    echo "Floating point math enabled."
#fi


### 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 unix/Config."
        else
            TTYDRIVER="USE_SGTTY"
            echo "    I'm going to use <sgtty.h>."
            echo "    To change it, set TTYDRIVER='USE_TERMIO' in unix/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}"

OTHER_OBJS=""

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

### write variables

echo "STRIP      = $STRIP" >&5
echo "FLAGS      = $FLAGS" >&5
echo "LIBS       = $LIBS" >&5
echo "OTHER_OBJS = $OTHER_OBJS" >&5
echo >&5

### Clean up.

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

### Create ../src/Makefile.

cat > ../src/Makefile <<EOF
#### DO NOT EDIT THIS FILE.
#### This src/Makefile was automatically generated by unix/tfconfig.  The
#### correct installation precedure is to run ./unixmake in the top directory.
#### You should not edit this file; all configuration changes should
#### be made to the unix/Config file.

EOF

cat vars.mak ../src/vars.mak unix.mak ../src/rules.mak >> ../src/Makefile

