:
# Guess values for system-dependant variables and create `Makefile'.
# Copyright (C) 1991 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Usage: configure [+srcdir=DIR] [+host=HOST] [+gas] [+nfp] [TARGET]
# All args except +srcdir=DIR are ignored.

trap 'rm -f conftest conftest.c uttest; exit 1' 1 3 15

set +u # Make sure unset variables are ok.

# A filename unique to this package, relative to the directory that
# configure is in, which we can look for to find out if srcdir is correct.
unique_file=src/ls.c

for arg in $*; do
  # Handle +srcdir with a space before the argument.
  if [ x$next_srcdir = xyes ]; then srcdir=$arg; next_srcdir=
  # Handle +host with a space before the argument.
  elif [ x$next_host = xyes ]; then next_host=
  else 
    case $arg in
     -srcdir=* | +srcdir=* | +srcdi=* | +srcd=* | +src=* | +sr=* | +s=*)
	srcdir=`echo $arg | sed 's/[+-]s[a-z]*=//'` ;;
     -srcdir | +srcdir | +srcdi | +srcd | +src | +sr | +s)
	next_srcdir=yes ;;
     -host=* | +host=* | +hos=* | +ho=* | +h=*) ;;
     -host | +host | +hos | +ho | +h)
	next_host=yes ;;
     -gas | +gas | +ga | +g) ;;
     -nfp | +nfp | +nf | +n) ;;
     *) ;;
    esac
  fi
done

vpsub='s@\$\([-./a-zA-Z0-9]*\)<@\1@g'
# Find the source files, if location was not specified.
if [ x$srcdir = x ]; then
  srcdirdefaulted=yes; srcdir=.
  if [ ! -r $unique_file ]; then srcdir=`pwd`/..; fi
fi
if [ $srcdir != . ]; then
  VPATH='VPATH = $(srcdir)'
  vpsub='s@\$\([-./a-zA-Z0-9]*\)<@\$<@g'
fi

if [ ! -r $srcdir/$unique_file ]; then
  if [ x$srcdirdefaulted = xyes ]; then
    echo "$0: Can not find sources in \`.' or \`..'." 1>&2
  else
    echo "$0: Can not find sources in \`${srcdir}'." 1>&2
  fi
  exit 1
fi

if test "$RANDOM" = "$RANDOM"; then
  # Plain old Bourne shell.
  echo checking for gcc
  test -z "$CC" -a -n "`gcc 2>&1`" && CC="gcc -O"

  echo checking for ranlib
  test -z "$RANLIB" -a -n "`ranlib conftest 2>&1`" && RANLIB=ranlib
  rm -f conftest
else
  # ksh, bash or zsh.  ksh and zsh write "command not found" to stderr.
  echo checking for gcc
  test -z "$CC" && type gcc && CC="gcc -O"

  echo checking for ranlib
  test -z "$RANLIB" && type ranlib && RANLIB=ranlib
fi

CC=${CC-cc}
RANLIB=${RANLIB-echo}
INCLUDEDIR=${INCLUDEDIR-/usr/include}

rm -f conftest conftest.c
compile="$CC $DEFS conftest.c -o conftest $LIBS >/dev/null 2>&1"

# Check for various header files.

echo checking signal handler return type
grep 'int[ 	]*(\*signal[ 	]*(' $INCLUDEDIR/signal.h \
  $INCLUDEDIR/sys/signal.h >/dev/null 2>&1 &&
  DEFS="$DEFS -DSIGTYPE=int"

echo checking for sys/mkdev.h
if test -f $INCLUDEDIR/sys/mkdev.h; then
  DEFS="$DEFS -DMAJOR_IN_MKDEV"
fi

echo checking for directory library header
if test -f $INCLUDEDIR/dirent.h; then
  DEFS="$DEFS -DDIRENT"
elif test -f $INCLUDEDIR/sys/ndir.h; then
  DEFS="$DEFS -DSYSNDIR"
elif grep 'void.*closedir' $INCLUDEDIR/sys/dir.h >/dev/null 2>&1; then
  DEFS="$DEFS -DVOID_CLOSEDIR"
fi

echo checking for st_blocks in struct stat
echo "#include <sys/types.h>
#include <sys/stat.h>
main() { struct stat s; exit(0); s.st_blocks; }" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
else
  DEFS="$DEFS -DST_BLOCKS_MISSING"
  LIBOBJS="$LIBOBJS fileblocks.o"
fi
rm -f conftest conftest.c

echo checking how to get filesystem info
if test -f $INCLUDEDIR/sys/statvfs.h -a -f $INCLUDEDIR/sys/fstyp.h; then
  DEFS="$DEFS -DFS_STATVFS" PROGS="$PROGS df" LIBOBJS="$LIBOBJS fsinfo.o"
elif test -f $INCLUDEDIR/sys/statfs.h -a -f $INCLUDEDIR/sys/fstyp.h; then
  DEFS="$DEFS -DFS_USG_STATFS" PROGS="$PROGS df" LIBOBJS="$LIBOBJS fsinfo.o"
elif test -f $INCLUDEDIR/mntent.h; then
  DEFS="$DEFS -DFS_MNTENT" PROGS="$PROGS df" LIBOBJS="$LIBOBJS fsinfo.o"
elif test -f $INCLUDEDIR/sys/mount.h -a -f $INCLUDEDIR/sys/fs_types.h; then
  DEFS="$DEFS -DFS_GETMNT" PROGS="$PROGS df" LIBOBJS="$LIBOBJS fsinfo.o"
elif grep MOUNT_UFS $INCLUDEDIR/sys/mount.h >/dev/null 2>&1; then
  DEFS="$DEFS -DFS_STATFS" PROGS="$PROGS df" LIBOBJS="$LIBOBJS fsinfo.o"
fi

echo checking for ANSI C header files
echo "#include <stdlib.h>
#include <string.h>
main() { exit(0); strerror(0); }" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then
  DEFS="$DEFS -DSTDC_HEADERS"
fi
rm -f conftest conftest.c

echo checking for POSIX.1 header files
echo "#include <unistd.h>
main() {
#ifdef _POSIX_VERSION
exit(0);
#else
exit(1);
#endif
}" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then
  DEFS="$DEFS -DPOSIX"
fi
rm -f conftest conftest.c

echo checking for BSD string and memory functions
echo "#include <strings.h>
main() { exit(0); rindex(0, 0); bzero(0, 0); }" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
  else DEFS="$DEFS -DUSG"
fi
rm -f conftest conftest.c

echo checking whether sys/types.h defines uid_t
echo '#include <sys/types.h>
main() { uid_t x; exit(0); }' > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
else
  uid_t=`awk '/pw_uid;/ {print $1}' $INCLUDEDIR/pwd.h`
  DEFS="$DEFS -Duid_t=${uid_t} -Dgid_t=${uid_t}"
fi
rm -f conftest conftest.c

# Check whether various functions exist.

# Functions we provide replacements for.
for func in fnmatch mkdir mktime stpcpy strdup strstr rename
do
  echo checking for $func
  echo "main() { exit(0); ${func}(); }" > conftest.c
  eval $compile
  if test -s conftest && ./conftest 2>/dev/null; then :
  else LIBOBJS="$LIBOBJS ${func}.o"
  fi
  rm -f conftest conftest.c
done

# Functions whose absence we work around.
for func in fchmod ftime mkfifo vprintf
do
  echo checking for $func
  echo "main() { exit(0); ${func}(); }" > conftest.c
  eval $compile
  if test -s conftest && ./conftest 2>/dev/null; then :
  else DEFS="$DEFS -D`echo $func|tr '[a-z]' '[A-Z]'`_MISSING"
  fi
  rm -f conftest conftest.c
done

case "$DEFS" in
*VPRINTF_MISSING*)
  func=_doprnt
  echo checking for $func
  echo "main() { exit(0); ${func}(); }" > conftest.c
  eval $compile
  if test -s conftest && ./conftest 2>/dev/null; then :
  else DEFS="$DEFS -DDOPRNT_MISSING"
  fi
  rm -f conftest conftest.c
  ;;
esac

func=ftruncate
echo checking for $func
echo "main() { exit(0); ${func}(); }" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
else
  rm -f conftest conftest.c
  echo "#include <sys/types.h>
#include <fcntl.h>
main() {
#if defined(F_CHSIZE) || defined(F_FREESP)
exit(0);
#else
exit(0); chsize();
#endif
}" > conftest.c
  eval $compile
  if test -s conftest && ./conftest 2>/dev/null; then
    LIBOBJS="$LIBOBJS ${func}.o"
  else DEFS="$DEFS -D`echo $func|tr '[a-z]' '[A-Z]'`_MISSING"
  fi
fi
rm -f conftest conftest.c

# Check other misc. things.

echo checking integer size
echo "main() { exit(!(sizeof(long) > sizeof(int))); }" > conftest.c
eval $compile
./conftest && DEFS="$DEFS -DINT_16_BITS"
rm -f conftest conftest.c

echo checking utime with null argument
rm -f uttest; > uttest
# Sequent interprets utime(file, 0) to mean use start of epoch.  Wrong.
echo '#include <sys/types.h>
#include <sys/stat.h>
main() {
struct stat s, t;
exit(!(stat ("uttest", &s) == 0 && utime("uttest", (long *)0) == 0
&& stat("uttest", &t) == 0 && t.st_mtime - s.st_mtime < 120));
}' > conftest.c
eval $compile
./conftest || DEFS="$DEFS -DUTIME_NULL_MISSING"
rm -f conftest conftest.c uttest

case "$LIBOBJS" in
*rename.o*)
  LIBPROGS="$LIBPROGS mvdir"
  DEFS="$DEFS -DMVDIR="'\\"$(libdir)/mvdir\\"'
  ;;
esac

echo checking for Minix
if test -d /usr/include/minix; then
  DEFS="$DEFS -UUSG -D_POSIX_SOURCE -D_POSIX_1_SOURCE=2 -D_MINIX"
fi

echo checking for Xenix
if test -f /xenix; then
  LIBS="$LIBS -lx"
  case "$DEFS" in
  *SYSNDIR*) ;;
  *) LIBS="-ldir $LIBS" ;; # Make sure -ldir precedes any -lx.
  esac
fi

echo checking how to get alloca
echo '
#ifdef __GNUC__
#define alloca __builtin_alloca
#else
#ifdef sparc
#include <alloca.h>
#else
#ifdef _AIX
 #pragma alloca
#else
char *alloca ();
#endif
#endif
#endif
main() { char *p = (char *) alloca(1); exit(0); }' > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
elif test -d /usr/ucblib; then LIBS="$LIBS -L/usr/ucblib -lucb"
else LIBS="$LIBS -lPW"
fi
rm -f conftest conftest.c

trap 'rm -f Makefile lib/Makefile src/Makefile man/Makefile config.status; exit 1' 1 3 15

for dir in . lib src man
do
  test -d $dir || mkdir $dir
  echo '# Generated automatically from Makefile.in by configure.' > $dir/Makefile
  sed -e "
$vpsub
s,@srcdir@,$srcdir,
s,@VPATH@,$VPATH,
s,@CC@,$CC,
s,@RANLIB@,$RANLIB,
s,@PROGS@,$PROGS,
s,@DEFS@,$DEFS,
s,@LIBS@,$LIBS,
s,@LIBPROGS@,$LIBPROGS,
s,@LIBOBJS@,$LIBOBJS,
" $srcdir/$dir/Makefile.in >> $dir/Makefile
done

echo "CC = $CC
RANLIB = $RANLIB
PROGS = $PROGS
DEFS = $DEFS
LIBS = $LIBS
LIBPROGS = $LIBPROGS
LIBOBJS = $LIBOBJS" > config.status
