#!/bin/sh
 
# file: $ISIP_PROTO/bin/scripts/convert_models.sh
#
# this script takes three arguments:
#
#       convert_models.sh output_mode input_mmf states_file models_file 
#       trans_file 
#
# the arguments are:
#
#	output_mode: mode for states file (ascii/bin)	
#       input_mmf: htk's ascii mmf file
#       states_file: output states file in isip format    
#       models_file: output models file in isip format    
#       trans_file: output transitions file in isip format    
#
# this script converts the HTK ascii mmf file into isip format mmf
#

# important definitions
#
PROGRAM_NAME="convert_models";

# define the help file location
#
ISIP_HELP_FILE="$ISIP_PROTO/doc/convert_models.help";
export ISIP_HELP_FILE;

# execute a generic help function:
# force the shellscript to exit if a help option is encountered.
set -e;
$ISIP_PROTO/bin/scripts/isip_function_help $*;
set +e;

# define constants
#
BIN_PATH="$ISIP_PROTO/bin/$ISIP_BINARY";
SCRIPTS_PATH="$ISIP_PROTO/bin/scripts";
SPLIT_MMF_PROG="$SCRIPTS_PATH/split_mmf";
INDEX_STATES_PROG="$SCRIPTS_PATH/index_states";
INDEX_MODELS_PROG="$SCRIPTS_PATH/index_models";
INDEX_TRANS_PROG="$SCRIPTS_PATH/index_trans";
CONVERT_MMF_PROG="convert_mmf";
ASCII_MODE="ascii";
BIN_MODE="binary";
NUM_ARGS="5";

# define current directory
#
isip_pwd=`pwd`;

# check arguments
#
if (test "$#" -ne "$NUM_ARGS") then
    echo "\t$0> incorrect number of arguments ($# $NUM_ARGS)";
    echo "\t$0> incorrect number of arguments ($# $NUM_ARGS)";
    exit 0;
fi

# get output mode for states file
#
mode="$1";
if (test "$mode" = "$ASCII_MODE") then
    if (test "$mode" = "$BIN_MODE") then
        echo "\t$0> output mode should be \"ascii\" or \"binary\"";
	exit 0;
    fi
fi

# check if the input mmf file exists
#
input_mmf_file="$2";
if (test ! -f "$input_mmf_file") then
    echo "\t$0> error opening $input_mmf_file";
    exit 0;
fi

# set the output file names
#
out_states_file="$3";
out_models_file="$4";
out_trans_file="$5";

# run the necessary scripts to convert the ascii htk mmf to isip format
#
temp_trans_file="$isip_pwd/conv_mmf_trans_$$.tmp";
temp_models_file="$isip_pwd/conv_mmf_models_$$.tmp";
temp_states_file="$isip_pwd/conv_mmf_states_$$.tmp";

# split the htk ascii mmf to temporary models, states and transitions 
# ascii files
#
$SPLIT_MMF_PROG $input_mmf_file $temp_states_file $temp_trans_file \
    $temp_models_file;

# index the models file and write to isip format models
#
$INDEX_MODELS_PROG $temp_states_file $temp_trans_file \
    $temp_models_file $out_models_file;

# index the states file and write to isip format states
#
$INDEX_STATES_PROG $temp_states_file $out_states_file;

# index the transitions file and write to isip format transitions
#
$INDEX_TRANS_PROG $temp_trans_file $out_trans_file;

# if binary output required convert the states file to binary using
# specified routine
#
if (test "$mode" = "$BIN_MODE") then
    
    # run the conversion routine
    #
    $CONVERT_MMF_PROG -input_mode "$ASCII_MODE" -output_mode "$BIN_MODE" \
	"$out_states_file" "$temp_states_file";
    mv "$temp_states_file" "$out_states_file";		
fi

# delete all temp files if they exist
#
if (test -f "$temp_trans_file") then
    rm "$temp_trans_file";
fi

if (test -f "$temp_states_file") then
    rm "$temp_states_file";
fi

if (test -f "$temp_models_file") then
    rm "$temp_models_file";
fi

# end of file
#



