#!/bin/sh

# this simple script verifies the installation
# 
# go to the data directory
#
cd $ISIP_PROTO/data;

err_file="error.text";

# remove the error.text if it already exists
#
if [ -f $err_file ]
then
    rm $err_file;
fi
	
echo "removing data files if they exist"
rm -f $ISIP_PROTO/data/output/*.out 2>/dev/null
rm -f $ISIP_PROTO/data/error.text \
      $ISIP_PROTO/data/tidigit.score.score \
      $ISIP_PROTO/data/tidigit.score.report \
      $ISIP_PROTO/data/models/models.text \
      $ISIP_PROTO/data/models/phones.text \
      $ISIP_PROTO/data/models/loop_grammar.lat \
      $ISIP_PROTO/data/models/init_states.text \
      $ISIP_PROTO/data/models/init_varfloor.text \
      $ISIP_PROTO/data/models/init_transitions.text \
      $ISIP_PROTO/data/models/init_states.bin \
      $ISIP_PROTO/data/models/tri_models.text \
      $ISIP_PROTO/data/models/tri_states.bin \
      $ISIP_PROTO/data/models/2mix_states.text \
      $ISIP_PROTO/data/models/states.bin \
      $ISIP_PROTO/data/models/new_states.text \
      $ISIP_PROTO/data/models/new_trans.text \
      $ISIP_PROTO/data/models/update_transitions.text \
      $ISIP_PROTO/data/models/update_states.text 2>/dev/null

echo "Starting Verification. THIS MIGHT TAKE A FEW MINUTES."

# verify the create_models utility
#
create_models="$ISIP_PROTO/bin/scripts/create_models";
if [ ! -s $create_models ]
then
    echo $create_models not found > $err_file;
fi

# verify the create_triphone_map utility
#
create_triphone_map="$ISIP_PROTO/bin/scripts/create_triphone_map";
if [ ! -s $create_triphone_map ]
then
    echo $create_triphone_map not found > $err_file;
fi

# verify the create_mixtures utility
#
create_mixtures="$ISIP_PROTO/bin/$ISIP_BINARY/create_mixtures";
if [ ! -s $create_mixtures ]
then
    echo $create_mixtures not found > $err_file;
fi

# verify the init_hmm utility
#
init_hmm="$ISIP_PROTO/bin/$ISIP_BINARY/init_hmm";
if [ ! -s $init_hmm ]
then
    echo $init_hmm not found > $err_file;
fi

# verify the init_hmm utility
#
init_triphones="$ISIP_PROTO/bin/$ISIP_BINARY/init_triphones";
if [ ! -s $init_triphones ]
then
    echo $init_triphones not found > $err_file;
fi

# verify the convert_mmf utility
#
convert_mmf="$ISIP_PROTO/bin/$ISIP_BINARY/convert_mmf";
if [ ! -s $convert_mmf ]
then
    echo $convert_mmf not found > $err_file;
fi

# verify the grammar_compiler utility
#
grammar_compiler="$ISIP_PROTO/bin/$ISIP_BINARY/grammar_compiler";
if [ ! -s $grammar_compiler ]
then

    echo $grammar_compiler not found > $err_file;
fi

# verify the decoder utility
#
trace_projector="$ISIP_PROTO/bin/$ISIP_BINARY/trace_projector";
if [ ! -s $trace_projector ]
then
    echo $trace_projector not found >> $err_file;
fi

# verify the tie_state utility
#
tie_state="$ISIP_PROTO/bin/$ISIP_BINARY/tie_state";
if [ ! -s $tie_state ]
then
    echo $tie_state not found >> $err_file;
fi

# verify the hmm_train utility
#
hmm_train="$ISIP_PROTO/bin/$ISIP_BINARY/hmm_train";
if [ ! -s $hmm_train ]
then
    echo $hmm_train not found >> $err_file;
fi

# check for the script
#
isip_eval="$ISIP_PROTO/bin/scripts/isip_eval";
if [ ! -s $isip_eval ]
then
    echo $isip_eval not found >> $err_file;
fi

# check for the training utility
#
bw_train="$ISIP_PROTO/bin/$ISIP_BINARY/bw_train";
if [ ! -s $bw_train ]
then
    echo $bw_train not found >> $err_file;
fi

# if there are errors, then cancel the run
#
if [ -s $err_file ]
then
    echo " "
    echo "Errors:  Executable files not created by install."
    more $err_file;
    exit;
fi

#
#  Programs all exist.  Now run them.
#

# make output directory
#
echo "making output directory"
mkdir $ISIP_PROTO/data/output 2>/dev/null

# run create_models
#
echo "Verifying create_models";
$create_models -states models/model_lengths.text -output models/models.text

# diff the models file
#
diff models/models.text ref/models.text >> $err_file

# run create_triphone_map
#
echo "Verifying create_triphone_map";
$create_triphone_map -mono models/monophones.text -clist models/monophones.text -context ci -models models/models.text -output models/phones.text

# diff the phones file
#
diff models/phones.text ref/phones.text >> $err_file

# run the grammar_compiler
# 
echo "Verifying grammar_compiler";
$grammar_compiler -input ref/loop_grammar.text -output models/loop_grammar.lat

# diff the loop grammar file
#
diff models/loop_grammar.lat ref/loop_grammar.lat >> $err_file

# run the init_hmm utility
#
echo "Verifying init_hmm";
$init_hmm -input models/mfc.list -models models/model_lengths.text -trans models/init_transitions.text -state models/init_states.text -mode ascii -vfloor_file models/init_varfloor.text -var_floor 0.0002

# convert the states file which is in ascii format into binary format
# run the conversion utility
#
echo "Verifying convert_mmf";
$convert_mmf -input_mode ascii -output_mode binary models/init_states.text models/init_states.bin

# run the init_triphones utility
#
echo "Verifying init_triphones";
$init_triphones -param params/params_init_triphones.text

# diff the tri_models file
#
diff models/tri_models.text ref/tri_models.text >> $err_file

# run create_mixtures utility
#
echo "Verifying create_mixtures";
$create_mixtures -input models/init_states.bin -output models/2mix_states.text -num_mixtures 2

# convert the states file which is in ascii format into binary format
# run the conversion utility
#
$convert_mmf -input_mode ascii -output_mode binary models/states.text models/states.bin

# run the viterbi trainer
#
echo "Verifying hmm_train";
$hmm_train -p params/params_hmm_train.text -c CI

# run trace projector
#
echo "Verifying trace_projector";
$trace_projector -p params/params_decode.text;

# run evaluation
#
$isip_eval isip_word file.list models/ref.score tidigit.score

# run a single pass of training and compare the states and transition files
#
echo "Verifying bw_train";
$bw_train -p params/params_train.text;

# compare the scores and report the difference
#
diff tidigit.score.report models/tidigit.score.report >> $err_file

echo " "

if [ -s $err_file ]
then
    cat $err_file;
    echo 'Error(s) Occured. Improper installation.';
    echo 'Check $err_file for discrepancies.';
else
    echo 'Installation Verified.';
    echo 'You can download the TIDIGITS for further verification.';
fi
