#!/bin/sh
#
#  arch
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__
#
#  The original idea of "aimk" where this script is based on came from the
#  PVM 3.x distribution, 22 Jul 1991 Robert Manchek manchek@CS.UTK.EDU. 
#
#  call:   arch       (print SGEEE architecture string)
#          arch -m    (print default MANPATH of system)
#          arch -mt   (print either "man" or "catman")
#          arch -lib  (print name of variable to extend shared library path)

# On some architectures, it is necessary to run the manpath(1) command
# with the user's path instead of our "safe" one.  Thus we need to
# save the old value of PATH for later restoration.
#
SAFE_PATH=/bin:/usr/bin:/usr/sbin
OLD_PATH=$PATH
PATH=$SAFE_PATH

ARCH=UNKNOWN

if [ -x /bin/uname ]; then
   UNAME=/bin/uname
elif [ -x /usr/bin/uname ]; then
   UNAME=/usr/bin/uname
else
   echo ERROR: \"uname\" command not found
   exit 1
fi

ossysname="`$UNAME -s`"
osmachine="`$UNAME -m`"
osrelease="`$UNAME -r`"

case $ossysname in
AIX)
   osversion=`$UNAME -v`
   case $osversion in
   3)
      ARCH=UNSUPPORTED-rs6000
      ;;
   4)
      case $osrelease in
      1)
         ARCH=UNSUPPORTED-aix41
         ;;
      2)
         ARCH=UNSUPPORTED-aix42
         ;;
      3)
         ARCH=aix43
         ;;
      esac
      ;;
   5)
      case $osrelease in
      1)
         ARCH=aix51
         ;;
      *)
         ARCH=UNSUPPORTED-aix-5-$osrelease
         ;;
      esac
      ;;
   esac
   ;;
CRSOS)
   if [ $osmachine = smp ]; then
      ARCH=craysmp
   fi
   ;;
Darwin)
   case $osmachine in
      i386)
         ARCH=darwin-x86
         ;;
      Power*)
         ARCH=darwin
         ;;
      *)
         darwin_machine=unsupported
         ;;
   esac
   ;;
FreeBSD|DragonFly)
   ARCH=fbsd-$osmachine
   ;;
NetBSD)
    osprocessor="`$UNAME -p`"
    ARCH=nbsd-$osprocessor
    ;;
IRIX*)
   case $osrelease in
   4*)
      ARCH=UNSUPPORTED-irix4
      ;;
   5*)
      ARCH=UNSUPPORTED-irix5
      ;;
   6.5)
      ARCH=irix65
      ;;
   6.*)
      ARCH=UNSUPPORTED-irix-$osrelease
      ;;
   esac
   ;;
HP-UX)
   case $osmachine in
   ia64)
      ARCH=hp11
      ;;
   9000/*)
      case $osrelease in
      *.09.*)
         ARCH=UNSUPPORTED-hp
         ;;
      *.10.10)
         ARCH=UNSUPPORTED-hp1010
         ;;
      *.10.*)
         ARCH=hp10
         ;;
      *.11.*)
         ARCH=hp11
         ;;
      esac
   esac
   ;;
Linux)
   case $osmachine in
   i*86)
     lxmachine=x86
     ;;
   alpha)
     lxmachine=alpha
     ;;
   sparc)
     lxmachine=sparc
     ;;
   sparc64)
     lxmachine=sparc
     ;;
   x86_64)
     lxmachine=amd64
     ;;
   ia64)
     lxmachine=ia64
     ;;
   ppc*)
     lxmachine=ppc
     ;;
   *)
     lxmachine=UNSUPPORTED-linux
   esac

   case $osrelease in
   2.2.*)
      lxrelease=22
      ;;
   2.[46].*)
      case $lxmachine in
      amd64)
        strings /lib64/libc.so.6 | grep "GNU C Library" | grep "2\.0" 2>&1 > /dev/null
        ;;
      ia64)
        strings /lib/libc.so.6.1 | grep "GNU C Library" | grep "2\.0" 2>&1 > /dev/null
        ;;
      *)
        strings /lib/libc.so.6 | grep "GNU C Library" | grep "2\.0" 2>&1 > /dev/null
      esac

      if [ $? -eq 0 ]; then
         lxrelease=UNSUPPORTED-linux2.2-glibc2.0
      else
         case $osrelease in
         2.4.*) 
            lxrelease=24
            ;;
         2.6.*) 
            if [ "$SGE_ROOT" != "" -a -d "$SGE_ROOT/bin/lx26-${lxmachine}" ] ; then
               lxrelease=26
            elif [ "$SGE_ROOT" = "" -a -d "bin/lx26-${lxmachine}" ] ; then
               lxrelease=26
            else
               lxrelease=24
            fi
            ;;
         esac
      fi
      ;;
   *)
      lxrelease=-UNSUPPORTED-$osrelease
   esac
   ARCH=lx${lxrelease}-${lxmachine}
   ;;
OSF1)
   case $osrelease in
   V4*)
      ARCH=osf4
      ;;
    V5*|T5*)
       ARCH=tru64
       ;;
    *)
       ARCH=UNSUPPORTED-osf1-$osrelease
       ;;
    esac
    ;;
SunOS)
   case $osmachine in
   sun4)
      ARCH=UNSUPPORTED-sun4
      ;;
   i86*)
      ARCH=sol-x86
      case $osrelease in
      5.[7891]*)
         if [ `isainfo -b` = 64 ]; then
            ARCH=sol-amd64
         else
            ARCH=sol-x86
         fi
      esac
      ;;
   *)
      case $osrelease in
      5.[7891]*)
         if [ `isainfo -b` = 64 ]; then
            ARCH=sol-sparc64
         else
            ARCH=sol-sparc
         fi
         ;;
      5.6)
         ARCH=sol-sparc
         ;;
      *)
         ARCH=UNSUPPORTED-sun4-$osrelease
         ;;
      esac
      ;;
   esac
   ;;
SUPER-UX)
   case $osmachine in
   SX-4*)
      ARCH=UNSUPPORTED-necsx4
      ;;
   SX-[56]*)
      ARCH=sx
      ;;
   *)
      ARCH=UNSUPPORTED-SUPER-UX-$osmachine
      ;;
   esac
   ;;
Interix)
   ARCH=win32-$osmachine
   ;;
*)
   if [ -f /unicos ]; then
      if [ "`$UNAME -m`" = "CRAY TS" ]; then
         if [ `/bin/target|/bin/grep ieee` = noieee ]; then
            ARCH=crayts
         else
            ARCH=craytsieee
         fi
      else
         ARCH=cray
      fi
   else
      ARCH=UNSUPPORTED-${ossysname}-${osmachine}-${osrelease}
   fi
   ;;
esac

if [ "$1" = "-m" -o "$1" = "-mt" -o "$1" = "-lib" ]; then
   MANTYPE=man
   SHARED_LIBRARY_PATH="LD_LIBRARY_PATH"
   DEFAULTMANPATH=/usr/man
   case $ARCH in
   aix*)
      DEFAULTMANPATH=/usr/man
      SHARED_LIBRARY_PATH="LIBPATH"
      ;;
   darwin*)
      DEFAULTMANPATH=`/usr/bin/manpath -q`
      SHARED_LIBRARY_PATH="DYLD_LIBRARY_PATH"
      ;;
   fbsd-*)
      PATH=$OLD_PATH
      DEFAULTMANPATH=`/usr/bin/manpath -q`
      PATH=$SAFE_PATH
      ;;
   hp*)
      DEFAULTMANPATH=/usr/man:/usr/contrib/man:/usr/local/man
      SHARED_LIBRARY_PATH="SHLIB_PATH"
      ;;
   irix65)
      DEFAULTMANPATH=/usr/share/catman:/usr/share/man:/usr/catman:/usr/man
      MANTYPE=catman
      ;;
   lx*)
      if [ "`which manpath 2>/dev/null`" != "" ]; then
         DEFAULTMANPATH=`manpath 2>/dev/null`
      else
         DEFAULTMANPATH=/usr/local/man:/usr/share/man:/usr/man:/usr/X11R6/man
      fi
      ;;
   sol-*)
      DEFAULTMANPATH=/usr/share/man
      # if bit-specific variable already set, use this variable!
      SHARED_LIBRARY_PATH_BITS="LD_LIBRARY_PATH_`isainfo -b`"
      if eval [ x\$$SHARED_LIBRARY_PATH_BITS != x ]; then
	      SHARED_LIBRARY_PATH=$SHARED_LIBRARY_PATH_BITS
      fi
      ;;
   tru64)
      DEFAULTMANPATH=/usr/share/man:/usr/dt/share/man:/usr/local/man
      ;;
   win32*)
      DEFAULTMANPATH=/usr/share/man
      MANTYPE=catman/cat
      ;;
   esac

   if [ "$1" = "-m" ]; then
      echo $DEFAULTMANPATH
   elif [ "$1" = "-lib" ]; then
      echo $SHARED_LIBRARY_PATH
   else
      echo $MANTYPE
   fi
else
   echo $ARCH
fi
