#!/bin/bash

tdata=$1
rec=$2
p=$3
NP=$4		# OBSOLETE
flags="$5"
V=$6	#V=0.01

ti=tmpE$p
tj=tmpE$((p+1))
tmp=/tmp/HERest$$
trap "rm -f $tmp" EXIT

W=2

rm -f $tj/accumulators/HER*.acc
{
for tlist in $ti/trainlist.d/*
do
      k=$(basename $tlist)
      echo HERest -T 1 -u "$flags" -d $ti -C $rec/config -H $tj/acoustics.mmf -p $((k+1)) -m 20 -w $W -L $ti/lab -S $tlist -M $tj/accumulators $tdata/generated_phones
done
} | run_several.py -v +1 3  | tee $tmp || die.sh "HERest terminating" || exit 1
 
set -x
HERest -T 1 -d $tj/ph -C $rec/config -H $tj/acoustics.mmf	\
	  -u "$flags" -p 0 -v $V -L $ti/lab	\
	  -M $tj -s $tj/stats.out $tdata/generated_phones $tj/accumulators/HER*.acc ||
	  die.sh "HERest -p0" || exit 1


echo '#HERest -p0 done'
python bin/watch_HERest.py -coef $tj/watch.av $ti/lab $tdata/mfcc <$tmp > $tj/trainlist || die.sh "watch_HERest.py fails" || exit 1
echo '#watch_HERest.py done'

if test $p -gt 2
then
  bash bin/redo_trainlist.sh $tdata $tj $tj $NP < $tj/trainlist || die.sh "Redo_trainlist.sh fails" || exit 1
fi
