#!/usr/local/bin/tcsh   -f
#  distjob: distribute jobs over many hosts
#   /bin/csh  does not work at line ##%%##
#   

if ($#argv == 0) then
    echo " Usage: distjob configFile [host host ...]"
    exit 1
endif
if (! -f $1)  then
    echo $1 not exists
    exit 1
else
    echo config file is: $1 
endif



#  get base directory to store data
set datadir=`awk '$1=="datadir" {print $2}' $1`
if ( "x$datadir" == "x") then
  echo  no datadir in config file 
  exit 1
endif
echo $datadir is the base directory to store data
echo "If this dose not exist, it will be created automatically."
#     get execution prog path name
set progpath=`awk '$1=="prog" {print $2}' $1`
if ( "x$progpath" == "x") then
    echo prog in config file not exists
        exit 1
    else if(! -f $progpath) then
        echo prog path $progpath not exists
        exit 1
    endif
endif

#    get last name of the path
    set prog=$progpath:t
echo  $prog

#  get file for data
set datafile=`awk  '$1=="datafile" {print $2}' $1`
if ( "x$datafile" == "x") then
  echo  no datafile in config file 
  exit 1
endif
echo $datafile will be the name of each data file as 
echo $datadir/hostname/$datafile


#       get append or new flag
set add=`awk '$1=="append" {print $2}' $1`
if ( "x$add" == "x" ) then
  echo "append in config file not exists"
  exit 1
endif
if ( $add == "yes" ) then
else if ( $add == "no" ) then
else
  echo "$add for append is no good in $1"
  exit 1
endif

#  get input namelist parameter specification file
    set input=`awk '$1=="input" {print $2}' $1`
    if ( "x$input" == "x") then
        echo input in $1 not exists
        exit 1
    else if(! -f $input) then
       echo $input not exists
       exit 1
    endif
    echo $input is the namelist parameter file.

#  get parameter output base dir
    set paramOut=`awk '$1=="paramOut" {print $2}' $1` 
    if ( "x$paramOut" == "x") then
	echo paramOut not exists in $1
	exit 1
    else if (! -d $paramOut ) then
	echo directory $paramOut not exists
	exit 1
    endif
    echo $paramOut is the base directory to save parameters
#    get alive 
    set alive=`awk '$1=="alive" {print $2}' $1` 
    if ( "x$alive" == "x") then
	echo alive not exists in $1
	exit 1
    else if (! -f $alive ) then
	echo alive file  $alive not exists
	exit 1
    endif
    echo $alive contains the host list as follows:
    cat $alive


    set qm="'"

#       get Cont
    set x=`awk '$1=="Cont" {print $3}' $input`   
    if( "x$x" == "x" ) then
       echo  "Cont = is missing"
       exit 1	
    endif
#      remove ' and ,
#    set xx =  $x:s/,//   this is ng in csh(tcsh is ok)
    set xx=`echo $x | tr -d ,`
    set cont=`echo $xx | tr -d $qm`
    echo  "Cont = " $cont

    if ( $cont == "F" || $cont == "f" ) then       
#      get primary file name which is specified in $input
      set x=`awk '$1=="PrimaryFile" {print $3}' $input`
      if( "x$x" == "x" ) then
         ehco  no PrimaryFile in $input
         exit 1
      endif
#       x is now like: 'PrimarySample',
#      or              'PrimarySample 
#      remove ' and ,
#      set xx=$x:s/,//
      set xx=`echo $x | tr -d ,`
      set primary=`echo $xx | tr -d $qm`
      echo Primary is given in $primary. Its copy will be used.

#      get cutoff file name which is specified in $input
      set x=`awk '$1=="CutOffFile" {print $3}' $input`
      if( "x$x" == "x" ) then
         echo  "no CutOffFile = in $input"
         exit 1
      endif
#       x is now like: ' ',
#      or              'Somefile', or 'somefile   
#      remove ' and ,
#      set xx=$x:s/,//          this is not valid for csh( tcsh is ok)
      set xx=`echo $x | tr -d ,`
      set cutoffin=`echo $xx | tr -d $qm`
      echo CutOffFile is  $cutoffin.
  endif
#       get Job
  set x=`awk '$1=="Job" {print $3}' $input`   
  if( "x$x" == "x" ) then
     echo  "Job = is missing"
     exit 1	
  endif
#      remove ' and ,
#  set xx=$x:s/,//
  set xx=`echo $x | tr -d ,`
  set job=`echo $xx | tr -d $qm`

  if ( $job != 'skeleton' && $job != 'flesh'  && x$job != "x" ) then
      echo " Job = " $job in namelist invalid
      exit 1
  endif	



######################################################################

@ count=0
foreach machine (`cat $alive`)
onintr -
   if(  $#argv >= 2) then
     if(`echo "$argv[2*]" | gawk '{if(index($0, h) >0 ) print "x";else print "y"}' h=$machine -` == "y") continue
     endif	

if (  $cont == "T" || $cont == "t" ) then
   if($count == 0 ) then
	if($add == 'no') then
	  echo "**************You specified Cont = t but **************"
          echo "              no append mode is given in config file"
          echo "              Is it O.K ?  "
          echo " Eneter 1 if O.K else other to stop the job "
          set ans=$< 
	  if ( $ans != 1 ) exit 1
        endif
#        get  DestEventNo
        set x=`awk '$1=="DestEventNo" {print $3}' $input`   
        if( "x$x" == "x" ) then
	     echo  "DestEventNo = is missing in namelist"
	     exit 1	
        endif	
	
#        remove ' and ,
#        set xx=$x:s/,//
        set xx=`echo $x | tr -d ,`
        set dest1=`echo $xx | tr -d $qm`

        set x=`awk '$1=="DestEventNo" {print $4}' $input`   
#        remove ' and ,
#        set xx=$x:s/,//
	set xx=`echo $x | tr -d ,`
        set dest2=`echo $xx | tr -d $qm`

        @  count++
    endif       
#        old saved parameters should be ready in   $paramOut/$machine/param
#        put Cont in saved parameters; everything is given there.

    gawk '$1=="Cont" {print " Cont = t,"; next }  \
         {print}'  \
         $paramOut/$machine/param > /tmp/param$$
#          copy back it to  param
       cp   /tmp/param$$   $paramOut/$machine/param
       rm -f /tmp/param$$

#         DestEventNo can be renewed by $input; change the value in ContInfo
    gawk '$1=="DESTEVENTNO" || $1=="DestEventNo" {print " DestEventNo = ", dest1, ",", dest2, ","; next} \
       {print}'  \
       dest1=$dest1 dest2=$dest2 \
       $paramOut/$machine/ContInfo > /tmp/continfo$$
#         copy back to ContInfo
       cp /tmp/continfo$$  $paramOut/$machine/ContInfo
       rm -f /tmp/continfo$$

else if ( $job == "flesh" ) then

#        old parameters are in $paramOut/$machine/param and 
#        for flesh job, skeletonFile must be used, with some
#        modification
#    first, put Job = 'flesh' in saved parameters

	echo ffile=$qm"flesh"$qm
#         above is needed ? to make the next valid
    gawk  '$1=="Job" {print " Job =", ffile, ","; next }  \
          $1=="Cont" {print " Cont = f,"; next }  \
          {print}' \
          ffile=$qm"flesh"$qm \
          $paramOut/$machine/param > /tmp/param$$

#         copy back it to param
         cp /tmp/param$$  $paramOut/$machine/param 
         rm -f /tmp/param$$
#       ################ except for Job='flesh', everything is
#       read from skeletonFile
#        modify skeltonFile; first get some values

     if( $count == 0 ) then
#          get ObsPlan
       set x=`awk '$1=="ObsPlane" || $1=="OBSPLANE" {print $3}' $paramOut/$machine/Skeleton`   
       if( "x$x" == "x" ) then
          echo  "ObsPlane = is missing in skeleton file"
          exit 1	
       endif
#        remove ' and ,
#       set xx=$x:s/,//
       set xx=`echo $x | tr -d ,`
       set obsplane=`echo $xx | tr -d $qm`
##
#         get reverse
       set x=`awk '$1=="Reverse" || $1=="REVERSE" {print $3}' $paramOut/$machine/Skeleton`   
       if( "x$x" != "x" ) then
#	        remove ' and ,
#	       set xx=$x:s/,//
               set xx=`echo $x | tr -d ,`
	       set reverse=`echo $xx | tr -d $qm`
   	else 
	       set reverse=0   
        endif	
#    
	if( $reverse == 0 || $obsplane != 3 ) then
	   set specialjob = 0
	else	
	   set specialjob = 1
      	endif	

	if( $specialjob  == 0 ) then
#          get KEminObs 
          set x=`awk '$1=="KEMINOBS" || $1=="KEminObs" {print $3}' $paramOut/$machine/Skeleton`   
     	  if( "x$x" == "x" ) then
        	  echo  "KEminObs = is missing in skeleton file"
	          exit 1		
          endif
#         remove ' and ,
#	  set xx=$x:s/,//
	  set xx=`echo $x | tr -d ,`
	  set keminobs=`echo $xx | tr -d $qm`
##

#          get Generate
          set x=`awk '$1=="GENERATE" || $1=="Generate" {print $3}' $paramOut/$machine/Skeleton`   
          if( "x$x" == "x" ) then
            echo  "Generate = is missing in skeleton file"
            exit 1	
          endif
#        remove ' and ,
#          set xx=$x:s/,//
	  set xx=`echo $x | tr -d ,`
          set generate=`echo $xx | tr -d $qm`
##
#         get EndLevel
          set x=`awk '$1=="ENDLEVEL" || $1=="EndLevel" {print $3}' $paramOut/$machine/Skeleton`   
          if( "x$x" == "x" ) then
             echo  "EndLevel = is missing"
             exit 1	
          endif
#          remove ' and ,
#          set xx=$x:s/,//
	  set xx=`echo $x | tr -d ,`
          set endlevel=`echo $xx | tr -d $qm`
#         ######
          echo  Generate = $generate, EndLevel = $endlevel, KEminObs = $keminobs
          echo  "**********"
endif
echo  Job = $job is specified. You have to modify some paramers
echo  for the flesh-out.
if( $specialjob == 1 ) then
	echo Reverse= $reverse and ObsPlane=3 were specified.
endif
Reset:
       if( $specialjob == 0 )  then
         echo  "Enter KEminObs value (skeleton value=" $keminobs ")"
         set fkeminobs=$<
         echo $fkeminobs

         echo  "Enter EndLevel (skeleton value="  $endlevel ")"
         set fendlevel=$<
         echo $fendlevel

         echo  "Enter Generarte (skeleton value=" $generate ")"
         set fgenerate=$<
         echo $fgenerate

         echo "********** the values you specified are *********"
        echo KEminObs = $fkeminobs, EndLevel = $fendlevel, Generate = $fgenerate
       else
	 echo "Enter HeightList (in m; SAY, 400e3  OR  500e3,400e3,300e3  ETC--no space after , )"
#             csh dose not need () but tcsh needs it
	 set  fheightlist=($<)        
 	 echo $fheightlist

	 echo "Enter BorderHeithtH(m)"
         set fborderh=$< 
	 echo $fborderh

	 echo "Enter BorderHeithtL(m)"
         set fborderl=$<
         echo $fborderl
   
	echo  HeigthList=$fheightlist, BorderHeightH=$fborderh, BorderHeightL=$fborderl
       endif
       echo "O.K ?,  If not, enter 1"
       set ans=$<
       if ( $ans == 1 ) then
           goto Reset
       endif	
       @ count++
   endif
#      modify the skeleton filen
   if( $specialjob == 0 ) then	
     gawk '$1=="KEMINOBS" || $1=="KEminObs" {print " KEminObs =", keminobs, ","; next }  \
       $1=="ENDLEVEL" || $1=="EndLvel" {print " EndLevel =  ", endlevel, ","; next}  \
       $1=="GENERATE" || $1=="Generate" {print " Generate = ", generate, ","; next} \
       $1=="JOB" || $1=="Job" {print " Job = ", job, ","; next} \
       {print}'  \
       keminobs=$fkeminobs \
       endlevel=$fendlevel \
       generate=$qm$fgenerate$qm \
       job=$qm$job$qm \
       $paramOut/$machine/Skeleton > /tmp/skel$$
   else
#         count number of heightlist and set negative
#        depthlist for the corresponding depth
       @ nh = 0
#              replace , by space in $fheightlist
       set fh=`echo $fheightlist | gawk '{gsub(/,/, " ");print}' `
       set fdepthlist =   
       foreach f($fh)
	@ nh ++
        set fdepthlist = (${fdepthlist}-$f,)
       end
#          make reverse = 0       	
       gawk '$1=="REVERSE" || $1=="Reverse" {print " Reverse = 0,"; next }  \
       $1=="DEPTHLIST" || $1=="DepthList" {print " DepthList = ", depthlist, "0,"; next} \
      $1=="JOB" || $1=="Job" {print " Job = ", job, ","; next} \
      $1=="HeightList" || $1=="HEIGHTLIST" {print " HeightList = ",hlist; next} \
      $1=="BorderHeightH" || $1=="BORDERHEIGHTH" {print " BorderHeightH = ",bhh,","; next} \
      $1=="BorderHeightL" || $1=="BORDERHEIGHTL" {print " BorderHeightL = ",bhl,","; next} \
      {print}'  \
       job=$qm$job$qm \
       depthlist=$fdepthlist \
       hlist=$fheightlist \
       bhh=$fborderh \
       bhl=$fborderl \
       $paramOut/$machine/Skeleton > /tmp/skel$$
endif
#     save Skeleton
	cp  $paramOut/$machine/Skeleton  $paramOut/$machine/SkeletonSave 
#       copy back it to Skeleton
       cp /tmp/skel$$  $paramOut/$machine/Skeleton 
       rm -f /tmp/skel$$

else
   if( $count == 0  && $add == "yes" ) then
	echo "*****************data append is specified in cofing"
        echo "                 but in namelist Cont=f "
        echo "                 Is it O.K ? "
        echo "       If O.K, Enter 1 else anything to stop "
        set ans=$<
        if (  $ans != 1 ) then
             exit 1
        endif
        @ count++
  endif  
#      non cont job; job = ' '  or job= 'skeleton'
#    make initial random numbers;
#    randomly choose 2 integers u,v
   set u=`echo "x" | gawk  '{srand(); print int(rand()*1010105+1); exit}'  `
   set v=`echo "x" | gawk  '{srand(); print int(rand()*7201003+1); exit}'  `
   echo $u, $v are the initial random numbers for host $machine
   mkdir -p  $paramOut/$machine
   if($cutoffin != "") then
      set cutoff=$paramOut/$machine/cutoff
   else	
      set cutoff=""	
   endif
   echo  "cutoff=" $cutoff
  if ( $cutoff == "") then
      gawk '$1=="PrimaryFile" {print " PrimaryFile =", pfile, ","; next }  \
       $1=="InitRN" {print " InitRN =  ", uu, ",", vv ","; next}  \
       $1=="ContFile" {print " ContFile = ", cfile, ","; next} \
       $1=="CutOffFile" {print " CutOffFile = \"", cutoff, "\","; next} \
       $1=="SeedFile"  {print " SeedFile = ", seed, ","; next} \
       $1=="SkeletonFile" {print " SkeletonFile = ", sfile, ","; next} \
       {print}'  \
       pfile=$qm$paramOut/$machine/primary$qm  uu=$u  vv=$v\
       cfile=$qm$paramOut/$machine/ContInfo$qm\
       seed=$qm$datadir/$machine/Seed$qm\
       sfile=$qm$paramOut/$machine/Skeleton$qm\
       $input  > $paramOut/$machine/param
   else	
       gawk '$1=="PrimaryFile" {print " PrimaryFile =", pfile, ","; next }  \
       $1=="InitRN" {print " InitRN =  ", uu, ",", vv ","; next}  \
       $1=="ContFile" {print " ContFile = ", cfile, ","; next} \
       $1=="CutOffFile" {print " CutOffFile = ", cutoff, ","; next} \
       $1=="SeedFile"  {print " SeedFile = ", seed, ","; next} \
       $1=="SkeletonFile" {print " SkeletonFile = ", sfile, ","; next} \
       {print}'  \
       pfile=$qm$paramOut/$machine/primary$qm  uu=$u  vv=$v\
       cfile=$qm$paramOut/$machine/ContInfo$qm\
       cutoff=$qm$cutoff$qm\
       seed=$qm$datadir/$machine/Seed$qm\
       sfile=$qm$paramOut/$machine/Skeleton$qm\
       $input  > $paramOut/$machine/param
     endif
   cp    $primary   $paramOut/$machine/primary
   if( $cutoff != "") then
      cp $cutoffin $paramOut/$machine/cutoff
   endif
#        copy config file 
   cp   $1  $paramOut/$machine/execConfig
endif
#  
#  make a command  for a run and store it in $paramOut/$machine/run
#        to inhibit removing {}
#     ********
    set noglob
#     ********
    set x=`awk '$1=="pipe1" {print}' $1` 
    if ( "x$x" == "x" ) then
	echo pipe1 not exists in $1
	exit 1
    endif
    if ("$x" == "pipe1") then
	set pipe1=
        echo no pipe operation on output
    else 
        set pipe1="$x[2-]"
	echo pipe operation specified is $pipe1
    endif



if ( $add == "yes") then
  echo "mkdir -p  $datadir/$machine;( $progpath < $paramOut/$machine/param   $pipe1  >> $datadir/$machine/$datafile ) >& $paramOut/$machine/error   & " > $paramOut/$machine/run
else
  echo "mkdir -p $datadir/$machine; ( $progpath < $paramOut/$machine/param   $pipe1 >  $datadir/$machine/$datafile ) >& $paramOut/$machine/error   & " >   $paramOut/$machine/run
endif
#      ***********
unset noglob
#      ***********
chmod +x  $paramOut/$machine/run

 echo next is the command to be executed by rsh
 cat $paramOut/$machine/run

#   if interuptted goto file delete-section

onintr delete

echo "Now starting job at $machine"


   rsh $machine  source $paramOut/$machine/run  &
#     kill "source" process at each machine
#       it is no more needed after job has started
   rsh $machine `echo "(cd $cwd; cd ../bin ; ./kesu $1 source)"`
delete:

#            disable double interupt   
	onintr -
	echo "$machine is now running"
	sleep 1
end






