#!/bin/bash
# 
# updated on May 14, 2008 for the config file based itg trainer
# March 22, 2005
# Hao Zhang (zhanghao AT cs DOT rochester DOT edu)

#$1: run id
#$2: L1 corpus, full path
#$3: L2 corpus, full path
#$4: machinelist
#$5: prog name
#$6: intial probalilities
#$7: grammar rules
#$8: the iteration to start from
#$9: the iteration to stop at
#$10: the destination full path 
#$11: the config file for the itg trainer
##$10: the pair probabilities for ttt pruning

#directories
base_dir="/u/zhanghao/nlp/mdpitg/src/itg-nnew/"
binpath=$base_dir"bin/"
datpath=$base_dir"Test/"
dest=${10}

config=$datpath${11}

#untility, you intput 1, it outputs a, <=26
nthc=$binpath"nthc"

#identifier
runidpref=$dest$1
idlist=$dest"idlist"

#training corpra
emaincorp=$2
cmaincorp=$3
ectotallen=$(cat $emaincorp|wc -l) #should be the same as c..

#machines to run on
mlist=$datpath$4

cmd=$binpath$5
defprb=$datpath$6
grmr=$datpath$7

si=$8 #start from 
ni=${9} #stop at this iteration

#pairprb=$datpath${10}

if [ -e $dest ]
then
	echo "$dest existing"
else
mkdir $dest
fi

for ((itr=si; itr<=ni; itr++))
do
	echo "ITERATION $itr "

	if [ $itr -gt 1 ]
	then
		let iitr=itr-1
		updprb=$runidpref"__"$iitr".t.prb" #updated rule (phrase) probabilities

                updgrmr=$runidpref"__"$iitr".rules.prb"  #updated t-table
	else
                updgrmr=$grmr
		updprb=$defprb
	fi

	n_m=0

	if [ -e $idlist ]
	then 
		rm $idlist
	fi

	totalchunks=$(cat $mlist|wc -l)
	echo "total chunks $totalchunks"

	let chunksize=ectotallen/totalchunks 
	let evensize=chunksize*totalchunks

	if [ $evensize -lt $ectotallen ]
	then
		let chunksize=chunksize+1
	fi

	echo "chunksize $chunksize"

	split -l$chunksize <(cat $emaincorp) $dest"ecorp"
	split -l$chunksize <(cat $cmaincorp) $dest"ccorp"

        let acc=0;
	for m in $(cat $mlist)
	do
                if [ $acc -lt $ectotallen ]
                then 
  		  let n_m=n_m+1
		  epart=$dest"ecorpa"$($nthc $n_m)
		  cpart=$dest"ccorpa"$($nthc $n_m)
		  id=$1"_"$n_m

		  echo $dest$id".cnt" >>$idlist

                #  echo "ssh -n $m $cmd -configfile $config -grammarfile $updgrmr -l1corpus $epart -l2corpus $cpart -iterations 1 -rundir $dest -runid $id -probfile $updprb" 
                  ssh -n $m "$cmd -configfile $config -grammarfile $updgrmr -l1corpus $epart -l2corpus $cpart -iterations 1 -rundir $dest -runid $id -probfile $updprb" &
	  	  echo "started on $m"
                fi
                let acc=acc+chunksize;
	done

	nj=$(ps ax|grep $id|grep -v grep|wc -l)
	#echo "jobs total $nj"
	while [ $nj -ne 0 ]
	do
		sleep 1
		#echo POLLING
		nj=$(ps ax|grep $id|grep -v grep|wc -l)
	done
	sleep 2

	echo $binpath"hub" -configfile $config -grammarfile $updggrmr -rundir $dest -runid $runidpref"__"$itr -countfilelist $idlist 
        $binpath"hub" -configfile $config -grammarfile $updgrmr -rundir $dest -runid $dest$1"__"$itr -probfile $updprb -countfilelist $idlist 

        grep likelihood $dest*".log" |awk '{sum+=$4}END{printf("log likelihood: %.20e\n", sum);}'
	echo "ITERATION $itr DONE"

done
