#!/bin/csh
##############################################################################
# FILE:  pvmtidy
# DESCRIPTION:  
#   Used to cleanup hopelessly lost PVM daemons, user processes and the files
#   (pvmd.uid and pvml.uid) that some PVM versions leave behind.  
#
#   Usage:  pvmtidy  <hostfilename> < -u option> <u args>
#
#   Argument 1: hostfilename 
#     Required.  Contains the name of machines where pvmtidy should do 
#     its work.  Generally, this is your PVM hostsfile, but it can be any file
#     which contains one machine name per line.  The full path name should be 
#     specified if the file is not in the current directory.  Valid hostnames 
#     are assumed to be lines that begin with a-z, A-Z.  Lines that begin 
#     with other characters will be ignored.  All "extra" characters on a 
#     valid hostname line will be ignored (only the hostname is extracted).
#   Argument 2: -u  
#     Optional.  No other options are valid at this time.  This option tells 
#     pvmtidy to kill other user processes in addition to PVM daemons.  
#     Only daemons will be killed by default.  If the -u option is used, the 
#     names of user processes should follow it on the command line.  If -u
#     is used without u args, then it is ignored and only PVM daemons will
#     be killed.
#   Arguments 3 - N: u args 
#     These are the names of user processes which should be killed.  Care 
#     should be taken with specifying the u args since any process with a 
#     name that contains the search string will be killed.  For example, if 
#     the command:
#       pvmtidy myhostfile -u junk
#     is issued, then all of the following processes would be killed:
#       junk  junk1 junkproc  anyjunk   somejunkhere  /tmp/junk
#
#  "pvmtidy" maintained by:
#  Minwen Lo
#  National Institute of Standards and Technology
#  Building 225, Room A29
#  Gaithersburg, MD  20899
#  (301) 975-2969
#  mlo@raven.nist.gov
#
#  "pvmtidy" originally written by: Blaise Barney
#
##############################################################################

##### Step 1. Make sure user has supplied name of pvm hostfile
if ($#argv == 0) then
   echo '  usage:  pvmtidy <hostfile-name> < -u > < u args >'
   echo '  You need to supply the name of your PVM hostfile.  Try again.'
   exit(0)
endif

##### Step 2. Check for existence of PVM hostfile
set hostfile=$argv[1]
if (! -e $hostfile) then
   echo "  Can not find the PVM hostfile: $hostfile"
   echo '  Check for misspelling or complete pathname and try again.'
   exit(0)
else
   echo "pvmtidy will use your PVM hostfile: $hostfile"
endif

##### Step 3. Extract host names from the pvm hostfile 
set pvmhosts=`cat $hostfile | egrep "^[a-z,A-Z]" | awk '{print $1}'`

##### Step 4. Determine if the -u option is being used and process accordingly
if ($#argv >= 2) then
  if ("$argv[2]" != "-u") then
    echo '  usage:  pvmtidy <hostfile-name> < -u > < u args >'
    echo '  Command line incorrect.  Try again.'
    exit(0)
  else if ("$argv[2]" == "-u") then
    if ($#argv < 3) then
      echo '  usage:  pvmtidy <hostfile-name> < -u > < u args >'
      echo '  -u option used without arguments...ignored'
      echo '  Continuing anyway...'
    else
      if ( -e ~/PvmProcsToKill ) then
         rm ~/PvmProcsToKill
      endif
      touch ~/PvmProcsToKill
      foreach c_arg ($argv[3-$#argv])
         echo $c_arg >> ~/PvmProcsToKill
      end
      foreach thishost ($pvmhosts[*])
        rcp ~/PvmProcsToKill ${thishost}:"~/PvmProcsToKill"
      end
    endif
  endif
endif

##### Step 5. Remote shell to each host and call pvm_rshtidy on each host
#####         to do the actual cleanup work. 
set arch=`pvmgetarch`
switch ( $arch )
     case "CRAY":
     case "HPPA":
        set remote_sh="remsh"        
        breaksw
     default:
        set remote_sh="rsh"        
     breaksw
endsw

set pvmhosts=`cat $hostfile | egrep "^[a-z,A-Z]" | awk '{print $1}'`
foreach thishost ($pvmhosts[*])
   echo "   Cleaning up on host: $thishost"
   $remote_sh $thishost pvm_rshtidy 
end

##### Step 6. Cleanup the files created to hold options
if ( -e ~/PvmProcsToKill ) then
  rm ~/PvmProcsToKill
  echo "   Cleanup the files created to hold options"
  foreach thishost ($pvmhosts[*])
    if ( -e ~/PvmProcsToKill ) then
      rm ~/PvmProcsToKill
    endif
  end
endif

echo 'Done.'
