#! /bin/csh -f
#
# 03/05/09 CLF: extracted from pppl_trmpi_NxX.tsch
# Distribute beam particle files to mpi nodes for re-starts
#
if ( $#argv == 0 ) then
    echo "trmpi_mvnb <NPROCS> <NUBEAM_WORKPATH> <orig_dir> <suffix> <plist>"
    exit(1)
endif

  set NPROCS = $argv[1]
  set NUBEAM_WORKPATH = $argv[2]
  set orig_dir = $argv[3]
  set suffix = $argv[4]
  set plist = $argv[5]

  if ( ! -f $plist ) then
    if ( $?MPI_CMD) then
       eval 'set mpx = "'"$MPI_CMD"'"'
    else
       set mpx = `which mpirun`
    endif  
    $mpx -np $NPROCS `which mpi_proclist` $plist
  endif
  set cpnums = `cat $plist | awk '{print $1}'`
  set cpnames = `cat $plist | awk '{print $2}' | sed 's#\..*##'`

  @ jj = 0 
  set tlist_1 = ()
  set tlist_2 = ()
  while ( $jj < $#cpnums )
    @ jj++
    echo " cpu $cpnums[$jj] found on node $cpnames[$jj] "
    if ( $jj > 1 ) then
      set tlist_1 = ( $tlist_1 _cpu$cpnums[$jj]_$#cpnums )
      set tlist_2 = ( $tlist_2 nubeam_comp_$cpnums[$jj] )
    endif
  end

  set hlist = ( status_summary )
  set dlist = ( _debug_ _xplasma_  dep-buf-out orb-buf-out _3dhalo_ )

  echo " "
  echo " distribute NUBEAM_WORKPATH work files... "
  set clist = `ls $orig_dir/$NUBEAM_WORKPATH`
  @ jj = 0
  while ( $jj < $#clist )
    @ jj++
    set cfile = $clist[$jj]

    @ ktarg = 0
# check for deletion
    @ kk = 0
    while ( $kk < $#dlist )
      @ kk++
      set dstr = $dlist[$kk]
      set dtest = `echo $cfile | sed "s#$dstr##"`
      if ( $dtest != $cfile ) then
        # match -- for deletion
        @ ktarg = -1
      endif
    end

# check for no action
    if ( $ktarg == 0 ) then
      @ kk = 0
      while ( $kk < $#hlist )
        @ kk++
        set tstr = $hlist[$kk]
        set ctest = `echo $cfile | sed "s#$tstr##"`
        if ( $ctest != $cfile ) then
          # match
          @ ktarg = -2
        endif
      end
    endif

# check lists -- determine destination
    if ( $ktarg == 0 ) then
      @ kk = 0
      while ( $kk < $#tlist_1 )
        @ kk++
        set tstr = $tlist_1[$kk]
        set ctest = `echo $cfile | sed "s#$tstr##"`
        if ( $ctest != $cfile ) then
          # match
          @ ktarg = $kk
        endif
      end
    endif

    if ( $ktarg == 0 ) then
      @ kk = 0
      while ( $kk < $#tlist_2 )
        @ kk++
        set tstr = $tlist_2[$kk]
        set ctest = `echo $cfile | sed "s#$tstr##"`
        if ( $ctest != $cfile ) then
          # match
          @ ktarg = $kk
        endif
      end
    endif

    if ( $ktarg == -1 ) then
      echo " delete $cfile"
      echo " rm -f $orig_dir/$NUBEAM_WORKPATH/$cfile"
      rm -f $orig_dir/$NUBEAM_WORKPATH/$cfile

    else if ( $ktarg == -2 ) then
      echo " leave in place: $cfile "

    else
      #  make index 1,2,3,...,N instead of 0,1,2,...,N-1

      @ ktarg++ 

      set target = /l/$cpnames[$ktarg]/$suffix/$NUBEAM_WORKPATH/$cfile
      # do not move if already there (production runs)
      if ( ! -f $target ) then
        if ( $ktarg == 1 ) then
          echo " move $cfile to $cpnames[$ktarg]"
        else    
          echo " move $cfile to $cpnames[$ktarg] for $cpnums[$ktarg]"
        endif
        mv $orig_dir/$NUBEAM_WORKPATH/$cfile $target
         if ( $status != 0 ) then
          echo " mv status not 0 -- exiting... "
          exit 1
        endif
      endif
    endif

  # end of file loop
  end
