#!/bin/sh
#
#     tiger - A UN*X security checking system
#     Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
#
#    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.
#
#     Please see the file `COPYING' for the complete copyright notice.
#
# util/gethostinfo - 04/22/93
#
# 03/25/2004 rbrad Added support for Tru64.
# 08/14/2003 jfs Applied code from TARA 3.0.3 to recognise MacOS
# 08/09/2003 jfs Applied patch from Ryan Braderitch which fixes a bug so the
#                directory structure works for HPUX.
# 07/26/2002 jfs Modified so it can work if called directly. Removed
#                unnecesary exit's
#
#-----------------------------------------------------------------------------
# TODO:
# - It might be better to not use $OS/$REV for Linux in the same sense
#   that it is used for other operating systems. For Linux distributions
#   (RedHat, Debian, Mandrake, SuSE...) that's just the Kernel thus,
#   the $OS/$REV variables should be substituted with 
#   $OS=distribution
#   $REV=distribution release
#   The code used by Bastille for OS identification might come in handy
#   here (basicly you just need to parse some /etc/ file which varies
#   from distribution and determines which release you are using)
#
# - Investigate if it's better to use uname -rms, which probably is
#   better to parse (and less error-prone) as suggested by Nicolas Franois
#-----------------------------------------------------------------------------
#
[ -z "$WORKDIR" ] && WORKDIR=./run

findcmd()
{
  CMD=$1

  SRCH=/bin:/usr/bin:/etc:/usr/etc:/usr/ucb
  
  SAVEIFS=$IFS
  IFS=:
  set $SRCH
  IFS=$SAVEIFS
  for dir
  do
    [ $TESTEXEC $dir/$CMD ] && {
      echo $dir/$CMD
      return
    }
  done
}

TESTEXEC=-x
( [ $TESTEXEC /bin/sh ] ) 2> $WORKDIR/te.$$
[ -s $WORKDIR/te.$$ ] && TESTEXEC=-f
export TESTEXEC

RM=`findcmd rm`
[ -n "$RM" ] && $RM $WORKDIR/te.$$
AWK=`findcmd awk`

#------------------------------------------------------------------------
# This should get:
#
#    Sun, Cray, AIX, HP-UX, IRIX, Linux and MacOSX
#
UNAME=`findcmd uname`

[ -n "$UNAME" ] && {
  $UNAME -a |
  $AWK '
         {
	   if($5 == "CRAY"){
		if($6 == "T3E")
		{
			printf("UNICOSMK %s %s.%s\n", $3, $5, $6);
		}
		else
		{
			printf("UNICOS %s %s.%s\n", $3, $5, $6);
		}
	   }
	   if($1 == "SunOS"){
   	        printf("%s %s %s\n", $1, $3, $5);
	   }
	   else if($1 == "AIX"){
	        printf("%s %s.%s RS6000\n", $1, $4, $3);
	   }
	   else if($1 == "HP-UX"){
                printf("HPUX %s %s\n", substr($3,3), $5);
           }
           else if($1 == "IRIX"){
                printf("IRIX %s %s\n", $3, $5);
           }
           else if($1 == "IRIX64"){
                printf("IRIX %s %s\n", $3, $5);
           }
	   else if($1 == "Linux"){
		printf("Linux %s %s\n",$3, $12);
	   }
	   else if($1 == "Darwin"){
	        printf("MacOSX %s %s\n",$3, $11);
	   }
	   else if($1 == "Jaguar"){
	        printf("MacOSX %s %s\n",$3, $11);
	   }
	   else if($1 == "OSF1"){
	        printf("Tru64 %s %s\n",substr($3,2), $5);
	   }
	 }
  '
# In some uname's we might need this ...
#		printf("Linux %s %s\n",$3, $11);
# TODO: check if the format has changed from
# version 2.0.11
  exit
}

#------------------------------------------------------------------------
# This should get:
#
#    NeXT
#
HOSTINFO=`findcmd hostinfo`

[ -n "$HOSTINFO" ] && {
  $HOSTINFO |
  $AWK '
        BEGIN {
	  OS="";
	  REV="";
	  ARCH="";
	}
	NR == 2 {
          OS=$1
	  REV=substr($3,1,length($3)-1);
	}
	NR == 6 && NF == 4 {
	  ARCH=substr($4,2,length($4)-2);
   	}
	NR == 6 && NF == 3 {
	  ARCH=$3
	}
	END {
	  printf("%s %s %s\n", OS, REV, ARCH);
	}
  '
  exit
}
#
exit 0
