#!/usr/local/bin/tcsh 
#  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 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 primary=`echo $xx | tr -d $qm`
    echo Primary is given in $primary. Its copy will be used.

#       get Cont
    set x=`awk '$1=="Cont" {print $3}' $input`   
    if( "x$x" == "x" ) then
       echo  "Cont = is missing"
       exit 1	
    endif
#      remove ' and ,
     echo "x=$x"
    set xx=$x:s/,//
    set cont=`echo $xx | tr -d $qm`
     
#     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/,//
    set cutoff=`echo $xx | tr -d $qm`
    echo CutOffFile is given in $cutoff. Its copy will be used.

#       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 job=`echo $xx | tr -d $qm`
##
#       get KEminObs 
    set x=`awk '$1=="KEminObs" {print $3}' $input`   
    if( "x$x" == "x" ) then
       echo  "KEminObs = is missing"
       exit 1	
    endif
#      remove ' and ,
    set xx=$x:s/,//
    set keminobs=`echo $xx | tr -d $qm`
##
#       get Generate
    set x=`awk '$1=="Generate" {print $3}' $input`   
    if( "x$x" == "x" ) then
       echo  "Generate = is missing"
       exit 1	
    endif
#      remove ' and ,
    set xx=$x:s/,//
    set generate=`echo $xx | tr -d $qm`
##
#       get EndLevel
    set x=`awk '$1=="EndLevel" {print $3}' $input`   
    if( "x$x" == "x" ) then
       echo  "EndLevel = is missing"
       exit 1	
    endif
#      remove ' and ,
    set xx=$x:s/,//
    set endlevel=`echo $xx | tr -d $qm`

#######
    echo Job = $job, Generate = $generate, EndLevel = $endlevel, KEminObs = $keminobs
#######
#  if Job == 'skeleton',  set parameters for Job=='flseh'
    if( ($cont=="t" || $cont=="T") &&  $job == 'skeleton') then
	echo  Job = $job is specified. You have to fix the conditions
        echo  "to be used for Job = 'flesh'"
Reset:
	echo  "Enter KEminObs value (current value=" $keminobs ")"
	set keminobs=$<
	echo $keminobs

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

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

	echo "********** the values you specified are *********"
        echo KEminObs = $keminobs, EndLevel = $endlevel, Generate = $generate
	echo "O.K ?,  If not, enter 1"
	set ans=$<
	if ( $ans == 1) goto Reset
    endif	 
exit
#
#     reform param for each host:
#   reset random number seed
#   reset PrimaryFile 
#   reset ContFile
#   reset SeedFile
#   reset SkeltonFile
#
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	

#******************** this part must be dependent on the  
#******************** parameter file format
#    make initial random numbers
#    randomly choose an integer 
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

set primary=$primary:t
if($cutoff != "") then
  set cutoff=$paramOut/$machine/$cutoff
endif

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$paramOut/$machine/Seed$qm, \
        sfile=$qm$paramOut/$machine/Skeleton$qm, \
        $input  > $paramOut/$machine/param
######
####
cp    $primary   $paramOut/$machine/primary
if( $cutoff != "") then
	cp $cutoff $
if($cutoff == "need") then
  cp    $CutOffFile $paramOut/$machine/Cutoff
endif
#        copy config file 
cp   $1  $paramOut/$machine/execConfig
#  
#  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/input   $pipe1  >> $datadir/$machine/$datafile ) >& $paramOut/$machine/error   & " > $paramOut/$machine/run
else
  echo "mkdir -p $datadir/$machine; ( $progpath < $paramOut/$machine/input   $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






