#!/bin/bash

set -x

tdata=$1
rec=$2
ti=$3
mfccname=$4
mapname=$5
shift; shift; shift
shift; shift
MetaData=$*

# rm -f $ti; rm -rf $ti
bin/mkscratch.sh $ti
mkdir $ti/ph
cp -p $rec/* $ti

# mv $tdata $tdata.junk && { ionice -c2 -n7 rm -rf $tdata.junk & }
# mkdir $tdata
# ( cd $tdata; ln -s ../$rec/coding; )
bin/mkscratch.sh $tdata
cp -p $rec/coding $tdata

mkdir $tdata/lab $tdata/mfcc
python bin/generate_Vy_HRest.py $mfccname $tdata $rec $mapname $MetaData ||
	die.sh "Generate fails" || exit 1
python bin/basename.py .mfcc <$tdata/trainlist > $ti/trainlist
mv $tdata/lab $ti/lab

python bin/writeHMM.py -p "$(cat $tdata/generated_phones)" $ti/ph <$rec/pmodels.txt || die.sh "writeHMM.py fails." || exit 1

awk '{print tdata "/mfcc/" $1 ".mfcc"; }' tdata=$tdata $ti/trainlist >$ti/trainlist.tmp

tlist=$ti/trainlist.tmp
for phn in $(cat $tdata/generated_phones)
do
  HCompV  -A -D -T 1 -C $rec/config -m -D -L $ti/lab -H $ti/ph/macros -M $ti/ph	\
  	-S $tlist -l $phn $ti/ph/$phn
done
wait
