#!/bin/bash

set -x

tdata=$1
rec=$2
p=$3
NP=$4
prune=$5

ti=tmpE$p
tj=tmpE$(($p+1))

rm -rf $tj/mlf $tj/lab tmpM
mkdir $tj/mlf $tj/lab tmpM
{
for f in $(cat $ti/trainlist.d/*)
do
	tmp=$(basename $f .mfcc)
	echo "HParse -A -T 1 -C $rec/config -D $tdata/gram/$tmp.grammar $tdata/gram/$tmp.slf && HVite -A -D -T 1 -C $rec/config -t $prune -d $ti -i $tj/mlf/$tmp.mlf -H $ti/acoustics.mmf -w $tdata/gram/$tmp.slf $tdata/dict $tdata/generated_phones $f && python bin/MLF_to_lab.py $tj/mlf/$tmp.mlf $tj $tmp $tdata || echo Cannot HVite on $tmp - skipping"
done
} | run_several.py -v +1 3 || die.sh "HParse & HVite terminating" || exit 1

# Now we make a new training list that omits those files that didn't work out on the previous
# HParse/HVite/MLF_to_lab.sh pass
rm -f $tj/trainlist.*
ls $tj/lab | python bin/basename.py .lab > $tj/trainlist
bash bin/redo_trainlist.sh $tdata $tj $tj $NP < $tj/trainlist || die.sh "Redo_trainlist.sh fails" || exit 1
