#!/bin/csh -f
#+
#PROCSTATUS - OSIRIS library of scripts
#
#NAME
#     procstatus - gives the status of a given process
#
#DESCRIPTION
#     procstatus is a wrapper for the ps command
#
#OPTIONS
#     process
#
#     -H runhost, defaults to localhost
#
#     -U runuser, run as a specific user
#
#EXAMPLES
#     procstatus mds -H kaimana     will give status for mds process (detector server)
#
#-
#
# Modification History:
# 20101118 - CAJ: created from scratch, OSIRIS reference script does not exist
# 20210405 - jlyke: Stolen back from MOSFIRE
#

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

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

# Check that one required argument (process) is present
if ( $#argv < 1 ) then
    help $0 | more
    exit 3
endif

# Get the required arguments passed in
set process=$1
shift
set runhost=`hostname`
set runuser=`whoami`

#echo argv = $#argv

while ($#argv != 0)
    switch ($1)
	case -H:
	    set runhost=$2
	    shift
	breaksw

	case -U:
	    set runuser=$2
	    shift
	breaksw
    endsw
    shift
end

if ($runhost == "localhost") then
    set runhost = `hostname`
endif

#echo process = $process
#echo runhost = $runhost

# Check to to see what host we are on, and check the process
set psargs = "ppid pid pgid uid vsz rss pcpu time command"
if ($runhost != `hostname`) then
  ssh $runuser@$runhost ps -eo "$psargs" | egrep "$process|UID" | grep -v grep | grep -v status
else
  ps -eo "$psargs" | egrep "$process|UID" | grep -v grep | grep -v status
endif
