#!/usr/bin/sh
## Copyright (c) 1996, Lotus Development Corporation.
## All Rights Reserved.

#
#
PATH=/bin:/usr/bin
base=`basename $0`

USAGE="Usage:	$base [-mnrsvza]
Options:
	-m	machine model (eg PA, Sparc)
	-n	nodename (eg ibm3, peregrine)
	-r	operating system revision (eg 07 for HP-UX 8.07)
	-s	system name (eg HP-UX)
	-v	operating system version (eg 8 for HP-UX 8.07)
	-b|B	base ISO name only (lower|upper case)
	-z	Lotus ISO conformant name for machine (lower-case)
	-Z	Lotus ISO conformant name for machine (upper-case - default)
	-q|Q	base, major, & minor seperated by vertical bars (lower|upper case)
	-a	all of the above (default is -Z for ISO name)"

DO_VERSION=0
DO_MODEL=0
DO_SYSTEM_NAME=0
DO_NODENAME=0
DO_ISONAME=0
DO_ISONAME_QUANTIFY=0
UPCASE=true
DO_REV=0
GET_VERSION=0
GET_REV=0
BASE_ISONAME_ONLY=false

VERSION=""
MODEL=""
SYSTEM_NAME=""
NODENAME=""
ISONAME=""
REV=""

parse_cmdline() 
{

    #
    #
    #

    if  [ -x /usr/bin/getopt -o -x /bin/getopt ]
    then
	set -- `getopt vbBmrasnzZaqQ $* 2>/dev/null`

	if [ $? -ne 0 ]; 
	then
		echo "$USAGE"
		exit 2
	fi

	if [ $# -eq 1 ] ; then
	  DO_SYSTEM_NAME=1
	else
	  for opt in $*
	  do
	    case $opt in
	      -v)
		DO_VERSION=1
		GET_VERSION=1
		;;
	      -b|-B)
		DO_ISONAME=1
		BASE_ISONAME_ONLY=true
		[ $1 = -b ] && UPCASE=false
		;;
	      -m)
		DO_MODEL=1
		;;
	      -s)
		DO_SYSTEM_NAME=1
		;;
	      -n)
		DO_NODENAME=1
		;;
	      -z|-Z)
		DO_ISONAME=1
		GET_VERSION=1
		GET_REV=1
		[ $1 = -z ] && UPCASE=false
		;;
	      -q|-Q)
                DO_ISONAME=1
		DO_ISONAME_QUANTIFY=1
                GET_VERSION=1
                GET_REV=1
		[ $1 = -q ] && UPCASE=false
		;;
	      -r)
		DO_REV=1
		GET_REV=1
		;;
	      -a)
		DO_VERSION=1
		DO_MODEL=1
		DO_SYSTEM_NAME=1
		DO_NODENAME=1
		DO_ISONAME=1
		DO_REV=1
		GET_VERSION=1
		GET_REV=1
		;;
	      *)
		;;
	    esac

	    shift
	  done
	fi
    else
	if [ $# -eq 0 ] ; then
	  DO_SYSTEM_NAME=1
	else
	    while getopts vbmrasnzZq c
	    do
		case $c in
		  v) 
		    DO_VERSION=1
		    GET_VERSION=1
		    ;;
		  b|B)
		    DO_ISONAME=1
		    BASE_ISONAME_ONLY=true
		    [ $c = b ] && UPCASE=false
		    ;;
		  m) 
		    DO_MODEL=1 
		    ;;
		  s)
		    DO_SYSTEM_NAME=1
		    ;;
		  n)
		    DO_NODENAME=1
		    ;;
		  z|Z)
		    DO_ISONAME=1
		    GET_VERSION=1
		    GET_REV=1
		    [ $c = z ] && UPCASE=false
		    ;;
		  q|Q)
                    DO_ISONAME=1
		    DO_ISONAME_QUANTIFY=1
                    GET_VERSION=1
                    GET_REV=1
  		    UPCASE=false
	            [ $c = q ] && UPCASE=false
		    ;;
		  r)
		    DO_REV=1
		    GET_REV=1
		    ;;
		  a)
		    DO_VERSION=1
		    DO_MODEL=1
		    DO_SYSTEM_NAME=1
		    DO_NODENAME=1
		    DO_ISONAME=1
		    DO_REV=1
		    GET_VERSION=1
		    GET_REV=1
		    ;;
		  \?) 
		    echo $USAGE
		    exit 2
		    ;;
		esac
	    done
        fi
    fi
}


get_majorminor()
{
	MINOR=`expr "$REV" : "\(.\).*"`
	if [ $ISONAME = "hppa" -o $ISONAME = "HPPA" ]; then
		MAJOR=$VERSION
	else
		MAJOR=`expr "$VERSION" : "\(.\).*"`
	fi
}


get_isoname() 
{
	# Check for base version only. I.e names will contain only
	# the O/S version (80, 90, etc).
	#
	get_majorminor
	if [ $BASE_ISONAME_ONLY = true ]; then
		return
	fi
	if [ $DO_ISONAME_QUANTIFY -eq 1 ]; then
		ISONAME="${ISONAME}|${MAJOR}|${MINOR}"
	else
		ISONAME="$ISONAME$MAJOR$MINOR"
	fi
}


do_hpux_stuff() 
{
       SYSTEM_NAME=$OS
       if [ $GET_VERSION -eq 1 -o $GET_REV -eq 1 ] ; then
         UNAME_DASH_R=`uname -r`
       fi
       if [ $GET_VERSION -eq 1 ] ; then
         VERSION=`expr "$UNAME_DASH_R" : "[A-Z]*\.\(.*\)\."` 
         VERSION=`expr "$VERSION" : "0*\(.*\)"`
       fi
       if [ $GET_REV -eq 1 ] ; then
         REV=`expr "$UNAME_DASH_R" : ".*\.\(.*\)"`
       fi
       if [ $DO_ISONAME -eq 1 -o $DO_MODEL -eq 1 ] ; then
	 MODEL="Unknown"
	 if [ $UPCASE = true ]
	 then
	 	ISONAME="HP"
	 else
	 	ISONAME="hp"
	 fi
	 if [ -x /bin/hp-pa -o -x /usr/bin/hp-pa ] ;
	 then
	   hp-pa
	   if [ $? -eq 0 ] ;
	   then
	     MODEL="PA"
	     if [ $UPCASE = true ]
	     then
	     	ISONAME=$ISONAME"PA"
	     else
	     	ISONAME=$ISONAME"pa"
	     fi
	   else
	     if [ -x /bin/hp-mc680x0 ] ;
	     then
	       /bin/hp-mc680x0
	       if [ $? -eq 0 ] ;
	       then
		 MODEL="Motorola"
		 if [ $UPCASE = true ]
	     	 then
	     		ISONAME=$ISONAME"68K"
	     	 else
	     		ISONAME=$ISONAME"68k"
	     	 fi
	       fi
	     fi
	   fi
	 fi
       fi
}


do_sun_stuff() 
{
       SYSTEM_NAME=$OS
       if [ $GET_VERSION -eq 1 -o $GET_REV -eq 1 ] ; then
	 UNAME_DASH_R=`uname -r`
       fi
       if [ $GET_VERSION -eq 1 ] ; then
	 VERSION=`expr "$UNAME_DASH_R" : "\([0-9]*\)\."`
       fi
       if [ $GET_REV -eq 1 ] ; then
	 REV=`echo $UNAME_DASH_R | sed -e 's/^[0-9]*//' -e 's/\.//g'`
       fi
       if [ $DO_ISONAME -eq 1 -o $DO_MODEL -eq 1 ] ; then
	 MODEL="Unknown"
	 if [ $UPCASE = true ]
	 then
		 ISONAME="SUN"
	 else
		 ISONAME="sun"
	 fi
	 if [ -x /bin/sparc ] ;
	 then
	   /bin/sparc
	   if [ $? -eq 0 ] ;
	   then
	     	MODEL="sparc"
	 	if [ $UPCASE = true ]
	 	then
	     		ISONAME=$ISONAME"SPA"
		else
	     		ISONAME=$ISONAME"spa"
		fi
	   else
	     	MODEL="INTEL"
	 	if [ $UPCASE = true ]
	 	then
	     		ISONAME=$ISONAME"X86"
		else
	     		ISONAME=$ISONAME"x86"
		fi
	   fi
	 fi
       fi
}


do_ibm_stuff() 
{
       SYSTEM_NAME=$OS
       if [ $GET_VERSION -eq 1 ] ; then
         VERSION=`uname -v`
       fi
       if [ $GET_REV -eq 1 ] ; then
         REV=`uname -r`
       fi
       MODEL="Power"
       if [ $UPCASE = true ]
       then
	       ISONAME="IBMPOW"
       else
	       ISONAME="ibmpow"
       fi
}


do_sco_stuff() 
{
       SYSTEM_NAME=SCO
       if [ $GET_VERSION -eq 1 -o $GET_REV -eq 1 ] ; then
	 UNAME_DASH_R=`uname -r`
       fi
       if [ $GET_VERSION -eq 1 ] ; then
	 VERSION=`expr "$UNAME_DASH_R" : "\(.*\)\."`
       fi
       if [ $GET_REV -eq 1 ] ; then
	 REV=`expr "$UNAME_DASH_R" : ".*\.\(.*\)"`
	 if [ "$REV" = "" ] ; then
	   REV=0
	 fi
       fi
       MODEL="INTEL"
       if [ $UPCASE = true ]
       then
	       ISONAME="SCOX86"
       else
	       ISONAME="scox86"
       fi
}

do_dec_stuff() 
{
       SYSTEM_NAME=$OS
       if [ $GET_VERSION -eq 1 -o $GET_REV -eq 1 ] ; then
	 UNAME_DASH_R=`uname -r`
       fi
       if [ $GET_VERSION -eq 1 ] ; then
	 VERSION=`expr "$UNAME_DASH_R" : "\(.*\)\."`
       fi
       if [ $GET_REV -eq 1 ] ; then
	 REV=`expr "$UNAME_DASH_R" : ".*\.\(.*\)"`
	 if [ "$REV" = "" ] ; then
	   REV=0
	 fi
       fi
       MODEL="MIPS"
       if [ $UPCASE = true ]
       then
	       ISONAME="DECMIP"
       else
	       ISONAME="decmip"
       fi
}

do_moto_stuff() 
{
	# R32V3.1
       SYSTEM_NAME=$OS
       if [ $GET_VERSION -eq 1 -o $GET_REV -eq 1 ] ; then
	 UNAME_DASH_R=`uname -r`
       fi
       if [ $GET_VERSION -eq 1 ] ; then
	 VERSION=`expr "$UNAME_DASH_R" : "R\([0-9]*\).\V"`
       fi
       if [ $GET_REV -eq 1 ] ; then
	 REV=`expr "$UNAME_DASH_R" : "R.\([0-9]*\)\V"`
       fi
       if [ $DO_ISONAME -eq 1 -o $DO_MODEL -eq 1 ] ; then
	 MODEL="Unknown"
	 if [ $UPCASE = true ]
	 then
		 ISONAME="MOT"
	 else
		 ISONAME="mot"
	 fi
	 if [ -x /bin/m88k ] ;
	 then
	   /bin/m88k
	   if [ $? -eq 0 ] ;
	   then
	     	MODEL="Motorola"
	 	if [ $UPCASE = true ]
	 	then
	     		ISONAME=$ISONAME"88K"
		else
	     		ISONAME=$ISONAME"88k"
		fi
	   else
		if [ -x /bin/m68k ]
		then
			/bin/m68k		
			if [ $? -eq 0 ]
			then
		     		MODEL="Motorola"
			 	if [ $UPCASE = true ]
		 		then
			     		ISONAME=$ISONAME"68K"
				else
		     			ISONAME=$ISONAME"68k"
				fi
			fi
		# else ?? pdp11, vax etc
		fi
	   fi
	 fi
       fi
}

do_unknown_stuff() {
       SYSTEM_NAME=UNKNOWN
       if [ $GET_VERSION -eq 1 ] ; then
	 VERSION=`uname -v`
       fi
       if [ $GET_REV -eq 1 ] ; then
	 REV=`uname -r`
       fi
       MODEL="Unknown"
       ISONAME="UNKNOWN"
       MODEL="UNKNOWN"
}


construct_output() 
{
       OUTS=""

       if [ $DO_SYSTEM_NAME -eq 1 ] ; then
         OUTS="$OUTS $SYSTEM_NAME"
       fi

       if [ $DO_NODENAME -eq 1 ] ; then
	 OUTS="$OUTS $NODENAME"
       fi

       if [ $DO_VERSION -eq 1 ] ; then
	 OUTS="$OUTS $VERSION"
       fi

       if [ $DO_REV -eq 1 ] ; then
	 OUTS="$OUTS $REV"
       fi

       if [ $DO_MODEL -eq 1 ] ; then
	 OUTS="$OUTS $MODEL"
       fi

       if [ $DO_ISONAME -eq 1 ] ; then
         get_isoname
	 OUTS="$OUTS $ISONAME"
       fi

}





parse_cmdline $*



OS=`uname -s`
case $OS in

     HP-UX) 
       do_hpux_stuff
       ;;

     SunOS) 
       do_sun_stuff
       ;;

     AIX*) 
       OS="AIX"
       do_ibm_stuff
       ;;

     ULTRIX)
	do_dec_stuff
	;;
     *)
       if [ -f /etc/scologin ] ;	# Serious hack ...
       then
	        do_sco_stuff
       else
		# Try this for an even worse hack!! (moto test)
		#
		if [ -x /bin/m68k -o -x /bin/m88k ]
		then
			do_moto_stuff
		else
		        do_unknown_stuff
		fi
       fi
       ;;

esac
  
if [ $DO_NODENAME -eq 1 ] ; then
    NODENAME=`uname -n`
fi


construct_output


echo $OUTS

