#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osirisCheckRPCInfo - checks if specified service is already registered 
#
#SYNOPSIS
#     osirisCheckRPCInfo service
#
#DESCRIPTION
#     Checks to see if the specified service is registered with RPC.  It does
#     this doing a 'rpcinfo' and looking for the correct PROGNUM.
#
#     If the service is registered, the username of the RPC entry is returned.
#     If the service is not registered, nothing is returned.
#
#OPTIONS
#     service
#          rpc server name to check
#
#EXAMPLES
#     osirisCheckRPCInfo ot1s
#         Checks to see if the ot1s server is registered with RPC.
#
#ENVIRONMENT VARIABLES
#     Oxxx_PROGNUM's
#
#FILES
#     none
#
#SERVERS & KEYWORDS
#     none
#
#SCRIPTS CALLED
#     help, syncheck, rpcinfo
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#     3 - error parsing command line input
#     other errors...
#
#SEE ALSO
#     ???
#-
#
# Modification History:
# 20050820 - MB: Initial version created
#

# Boiler plate for "-h" support for command autohelp.

if ("$1" == "-h") then
    help $0 | more
    exit $status
endif

# Boilerplate for syncheck.
# Note that the boiler plate should be transparent for all usages,
# but to make correct use of syncheck you will need to specify the
# correct pattern.

set noglob
set servlist = "[oids,osds,otcs,ot1s,ot2s,op1s,op2s,oprs,om1s,om2s,om3s,\
		 om4s,om5s,om6s,osiris,obbs]"
set CheckStatus = `syncheck -command $0 $* -pattern ${servlist}` 
unset noglob

if ("$CheckStatus" != "OK") then
    help $0 | more
    exit 2
endif

# End of help/syncheck boiler plate.

# Set up to trap interrupts (Ctrl-C, etc.)
onintr abort

# Set default variable values
set owner = ""

# Get the command line argument
set service = $1

# Formulate the environment variable name that stores the PROGNUM
set sNAME = `echo $service | gawk '{print toupper($0)}'`
set varname = "${sNAME}_PROGNUM"

# Check if the service is registered with RPC
set num = `env | grep $varname | cut -f2 -d"="`
#echo $num
set entry = `rpcinfo | grep $num`
#echo $entry
if ("${entry}" == "") then
    # No entry found, so return "nothing"
    echo $owner
else
    # Get the UID for the entry
    set rpcuid = `echo $entry | awk '{print $6}'`

    # Convert the UID to a username
    switch ($rpcuid)
	case 723:
	    set rpcname = osrsbld
	    breaksw
	case 724:
	    set rpcname = osrsdev
	    breaksw
	case 725:
	    set rpcname = osrseng
	    breaksw
	case 726:
	    set rpcname = osiris
	    breaksw
	case 738:
	    set rpcname = osiriseng
	    breaksw
	default:
	    set rpcname = unknown
	    breaksw
    endsw

    # Return the username of the rpcinfo entry
    echo $rpcname
endif

goto done

abort:
# Block of code to handle interrupts.
exit 1

done:
# is there anything that needs to go here?
exit