#! /bin/sh
#------------------------------------------------------------------------
# Get a list of processes
#------------------------------------------------------------------------
KEY='RUNDIR'
#pids="`ps -U $USER -u $USER -o pid | grep -v -i 'pid'`"
pids="`pgrep -U $USER -u $USER`"
results=''
#------------------------------------------------------------------------
# Setup the variable PENV which executes a set of commands to provide
#  a list of environment variables of the form <var>=<val><newline>
#------------------------------------------------------------------------
PARGV0="ps -p @pid@ -o comm | grep -v COMMAND"
PS="/bin/ps"
case "`uname -s`" in 
Linux)
  PENV="cat /proc/@pid@/environ 2> /dev/null | tr '\000' '\n'"
  ;;
SunOS)
  PENV='pargs -e @pid@ 2> /dev/null | egrep '\''^envp'\'' | sed -e '\''s/^envp\[[0-9]*\]: //'\'
  ;;
esac
if test -z "$pids"
then
  exit 0
fi
for pid in $pids
do
   cmd=`echo "${PENV}" | sed -e 's/@pid@/'$pid/g`
   a=`eval $cmd | sed -e 's/=/ /' | while read var val
   do
     case $var in
     ${KEY})
       echo $pid
       break
       ;;
     esac
   done`
   results="$results $a"
done
if test -z "$results"
then
  exit 0
fi
for pid in $results
do
  cmd=`echo "${PARGV0}" | sed -e 's/@pid@/'$pid/g`
  case "`eval $cmd`" in
  ssh|*/ssh)
    ;;
  *)
    newresults="$newresults $pid"
    ;;
  esac
done
results="`echo $newresults | sed -e 's/ /,/g'`"
if test -z "$results"
then
  exit 0
fi
${PS} -p $results $* 2> /dev/null
