#!/bin/sh
#
# This file creates a file called "sysdefs.h" which contains CPP defines
# helping to describe the operating system features.  We just take guesses
# by looking at random files.

# Removes any inherited definitions.
SYSDEF=
MAKE_ANSI=

while [ $# -gt 0 ]; do
	case "$1" in
	-s)	shift; srcdir=$1; shift ;;
	-i)	shift; incdir="$1"; shift ;;
	-A)	shift; MAKE_ANSI=true ;;
	*)	break ;;
	esac
done

if [ -n "$1" ]; then
  sysdefs=$1
else
  sysdefs=./sysdefs.h
fi

if [ -z "$srcdir" ]; then
	srcdir=.
fi

rm -f $sysdefs

echo "/* sysdefs.h -- #defines for your system created by $0."	   >>$sysdefs
echo "   Do NOT EDIT this file, since any changes will disappear." >>$sysdefs
echo "   Instead, edit $0, or config.h, or machines.h. */"	   >>$sysdefs
echo ""								   >>$sysdefs
echo "#if !defined (_SYSDEFS_H_)"				   >>$sysdefs
echo "#  define _SYSDEFS_H_"					   >>$sysdefs

# was if [ -f /usr/bin/uname ] || [ -f /bin/uname ]
if ( uname >/dev/null 2>&1 ) 2>/dev/null
then
	UNAME=`uname`
	UNAME_R=`uname -r 2>/dev/null`
	UNAME_M=`uname -m 2>/dev/null`
	UNAME_V=`uname -v 2>/dev/null`
	UNAME_S=`uname -s 2>/dev/null`
	RELEASE=`expr "$UNAME_R" : '[^0-9]*\([0-9]*\)'`
	RELEASE=`expr "$RELEASE" + 0`
	LEVEL=`expr "$UNAME_R" : '[^0-9]*[0-9]*.\([0-9]*\)'`
fi

# check for versions of SunOS 
case "${UNAME}${RELEASE}" in
SunOS4*) SYSDEF=SunOS4 ;;
SunOS5*) SYSDEF=SunOS5 ;;
esac

# Test for NeXT
if [ -d /NextLibrary ]; then
	MAKE_ANSI=true
fi

# Intel Paragon
case "$UNAME_M" in
paragon) MAKE_ANSI=true ;;
esac

# Test for shared libraries (this is pretty sVr4ish).
if [ -f /usr/ccs/lib/libc.so ]; then
  SYSDEF=USGr4
fi

# Some versions of i386 SVR4.2 make `uname' equivalent to `uname -n', which
# is contrary to all other versions of uname
if [ -n "$UNAME" ] && [ "$UNAME_S" != "$UNAME" ] && [ "$UNAME_S" = UNIX_SV ]; then
	UNAME=UNIX_SV
fi

# another check for SVR4 on 386 or 486 machines
case "${UNAME_M}:${UNAME}:${UNAME_R}" in
i[34]86:UNIX_SV:4.*)	SYSDEF=USGr4 ;;
esac

# A check for Mips RISCos
case "$UNAME_V" in
UMIPS|RISCos) SYSDEF=RISCos_${RELEASE}_${LEVEL} ;;
esac

# Look for an error message when trying to exec bison.  If we find
# what we're looking for, then we don't have it.  If we get something
# else (like an error message about no grammar file), then we have
# it.
HAVE_BISON=
if ( cd /tmp ; bison /dev/null 2>&1 >/dev/null | grep 'no input grammar' >/dev/null 2>&1 ) 2>/dev/null
then
  HAVE_BISON=yes
fi

# Try to locate ranlib.  I think this is a bad idea.
if [ -f /usr/bin/ranlib ]; then
	RANLIB_LOCATION=/usr/bin/ranlib;
elif [ -f /bin/ranlib ]; then
	RANLIB_LOCATION=/bin/ranlib;
elif [ -f /usr/local/bin/ranlib ]; then
	RANLIB_LOCATION=/usr/local/bin/ranlib;
elif [ -f /usr/local/gnubin/ranlib ]; then
	RANLIB_LOCATION=/usr/local/gnubin/ranlib;
fi

if [ -n "${RANLIB_LOCATION}" ]; then
  echo ""						>>$sysdefs
  echo "#if !defined (RANLIB_LOCATION)" 		>>$sysdefs
  echo "#  define RANLIB_LOCATION ${RANLIB_LOCATION}"	>>$sysdefs
  echo "#endif /* RANLIB_LOCATION */"		 	>>$sysdefs
fi

#
# Is this a Xenix system?
#
if [ -f /xenix ]; then
  SYSDEF="Xenix"
  case "`/bin/uname -p`" in
    *286) SYSDEF="Xenix286" ;;
    *386) SYSDEF="Xenix386" ;;
  esac

  # make sure that `i386' is defined for machines.h
  if [ "$SYSDEF" = "Xenix386" ]; then
    echo ""				>>$sysdefs
    echo "#if !defined (i386)"		>>$sysdefs
    echo "#  define i386"		>>$sysdefs
    echo "#endif /* !i386 */"		>>$sysdefs	
  fi

  # Pass the release number of the OS through to the machine descriptions
  # in machines.h.
  if [ -f /etc/perms/soft ]; then
    rel=`grep rel= /etc/perms/soft`
    case "$rel" in
      *2.2.*) XREL=XENIX_22 ;;
      *2.3.*) XREL=XENIX_23 ;;
      *3.2.*) XREL=XENIX_32 ;;
      *) XREL= ;;
    esac

    if [ "$XREL" ]; then
      echo ""				>>$sysdefs
      echo "#if !defined ($XREL)"	>>$sysdefs
      echo "#  define $XREL"		>>$sysdefs
      echo "#endif /* !$XREL */"	>>$sysdefs
    fi
  fi
fi

#
# Is this some kind of Sys Vish system?
#
if [ -f /unix ]; then
  if [ -d /generic ]; then   # This is an AIX system.
    SYSDEF="aixpc"
    MAKE_ANSI=true
  elif [ -d /etc/conf/kconfig.d ] && [ -f /usr/include/sys/limits.h ]; then
    SYSDEF="isc386"			# This is a 386 running ISC?
    ISCREL="ISC_$RELEASE"
    echo "#if !defined ($ISCREL)"	>>$sysdefs
    echo "#  define $ISCREL"		>>$sysdefs
    echo "#endif /* $ISCREL */"		>>$sysdefs
  elif [ -f /etc/xlc.cfg ]; then
    if fgrep _IBMR2 /etc/xlc.cfg >/dev/null 2>&1; then
      SYSDEF=RISC6000
      MAKE_ANSI=true
    fi
  elif [ -f /bin/4d -a -f /bin/uname ]; then
    case "$UNAME_R" in
      3.*) SYSDEF="Irix3" ;;
      4.*) SYSDEF="Irix4" ;;
      5.*) SYSDEF="Irix5" ;;
      *)   SYSDEF="Irix3" ;;
    esac
  elif [ -d /usr/amiga ]; then
    SYSDEF="amiga"		# An Amiga running V.4.
  elif [ -f /bin/fxc.info ]; then
    SYSDEF="alliant"
  fi
fi

# Is this a Unicos system?
if [ -f /unicos ]; then
   MAKE_ANSI=true
   UnicosMachine=

   # Test for the variaous flavors of Cray machines.
   if [ -x /bin/cray1 ] && /bin/cray1 2>/dev/null; then
      UnicosMachine=Cray1
   fi

   if [ -x /bin/cray2 ] && /bin/cray2 2>/dev/null; then
      UnicosMachine=Cray2
   fi

   if [ -x /bin/crayxmp ] && /bin/crayxmp 2>/dev/null; then
      UnicosMachine=CrayXMP
   fi
   if [ -x /bin/crayymp ] && /bin/crayymp 2>/dev/null; then
      UnicosMachine=CrayYMP
   fi

   if [ "$UnicosMachine" ]; then
      echo "#if !defined ($UnicosMachine)"	>>$sysdefs
      echo "#  define $UnicosMachine"		>>$sysdefs
      echo "#endif /* !$UnicosMachine */"	>>$sysdefs
   fi
fi

# Is this (and what kind of) a HPUX system?
if [ -f /hp-ux ]; then
  SYSDEF=HPUX_${RELEASE}
  if [ "$RELEASE" = 6 -a "$LEVEL" -lt 2 ]; then
    SYSDEF=HPUX_USG
  fi
fi

if [ "$SYSDEF" = "" ]; then
	case "$UNAME_M" in
	ESA) SYSDEF=AIXESA ;;    
	XD88*) SYSDEF=XD88 ;;
	M88100) SYSDEF=M88100 ;;	# Motorola Delta 88K
	esac
fi

# What release of SCO Unix is this?
if [ "$SYSDEF" = "" -a -f /bin/uname ]; then
  case `/bin/uname -X 2>/dev/null | grep '^Release' 2>/dev/null` in
    *3.2v4.*) SYSDEF=SCOv4 ;;
	   *) SYSDEF=SCO ;;
  esac
fi

#
# Default to cadmus for unknown SysVish systems
#
if [ -f /unix ] && [ "$SYSDEF" = "" ]; then
  SYSDEF="cadmus"
fi

if [ "$SYSDEF" != "" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined ($SYSDEF)"			>>$sysdefs
  echo "#  define $SYSDEF"			>>$sysdefs
  echo "#endif /* $SYSDEF */"			>>$sysdefs
fi

# Now look for certain include files in a list of directories
# Poor substitute for autoconf

# Add any other directories where include files are found to this list or
# create another case
if [ -n "$incdir" ]; then
	dirlist="$incdir"
else
	case "$SYSDEF" in
	RISCos*) dirlist="/bsd43/usr/include";;
	*) dirlist="/usr/include /usr/include/bsd /usr/include/ansi" ;;
	esac
fi

# Code fragment to be executed to find a particular include file.  Make sure
# to set `file' to the pathname of the file you want, relative to /usr/include,
# before calling `eval $findf'.
findf="
found='';
for d in \$dirlist;
do
	if test -f \$d/\$file;
	then
		found=yes;
		break;
	fi;
done
"

found=
file=sys/stream.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_SYS_STREAM_H)"	>>$sysdefs
  echo "#  define HAVE_SYS_STREAM_H"		>>$sysdefs
  echo "#endif /* HAVE_SYS_STREAM_H */"		>>$sysdefs
fi

found=
file=sys/ptem.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_SYS_PTEM_H)"		>>$sysdefs
  echo "#  define HAVE_SYS_PTEM_H"		>>$sysdefs
  echo "#endif /* HAVE_SYS_PTEM_H */"		>>$sysdefs
fi

file=sys/pte.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_SYS_PTE_H)"		>>$sysdefs
  echo "#  define HAVE_SYS_PTE_H"		>>$sysdefs
  echo "#endif /* HAVE_SYS_PTE_H */"		>>$sysdefs
fi

file=sys/wait.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_WAIT_H)"		>>$sysdefs
  echo "#  define HAVE_WAIT_H"			>>$sysdefs
  echo "#endif /* HAVE_WAIT_H */"		>>$sysdefs
fi

file=sys/resource.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_RESOURCE)"		>>$sysdefs
  echo "#  define HAVE_RESOURCE"		>>$sysdefs
  echo "#endif /* HAVE_RESOURCE */"		>>$sysdefs
fi

file=sys/param.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_SYS_PARAM)"		>>$sysdefs
  echo "#  define HAVE_SYS_PARAM"		>>$sysdefs
  echo "#endif /* HAVE_SYS_PARAM */"		>>$sysdefs
fi

file=unistd.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_UNISTD_H)"		>>$sysdefs
  echo "#  define HAVE_UNISTD_H"		>>$sysdefs
  echo "#endif /* HAVE_UNISTD_H */"		>>$sysdefs
fi

file=stdlib.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_STDLIB_H)"		>>$sysdefs
  echo "#  define HAVE_STDLIB_H"		>>$sysdefs
  echo "#endif /* HAVE_STDLIB_H */"		>>$sysdefs
fi

file=limits.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_LIMITS_H)"		>>$sysdefs
  echo "#  define HAVE_LIMITS_H"		>>$sysdefs
  echo "#endif /* HAVE_LIMITS_H */"		>>$sysdefs
fi

file=alloca.h
eval $findf
if [ -f /usr/include/alloca.h ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_ALLOCA_H)"		>>$sysdefs
  echo "#  define HAVE_ALLOCA_H"		>>$sysdefs
  echo "#endif /* HAVE_ALLOCA_H */"		>>$sysdefs
fi

file=dirent.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_DIRENT_H)"		>>$sysdefs
  echo "#  define HAVE_DIRENT_H"		>>$sysdefs
  echo "#endif /* HAVE_DIRENT_H */"		>>$sysdefs
fi

file=string.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_STRING_H)"		>>$sysdefs
  echo "#  define HAVE_STRING_H"		>>$sysdefs
  echo "#endif /* HAVE_STRING_H */"		>>$sysdefs
fi

file=varargs.h
eval $findf
if [ -n "$found" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_VARARGS_H)"		>>$sysdefs
  echo "#  define HAVE_VARARGS_H"		>>$sysdefs
  echo "#endif /* HAVE_VARARGS_H */"		>>$sysdefs
fi

# Does the system have a /dev/fd directory?
if [ -d /dev/fd ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_DEV_FD)"		>>$sysdefs
  echo "#  define HAVE_DEV_FD"			>>$sysdefs
  echo "#endif /* HAVE_DEV_FD */"		>>$sysdefs
fi

# Is this SVR4.2?  It's subtly different from USGr4
if [ "$UNAME" = "UNIX_SV" ] && [ "$UNAME_R" = "4.2" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (USGr4_2)"		>>$sysdefs
  echo "#  define USGr4_2"		>>$sysdefs
  echo "#endif /* USGr4_2 */"		>>$sysdefs
fi

# Is this AIX PS/2 1.3?  Yuck.
if [ "$UNAME" = "AIX" ] && [ "$UNAME_V" = "1" ] && [ "$RELEASE" = "3" ]; then
  case "$UNAME_M" in
  i386|i486)
	echo ""					>>$sysdefs
	echo "#if !defined (AIX_13)"		>>$sysdefs
	echo "#  define AIX_13"			>>$sysdefs
	echo "#endif /* AIX_13 */"		>>$sysdefs
	;;
  esac
fi

if [ -n "$HAVE_BISON" ]; then
  echo ""					>>$sysdefs
  echo "#if !defined (HAVE_BISON)"		>>$sysdefs
  echo "#  define HAVE_BISON"			>>$sysdefs
  echo "#endif /* HAVE_BISON */"		>>$sysdefs
fi

# Functions to test for a la autoconf
#	getwd
#	getcwd
#	strchr
#	strcasecmp
#	getgroups
#	setlinebuf
#	strerror
#	vfprintf
#	bcopy
#	getdtablesize
#	setdtablesize
#	alloca
#	gethostname
#	memmove (missing)
#	mkfifo (missing)
#
# Other things to test
#	opendir robustness
#	dup2 working
#	void sighandler
#	sys_siglist[]
#	uid_t, gid_t
#	have_getpw_decls
#	reversed setvbuf args
#	int getgroups

# If this system's cpp might not like `/**/#' in cpp-Makefile, make an
# alternate ansi-style cpp-Makefile.
if [ -n "$MAKE_ANSI" ]; then
   grep -v '/\*\*/' ${srcdir}/cpp-Makefile >ansi-Makefile
fi

# These should be the last 2 lines in this file!
echo ""						>>$sysdefs
echo "#endif /* _SYSDEFS_H_ */"			>>$sysdefs
