#!/bin/csh -f
#
# Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved. 
#
#   NAME
#     canbe
#
#   DESCRIPTION
#     Oracle Secure Backup tool to define what roles are available per platform
#
#   MODIFIED   (MM/DD/YY) 
#   kdayal     06/16/10 - Added solarisx86_64 platform in supporting roles table
#   jwadleig   11/11/08 - Merge from 10.2 -- Remove AIX client only restriction
# ******************************************************************************
#  Old changes (pre ade migration):
#  Changes:
#     08/04/06 asm  Restrict AIX as client only
#     07/31/06 jew  Added linux86_64 and linuxia64
#     05/26/05 rvv  Added hp64pa, hp64ia & ibm64
#     10/12/04 rvv  replace linux86-lfs with linux32, remove unsupported 
#                   or yet unavailable architectures from the list  
#     06/11/04 rvv  add solaris64
#     07/15/02 jrw  sparc is now client only.
#     06/15/02 jrw  update copyright/IP notice.
#     02/21/02 jrw  add NetBSD.
#     12/03/01 gms  removed ncr86; v3 mods.
#     11/27/01 gms  removed sparc server.
#     06/27/01 jrw  add linux86-lfs.
#     04/13/01 jrw  remove domain68.
#     11/16/00 gms  don't count ENDOFLIST entry in -mach_archs.
#     11/16/00 cmg  add ENDOFLIST to signal the end of platform table
#     10/16/00 jrw  remove hppa, hp800, decult.
#     05/28/00 jrw  add freebsd-elf.
#     02/12/00 jrw  add alphalinux.
#     10/03/99 jrw  remove gui from linux86.
#     08/30/99 jrw  support FreeBSD.
#     08/21/99 jrw  remove sgi4.
#     08/19/99 jrw  support linux GUI/scheduler configuration.
#     08/15/99 jrw  support solaris for intel (solx86).
#     07/15/99 gms  linux* now servers.
#     04/07/99 gms  allow nt to be a server.
#     02/26/99 adp  support Linux with glibc.
#     01/03/99 jrw  add "-set" function to show everything a platform can be.
#     12/31/98 jrw  support pseudo-architecture "all" for CD-ROM encode/decode tools.
#     12/21/98 jrw  add architecture number, abbreviation name.
#     12/17/97 jrw  support Linux.
#     04/07/97 gms  mods for hpvxfs.
#     01/08/97 gms  added ntx86; removed sun68; removed hp68 & domain68 gui.
#     12/01/96 jrw  port to alpha.
#     07/14/95 jrw  add sgi4.
#     09/01/94 mbc  enable sgi 
#     08/15/94 mbc  allow domain68 gui.	
#     07/26/94 jrw  more changes for SCO.
#     07/20/94 jrw  ditto gms' change (below) for hp68.
#     07/19/94 gms  added hp800 server, admin, gui, all.
#     07/18/94 gms  added sco.
#     05/13/94 gms  merged ncr branch to mainline.
#     05/09/94 gms  added ncr86.
#     04/29/94 mbc  solaris can now be gui
#     03/17/94 kcl  initial coding.
#
#  NOTE: This little script is completely independent of any environmental or
#  inherited variables. It is used by both the "produce" and the shipped install
#  scripts.
#
#  Usage:
#     <see help text at the bottom of this file>
#
#

if ( $#argv == 0 ) goto usage

#  Notes:
#   1. It is important that "server" & "gserver" occur before "admin" & "gadmin".
#   2. Don't use archno 0.  The max archno supported by the cdencode and cddecode
#      programs is 62.  Anything higher than that requires mods there.

#          architecture         5 char
#              #  name          abbrev            s u p p o r t e d   r o l e s

set configs =                          ( client  gclient   server   gserver  admin   gadmin  )
set confabb =                          ( cl      gc        se       gs       ad      ga      )

set table = (  1  hp64ia         hp64i    X        X        X        X        X        X    \
               2  hp64pa         hp64p    X        X        X        X        X        X    \
               3  ibm64          ibm64    X        X        X        X        X        X    \
               4  linux32        lx32     X        X        X        X        X        X    \
               5  linux86_64     lx64     X        X        X        X        X        X    \
               6  linuxia64      lx64i    X        X        X        X        X        X    \
               7  ntx86          ntx86    X        -        X        -        -        -    \
               8  solaris        solar    X        X        X        X        X        X    \
               9  solaris64      sol64    X        X        X        X        X        X    \
              10  solarisx86_64  sx64     X        X        X        X        X        X    \
              11  ENDOFLIST      END      -        -        -        -        -        -    \
            )

@ items_per_row = 3 + $#configs
set all_archno  = 63              # special arch # representing "everything"
set value       = "lookup_error"  # if not changed, we will set exit to 1

if ( $#argv != 2 ) then

    if ( $#argv == 1 ) then

        if ( "$argv[1]" == "-configs" ) then
            echo $configs
            exit ( 0 )

        else if ( "$argv[1]" == "-mach_archs" ) then
            @ row_start = 2
            @ nrows = $#table - $items_per_row
            while ( $row_start < $nrows )
            	echo -n "$table[$row_start] "
            	@ row_start += $items_per_row
            	end
            echo ""
            exit ( 0 )
            endif

        endif

    # if we haven't exited, take the "error" exit...

    goto exit_lookup

    endif  # ( $#argv != 2 )


# past here if 2 args (they may be bad, but we must do a look-up to find out...)

set which_arch   = $argv[1]


# if our user wants the architecture name, given either a package number or
# an abbreviation, determine that here:

if ("$argv[2]" == "-name") then
    @ row_start = 1
    while ( $row_start < $#table )
        if ( $table[$row_start] == $which_arch ) break
        @ rsix = $row_start + 2
        if ( $table[$rsix] == $which_arch ) break
        @ row_start += $items_per_row
        end

    if ( $row_start >= $#table ) then
        if ($which_arch == $all_archno) then
            echo "all"
        else
            echo ""
            endif
    else
        @ row_start += 1
        echo $table[$row_start]
        endif
    exit (0)
    endif


# look up the starting index of the mach_arch in the table...

@ row_start = 2
while ( $row_start < $#table )
    if ( $table[$row_start] == $which_arch ) break
    @ row_start += $items_per_row
    end


#  Write a string to stdout which, when interpreted by the C-shell, will set a
#  list of variables named p_cl, p_gc, p_se, p_gs, p_ad, p_ga to 0 or 1
#  corresponding to what this platform can do:  1 if it can play that role, 0
#  if not.
#
if ("$argv[2]" == "-set") then
    if ($row_start >= $#table) then
        echo ""
        exit (0)
        endif

    @ row_start += 1
    @ config_ix = 0
    set echostr = "set"

    while ($config_ix < $#configs)
        @ config_ix += 1
        @ row_start += 1
        if ("$table[$row_start]" == "X") then
            set echostr = "$echostr p_$confabb[$config_ix]=1"
        else
            set echostr = "$echostr p_$confabb[$config_ix]=0"
            endif
        end

    echo "$echostr"
    exit (0)


#  Print architecture number:
#
else if ("$argv[2]" == "-archno") then
    if ( $row_start >= $#table ) then
        if ("$which_arch" == "all") then
            echo $all_archno
        else
            echo "0"
            endif
        exit (0)
    else
        @ row_start = $row_start - 1
        echo $table[$row_start]
        exit (0)
        endif


#  Print architecture name abbreviation:

else if ("$argv[2]" == "-abbrev") then
    if ( $row_start >= $#table ) then
        echo ""
        exit (0)
    else
        @ row_start = $row_start + 1
        echo $table[$row_start]
        exit (0)
        endif
    endif


set which_config = "$argv[2]"
if ( $row_start >= $#table ) goto exit_lookup


if ( "$which_config" == "any" ) then
                                      # caller just wants to know if this is
	set value = "X"                   # a known mach_arch...
	goto exit_lookup
	endif

# look up the index of the config (== row offset in table rel mach_arch)...

@ config_index = 1

while ( $config_index <= $#configs )
	if ( $configs[$config_index] == $which_config ) break
	if ( $confabb[$config_index] == $which_config ) break
	@ config_index++
	end

if ( $config_index > $#configs ) goto exit_lookup


# mach_arch and config OK: do the table look-up

@ index = $row_start + 1 + $config_index

set value = $table[$index]



exit_lookup:


if ( "$value" == "X" ) then
    echo 1
else
    echo 0
    endif

if ( "$value" == "lookup_error" ) exit ( 1 )

exit ( 0 )

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
usage:

#  1) Given a "mach_arch" and a "configuration", echo "1" to stdout if the given
#     machine can be that flavor host, else echo "0". If either of the args are
#     invalid the exit code is set to 1 (and we echo "0"), else it is set to 0.
#     The 2nd arg may be "any", in which case "1" is output if the mach_arch
#     is known, else "0" is.
#
#  2) Or, with arguments "-mach_archs" or "-configs", echo the known list of
#     values of the indicated category and set the exit code to 0.
#

echo "  Usage:"
echo '     set can_be = `canbe <mach_arch> {<config> | any}`  # yields 1 or 0'
echo "  or"
echo '     set alist = ( `canbe {-mach_archs | -configs}` )   # yields list'
exit ( 0 )
