#!/bin/csh
## Compiling common lisp (or maxima)
## source programs (.lsp) into corresponding object 
## code or fasl file (.o) and distribute to all hosts given in hostfile
## Usage:  pvmlc/pvmmc [-U compiler] [-H hostfile] file.lsp ...
## Paul Wang, 9/10/95

## all different architectures on for pvm
set archs = (SUN4 HPPA SGI)
set user_main = $HOME/pvm3

set cmd = $0
if ( $#argv == 0 ) then
     echo "Usage: $cmd [-U compiler] [-H hostfile] file.lsp ..."
     exit(1)
endif

if ( $cmd:t == pvmlc ) then
   set CC = lc
else
   set CC = mc
endif
set hostfile = 0
set optdone = 0

while ( ! $optdone )
  switch ($1)
    case -U: ## compiler to use
         set CC = $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

## make name of wd relative to $home
set wd = `pwd | sed "/.*$home:t.pvm3/s//pvm3/"`
echo wd = $wd

## find hostfile
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
echo hostfile = $hostfile
set hosts = `sed '/^.*#/d' $hostfile`
set unprocessed = ()

if ( { $UTIL/pvmlc1 -U $CC $argv[*] } ) then
     echo Compilation succeeded on $HOST
     echo -n 'Do you wish to continue ? (y or n):'
     if ( $< == n ) then
         exit(0)
     endif
else
     echo Compilation failed on $HOST
     exit(1)
endif

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

foreach host ( $hosts )
     if ( $host == $HOST ) then
         continue
     endif
     set h_ar = `$RSH $host pvmgetarch`
     set done = 0
     foreach ar ( $archs )
         if ( $h_ar == $ar ) then
## echo matched $h_ar
             echo $host >>! ${ar}.${USER}$$
             set done = 1
             break
         endif
      end
      if ( $done == 0 ) then
         set unprocessed = ($unprocessed $host)
      endif
end

if ( $#unprocessed > 0 ) then
    echo Hosts $unprocessed will not be processed
endif

foreach ar ( $archs )
     if ( -f ${ar}.${USER}$$ ) then
         set hosts = `cat ${ar}.${USER}$$`
         echo Processing $ar hosts: $hosts
     else 
         continue
     endif
## compile and distribute on one remote host
     if ( $ar != $PVM_ARCH ) then
         set h = $hosts[1]
         shift hosts
         echo $RSH $h 'cd $home/'"$wd; $UTIL/pvmlc1 -U $CC $argv"
         $RSH $h 'cd $home/'"$wd; $UTIL/pvmlc1 -U $CC $argv"
     endif     
## distribute executable to remaining hosts
     if ( $#hosts > 0 ) then
          echo $hosts >! ${ar}.${USER}$$
          foreach file ( $argv:gt )
              set obj = $HOME/pvm3/bin/$ar/$file:r.o
              if ( -f $obj ) then
                  $UTIL/pvm_distrib -H ${ar}.${USER}$$ $obj
              endif
          end
     endif
end

cleanup:
set nonomatch
/bin/rm -f *.${USER}$$
exit(1)
