#!/bin/csh
## distribute pvm application executables to hosts
## Usage: pvm_distrib [-T architecture] [ -H hostfile ] a.out1 ...
## 6/28/94 Paul Wang
## Incorported suggestions by Mike Lewis 9/30/94

set USER_MAIN = $HOME/pvm3

## Check input
if ( $#argv == 0 ) then
    echo Usage: $0 [-T architecture] [ -H hostfile ] a.out1 ...
    echo     where executables are files on shared disks
    exit(1)
endif

set checkarch = 0
set hostfile = 0
set optdone = 0

while ( ! $optdone )
  switch ($1)
    case -T: ## target architecture
         set checkarch = $2
         shift; shift; breaksw
    case -H:
         if ( -f $2 ) then
             set hostfile = $2
             shift; shift; breaksw
         else 
             echo $2 file not found
             exit(1)
         endif
    default:
         set optdone = 1
  endsw
end

if ( $hostfile == 0 ) then
     if ( -f hostfile ) then            ## in cwd
          set hostfile = hostfile
     else if ( -f $USER_MAIN/hostfile ) then ## in user's pvm3
          set hostfile = $USER_MAIN/hostfile
     else
          echo hostfile not given/found
          exit(1)
     endif
endif
set hosts = `sed '/^.*#/d' $hostfile`

## executables to be distributed
set aouts = "$*"

## local disk directory to receive distributed executable
set ldsk = `ls -l $USER_MAIN/local_disk_bin`
set ldsk = $ldsk[$#ldsk]

if ( $PVM_ARCH == HPPA ) then
    set RSH = remsh
else
    set RSH = rsh
endif

# Distribution
foreach host ( $hosts )

    foreach exec ( $aouts )
        ## Check executable
        if ( $exec !~ /* ) then
           set exec = `pwd`/$exec
        endif
        if (! -f $exec || ! -r $exec ) then
            echo $exec not there or not readable
            continue  # to next exec
        endif      
        ## only designated arch gets distribution
        if ( $HOST == $host ) then
            if ($checkarch == 0 || `pvmgetarch` == $checkarch ) then
                 echo Distribute $exec:t to $host;
                 mkdir $ldsk >& /dev/null
                 cp $exec $HOME/pvm3/local_disk_bin/$exec:t
            endif
        else
            if ($checkarch == 0 || `$RSH $host pvmgetarch` == $checkarch) then
                 echo Distribute $exec:t to $host;
                 $RSH $host "mkdir $ldsk" >& /dev/null
                 rcp $exec ${host}:$HOME/pvm3/local_disk_bin/$exec:t
            endif
        endif
    end
end
