#!/bin/sh

# This runs HTK to do a simple segmentation of training data,
# where training data is human-segmented.  It uses HRest to do
# the training.

set -x

date >>LOG
echo ARGS: $0 $* >>LOG


tdata=training
rec=$1
#rec is a directory containing initial specification of stuff for
# a particular try at building a HMM.
NP=4
mfccname=$2
mapname=$3
shift; shift; shift
MetaData=$*
V=0.01

ti=tmpE0
bash bin/initialize_HRest.sh $tdata $rec $ti $mfccname $mapname $MetaData ||
	die.sh "Initialize.sh fails" || exit 1


phones=$(cat $tdata/generated_phones)

for flags in "twmv"
do
  p=${ti#tmpE};
  tj=tmpE$(($p+1))
  # mv $tj $tj.junk && { rm -rf $tj.junk & }
  bin/mkscratch.sh $tj
  cp -p $ti/* $tj
  cp -r -p $ti/ph $tj
  cp -r -p -l $ti/lab $tj

  if test $flags = "e"
  then
    exit 1
    rm -f $tj/ph/*
    HHEd -d $ti/ph -H $ti/ph/macros -M $tj/ph $tj/cmds.hed $tdata/generated_phones ||
     	die.sh "HHed fails." || exit 1
  else
    bash bin/HRest_script.sh $tdata $rec $p "$flags" $V ||
	die.sh "HERest_script.sh fails." || exit 1
  fi
  ti=$tj
done

rm -f tmpR ; rm -rf tmpR
cp -r -p $tj tmpR
mkdir tmpR/reco

(
cd $tj/ph;
cat macros $phones >hmmsdef.mmf
)

# bash bin/recognize_training_set.sh

HParse -T 1 -A -C $rec/config -D $rec/gram tmpR/net.slf || die.sh "HParse" || exit 1

# OK.  Now we have the HMM optimized.
# tr ' ' '\012' < $tdata/generated_phones >tmpR/hmmlist.txt
cp $tdata/generated_phones tmpR/hmmlist.txt

