#!/bin/sh -
#
# rp: remote process-scanning on the host(s) given as argument(s), or on
#  all the student hosts if none given.
#
# usage: rp [-a] [-s <search-string>] <host>0+
#  -a for all processes (not just related to $USER), -s for string-searching.
#
progname=`basename $0`
pscommand="ps afSuwwwwwwwwwwx"
auxiliary=FALSE		#default
searchstring=""		#default


#
#	check for -a option
#
if [ $# -ne 0 ]; then
  if [ "x$1" = "x-a" ]; then
    auxiliary=TRUE ; shift
  fi
fi


#
#	check for -s option
#
if [ $# -ge 1 ]; then
  if [ "x$1" = "x-s" ]; then
    if [ $# -ge 2 ]; then
      searchstring="$2"
      shift ; shift
    else
      echo "usage: $progname [-a] [-s <search-string>] <host>0+" >&2
      exit 1
    fi
  fi
fi


#
#	check for named hosts
#
if [ $# -ne 0 ]; then
  hosts="$*"
else
  hosts=`sed 's/#.*$//' $LOCALLAB/lib/student_hosts.cf`
fi


#
#	OK, now the actual work!
#
if [ -z "$searchstring" ]; then #straight run

  for host in $hosts ; do
    if [ "x$auxiliary" = "xTRUE" ]; then
      ssh -n -x $host "$pscommand" 2>&1 | sed "s/^/$host:	/"
    else
      (ssh -n -x $host "$pscommand" | egrep "^USER|$USER") 2>&1 | sed "s/^/$host:	/"
    fi
  done #for host

else #search for given search-string

  for host in $hosts ; do
    if [ "x$auxiliary" = "xTRUE" ]; then
      (ssh -n -x $host "$pscommand" | egrep -i "^USER|$searchstring") 2>&1 | sed "s/^/$host:	/"
    else
      (ssh -n -x $host "$pscommand" | egrep "^USER|$USER" | egrep -i "^USER|$searchstring") 2>&1 | sed "s/^/$host:	/"
    fi
  done #for host

fi
