#!/bin/tcsh -f
##------------------------------------------------------------------------------
## whichresource
##
## Use this csh script to determine which OPUS "resource" file is being
## used in the current environment.  "resource" is in quotes because I
## do not mean just files ending in ".resource", but instead I mean files
## that are resources for OPUS.  If the file extension is left off, then
## '.resource' is assumed.
##
## E.g. > whichresource opus_login.csh
##      /hstdev/store/sherbert/opus_definitions/OPUS-2012_4/opus_login.csh
##      > whichresource opus_login
##      OPUS_DEFINITIONS_DIR:opus_login.resource
##
## If used to find a file that does end in '.resource', then there is an 
## optional parameter (can be anything) that will report which TASK/COMMAND 
## that resource file calls.   This also clues the user in to whether this 
## process is an internal or external poller.  Internal pollers will only 
## use the TASK line, whereas external pollers TASK will be xpoll and there 
## will be a COMMAND line.  (Java processes do not play as nice, but you 
## will get the general idea.) 
## 
## E.g.  > whichresource bc_nca
##       /hstdev/project/sherbert/opus_ops/mine120917_184408//definitions/unix//bc_nca.resource
##       > whichresource bc_nca slls
##       /hstdev/project/sherbert/opus_ops/mine120917_184408//definitions/unix//bc_nca.resource
##       TASK = <xpoll -p $PATH_FILE -r bc_nca>
##       COMMAND = bc_nca.csh            ! Name of the command procedure
##       which bc_nca.csh 
##       /hstdev/project/sherbert/opus_ops/mine120917_184408/bin/linux/bc_nca.csh
##
##       > whichresource gc_wf3 lals
##       /hstdev/project/sherbert/opus_ops/mine120917_184408//definitions/unix//gc_wf3.resource
##       TASK = <gci_wfc3 -p $PATH_FILE -r gc_wf3>
##       which gci_wfc3 
##       /hstdev/project/sherbert/bin/linux/gci_wfc3
##
## Also, there is the optional 2nd argument "lit" (for literally).  Use this 
## for files that do not have extensions and you do not want '.resource' 
## appended.
## 
## E.g.  > whichresource.csh opus_corba_objs lit
##       /hstdev/store/sherbert/opus_definitions/OPUS-2012_4/opus_corba_objs
## 
## mm/dd/yy PR     Who        What
## -------- ------ ---------- --------------------------------------------------
## 09/28/12 72180  Sherbert   share with everyone else
##
##------------------------------------------------------------------------------

set OK     = 0
set NOT_OK = 1
set nm = `basename $0`

# echo "## Number of arguments: $#argv "  ## trace
if ( $#argv < 1 || $#argv > 2 ) then
    goto USAGE
endif

set cmdgrep = 0
set lit = 0

if ( $#argv == 2 ) then
    ## Is 2nd arg to be taken literally?  I.e. a resource file with no extension.
    ## If NOT (more likely), then user wants to know command run by resource file
    if ( $argv[2] !~ "lit" ) then
        ## want to run cmdgrep alias
        set cmdgrep = 1
        # echo "## Run cmdgrep after osfile_stretch_file" ## trace
    else
        ## indicate no extension expect: take filename literally
        set lit = 1
        # echo "## Take resource name literally" ## trace
    endif
endif

## If we are NOT to take the resource file name literally, ie. w/ no ext
## then we will assume missing ext should be ".resource"
## otherwise existing extension should be taken as specified

set extension = $argv[1]:e
if ( $extension == "" && ! $lit ) then
    set name = $argv[1].resource
else
    set name = $argv[1]
endif
# echo "## resource name is $name" ## trace


## get name of resource file
set name = `osfile_stretch_file OPUS_DEFINITIONS_DIR:$name`
echo $name

## get task|command info
if ( $cmdgrep ) then
    egrep -i "^task|^command" $name

    ## how can I 'which' the returned command?
    ## TASK and COMMAND are NOT in any set order, so what we really want
    ## is the 1st field after the "=" on the COMMAND line

  ## Make sure there was a COMMAND line, otherwise skip this
    set command = `egrep -i "^command" $name | awk -F'=' '{print $2}' | awk '{print $1}'`

    ## Sometimes the command will have a single quote attached and we need to 
    ## get rid of it so that the which command will work.
    if ( $#command == 0 ) then
#       echo "## No COMMAND line found"
        set command = `egrep -i "^task" $name | awk -F'=' '{print $2}' | awk '{print $1}' | cut -c 2-`
#       echo "## TASK is $command"
    else
	    if ( $command =~ \'* ) then
	        # echo "command contains a single quote "   ## trace
	        set command = `perl -e 'print substr($ARGV[0],1); ' $command `
	        # echo "command is $command"    ## trace
	    endif
	    echo "which $command "
	    which $command
	endif
endif

exit $OK

##------------------------------------------------------------------------------
USAGE:
    echo ""
    echo "This script finds a resource file in OPUS_DEFINITIONS_DIR, either a "
    echo "*.resource (the default) or another file considered an OPUS resource, "
    echo "like opus_corba_objs or keyword_source.dat.  "
    echo ""
    echo "Usage:"
    echo "     $nm <resource> [<cmdgrep>|lit]"
    echo ""
    echo "Where: "
    echo "       <cmdgrep> is an optional parameter to indicate user wants "
    echo "                 information of the process actually run by the "
    echo "                 *.resource file "
    echo ""
    echo "       lit       (literally) is the string used to indicate the first "
    echo "                 parameter does NOT have an extension and .resource "
    echo "                 should NOT be assumed "
    echo ""
    echo "Examples:"
    echo "     "
    echo "     % $nm keyword_rule.lis"
    echo ""
    echo "     /project/devcl/odosw1/dptbuild/robin/opus_ops14_0_b0/definitions//keyword_rules.lis"
    echo ""
    echo ""
    echo "     % $nm lstxxx anytext "
    echo ""
    echo "     /project/devcl/odosw1/dptbuild/robin/opus_ops14_0_b0/definitions/unix/lstxxx.resource"
    echo ""
    echo '     TASK = <xpoll -p $PATH_FILE -r lstxxx>'
    echo "     COMMAND  = podlst.pl            ! Name of the command procedure"
    echo ""
    echo "Illustratiions of the requirements: "
    echo "  $nm opus_corba_objs lit (want loc (location) of file w/o ext)"
    echo "  $nm keyword_rules.lis   (want loc of file w/given ext)"
    echo "  $nm dc_acs              (want loc of file w/default ext '.resource')"
    echo "  $nm unreserve cmdgrep   (want loc of file w/default ext AND "
    echo "                                 name and loc of script on TASK or COMMAND line)"
    echo ""
    exit $NOT_OK
