#!/bin/csh
##############################################################################
# FILE:  pvm_rshtidy
# DESCRIPTION: Called by pvmtidy.  Will remove leftover pvm files (pvm?.uid) 
#   and kill whatever pvmd processes it can find.  If user specified the
#   -u option with call to pvmtidy, then the user processes will be killed.
#
#  "pvm_rshtidy" maintained by:
#  Minwen Lo
#  National Institute of Standards and Technology
#  Building 225, Room A29
#  Gaithersburg, MD  20899
#  (301) 975-2969
#  mlo@raven.nist.gov
#
#  "pvm_rshtidy" originally written by: Blaise Barney
#
##############################################################################
set pvmlogdir="/tmp"
set arch=`pvmgetarch`
switch ( $arch )
     case "RS6K":
        set ps_opt="uxw"        
        breaksw
     case "CNVX":
        set pvmlogdir="/tiber/support/pvm3/log"
        set ps_opt="uxw"        
        breaksw
     case "NEXT":
     case "NEXT586":
        set ps_opt="uxw"        
        breaksw
     case "SUN4":
        set ps_opt="uxw"        
        breaksw
     case "CRAY":
        set pvmlogdir="/home/support/pvm3/log"
        set ps_opt="-ef"        
        breaksw
     case "SGI":
     case "SGI5":
     case "HPPA":
        set ps_opt="-ef"
     breaksw
     default:
        set ps_opt=" "
     breaksw
endsw

##### Step 1.  Get rid of the pvm?.uid files
set myuid=`getuserid`
if ( (-e ${pvmlogdir}/pvmd.$myuid) || \
     (-e ${pvmlogdir}/pvml.$myuid) || \
     (-e ${pvmlogdir}/pvmu.$myuid)) then
  echo "      Removing `ls ${pvmlogdir}/pvm?.$myuid`"
  rm ${pvmlogdir}/pvm?.$myuid
endif

##### Step 2.  Kill any process which smells like a pvm daemon
set pvmdpids=`ps ${ps_opt}|grep pvmd|grep -v grep|grep -v tidy|awk '{print $2}'`
foreach killpid ($pvmdpids[*])
  kill -9 $killpid
  echo "      Killed pvmd process $killpid"
end

##### Step 3.  Kill any additional processes specified by user
if ( -e ~/PvmProcsToKill) then
  set pvmprocs=`cat ~/PvmProcsToKill`
  foreach pvmproc ($pvmprocs[*])
    set pvmdpids=`ps ${ps_opt}|grep -v grep|grep $pvmproc|grep -v tidy|awk '{print $2}'`
    foreach killpid ($pvmdpids[*])
      kill -9 $killpid
      echo "      Killed user process: $pvmproc $killpid"
    end
  end
endif
