#! /bin/sh

# Shell script to determine the operating system type by various heuristics.
# For some OS there are two variants: a full name, which is used for the
# build directory, and a generic name, which is used to identify the OS-
# specific scripts, and which can be the same for different versions of
# the OS. Solaris 2 is one such OS. The option -generic specifies the
# latter type of output.

# If OSTYPE is set, use it. This allows a manual override.

case "$OSTYPE" in ?*) os="$OSTYPE";; esac

# For some shells running under IRIX the value of $OSTYPE is just "irix",
# which isn't very helpful. We know that uname works under IRIX, so in
# this case just unset os and fall through. BSDI 3.0 sets $OSTYPE to
# "386BSD" which is something other OS may also use, so unset that too.

case "$os" in
irix)       os='';;
386BSD)     os='';;
esac

# If os is still unset, try to get a value from the uname command.

case "$os" in '') os=`uname -s`;; esac

# ... what else can be tried? Insert here any other bright ideas that might
# come to mind ...

# Failed to find OS by automatic means.

case "$os" in
'') echo "" 1>&2
    echo "*** Failed to determine the operating system type." 1>&2
    echo "" 1>&2
    echo UnKnown
    exit 1;;
esac

# Clean out gash characters

os=`echo $os | sed 's,[^-+_.a-zA-Z0-9],,g'`

# A value has been obtained for the os. Some massaging may be needed in
# some cases to get a uniform set of values. Various shells set OSTYPE to
# things that aren't quite the same as each other. It is all a huge mess.

case "$os" in
aix*)       os=AIX;;
AIX*)       os=AIX;;
bsdi)       os=BSDI;;
BSDOS)      os=BSDI;;
BSD_OS)     os=BSDI;;
dgux)       os=DGUX;;
freebsd*)   os=FreeBSD;;
Irix5)	    os=IRIX;;
Irix6)	    os=IRIX6;;
IRIX64)     os=IRIX6;;
IRIX)       version=`uname -r`
            case "$version" in
            5*)  os=IRIX;;
            6*)  os=IRIX632;;
            esac;;
HI-OSF1-MJ) os=HI-OSF;;
HI-UXMPP)   os=HI-OSF;;
hpux*)      os=HP-UX;;
linux)      os=Linux;;
linux-*)    os=Linux;;
Linux-*)    os=Linux;;
openbsd*)   os=OpenBSD;;
osf1)       os=OSF1;;
solaris*)   os=SunOS5;;
sunos4)     os=SunOS4;;
Ultrix)     os=ULTRIX;;
esac

# In the case of SunOS we need to distinguish between SunOS4 and SunOS5.

case "$os" in
SunOS)	case `uname -r` in
	5*)	os="${os}5";;
	4*)	os="${os}4";;
	esac
esac

# Need to distinguish SunOS5 from the version on the HAL (64bit sparc,
# CC=hcc -DV7). Also need to distinguish different versions of the OS
# for building different binaries.

case "$os" in
SunOS5) case `uname -m` in
        sun4H)  os="${os}-hal";;
            *)  os="${os}-`uname -r`";;
        esac
esac

# In the case of Linux we need to distinguish which libc is used.
# This is more cautious than it needs to be. In practice libc5 will always
# be a symlink, and libc6 will always be a linker control file, but it's
# easy enough to do a better check, and check the symlink destination or the
# control file contents and make sure.

case "$os" in
Linux)  if [ -L /usr/lib/libc.so ]; then
            if [ x"$(file /usr/lib/libc.so | grep "libc.so.5")"x != xx ]; then
                    os=Linux-libc5
            fi
        else
            if grep -q libc.so.5 /usr/lib/libc.so; then
                    os=Linux-libc5
            fi
        fi
        ;;
esac

# If a generic OS name is requested, some further massaging is needed
# for some systems.

if [ "$1" = '-generic' ]; then
  case "$os" in
  SunOS5*) os=SunOS5;;
  esac
fi

# OK, the script seems to have worked. Pass the value back.

echo "$os"

# End of os-type
