#!/bin/sh

set -x

NP=4
NPR=$(($NP+$NP/2))
SIZE=2.2e12
MINVAR=0.003

rec=$1
mfccname=$2
#rec is a directory containing initial specification of stuff for
configfile=${mfccname}
mapping=$3
#rec is a directory containing initial specification of stuff for
# a particular try at building a HMM.

TrainingFiat=/proj/aesop/proc/metadata/*.fiat
tdata=/tmp/recf2_all


rm -f tmpE0
ti=tmpE0
ln -s tmpR $ti

for flags in m m mvw mw mvw mvw mvw mvtw mvw mvtw mvw \
	mvtw mvw mvtw mvw mvtw mvw mvtw mvw mvtw
do
  p=${ti#tmpE}
  tj=tmpE$(($p+1))
  mv $tj $tj.junk
  rm -rf $tj.junk &
  mkdir $tj $tj/ph
  cp -p $ti/* $tj ; cp -p $ti/ph/* $tj/ph

  mv $tdata $tdata.junk && { rm -rf $tdata.junk & }
  mkdir -p $tdata $tdata/mlf
  python bin/generate_for_HERest.py $configfile $tdata $((p+1)) $mapping $TrainingFiat || die.sh "Generate fails" || exit 1

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

  NSPL=$( cd $ti/ph >/dev/null 2>&1 ; wc -c $(cat phones) | awk '/total$/ {print int(size/($1*$1*np));}' np=$NP size=$SIZE - )
  echo 'NSPL=' $NSPL 'NP=' $NP

  {
  for f in $(cat $tdata/trainlist)
  do
    tmp1=${f#$tdata/mfcc/}
    tmp=${tmp1%.mfcc}
    echo "HParse -A -T 1 -C $tj/config -D $tdata/gram/$tmp.grammar $tdata/gram/$tmp.slf && HVite -A -D -T 1 -C $tj/config -d $tj/ph -i $tdata/mlf/$tmp.mlf -H $tj/ph/macros -w $tdata/gram/$tmp.slf $tdata/dict $tj/ph/phones $f && bash bin/MLF_to_lab.sh $tdata/mlf/$tmp.mlf $tdata/lab/$tmp.lab"
  done
  } | bash bin/run_several.sh /tmp/$$/stdouts $NP 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 $tdata/trainlist.*
  ls $tdata/lab | sed -e 's/[.]lab$/.mfcc/' | awk '{print td "/mfcc/" $1;}' td=$tdata - | python bin/split_list.py -np $NP $NSPL "$tdata/trainlist.%d"

  echo 'tdata=' $tdata
  echo 'trainlist=' $tdata/trainlist.*
  mkdir $tj/accumulators
  {
  for tlist in $tdata/trainlist.*
  do
    k=${tlist#$tdata/trainlist.}
    echo HERest -T 1 -u "$flags" -d $tj/ph -C $tj/config -H $tj/ph/macros -p $((k+1)) -L $tdata/lab -S $tlist -M $tj/accumulators $tj/ph/phones
  done
  } | bash bin/run_several.sh /tmp/$$/stdouts $NP 3 || die.sh "HERest terminating" || exit 1

  HERest -T 1 -d $tj/ph -C $tj/config -H $tj/ph/macros	\
	-u "$flags" -p 0 -L $tdata/lab	\
	-M $tj/ph -s $tj/stats.out $tj/ph/phones $tj/accumulators/HER*.acc ||
	die.sh "HERest -p0" || exit 1
  ti=$tj
done

rm -f tmpER
ln -s $tj tmpER

{
python -c 'import sys; sys.stdout.write("$phone = " + " | ".join([q.strip() for q in sys.stdin.readlines()]) + " ;\n")' <$tj/ph/phones
cat $tj/gram
} >$tj/Gram

HParse -T 1 -A -C $tj/config -D $tj/Gram $tj/net.slf || die.sh "HParse" || exit 1


mkdir $tj/reco
# Recognize the training set, just to make sure that everything is working
# properly:
{
for f in $(select_fiat_entries.py -show fncore $tdata/metadata.fiat)
do
  echo HVite -A -D -T 1 -C $tj/config -d $tj/ph -i $tj/reco/$f.mlf -H $tj/ph/macros -w $tj/net.slf $tdata/dict_final $tj/ph/phones $tdata/mfcc/$f.mfcc
done
} | bash bin/run_several.sh /tmp/$$/stdouts $NPR 3 || die.sh "HVite" || exit 1

python bin/convert_back_fa.py ${rec}_$mfccname $tj/reco $tdata/metadata.fiat
