#! @PERL@
#
# sample usage: index_state.pl input_states.text output_states.text 
#
# this script indexes the states 
#

# define constants
#
$NUM_ARGS = 1;
$STATES_FLAG = "\~s";
$STATE_STR = "State\: ";
$STATE_FEATURE_SIZE_STR = "feature_size = ";
$STATE_NUM_STATES_STR = "num_states = ";
$STATE_NUM_MIXES_STR = "nummixes\: ";
$STATE_NUMMIXES = "\<NUMMIXES\>";
$STATE_MIXTURE_STR = "mixture\: ";
$STATE_MIX_WEIGHT_STR = "mix_weight\: ";
$STATE_MEAN_STR = "mean\: ";
$STATE_COVAR_STR = "covar\: ";
$STATE_SCALE_STR = "scale\: ";
$DEF_NUM_FEATURES = 39;

# define local variables
#
# trans_ind starts with 1 because transitions index for S! and !S are set to
# 0 by default in the  decoder
# states_ind also starts with 1 for similar reasons
#
$states_ind = 0;
$num_states = 1;

# check for correct number of arguments
#
($#ARGV == ($NUM_ARGS)) || 
    die "Usage: index_state.pl <input states> <output states>\n";

$in_states = $ARGV[0];
$out_states = $ARGV[1];

# open files
# 
open (in_states_file, "$in_states") || die "Cannot open file $in_states: $!\n";
open (out_states_file, ">$out_states") || die "Cannot open file $out_states: $!\n";


# read the input states file and find the number of states
#
while (<in_states_file>) {
    
    chop;
    $line = $_;

    if ($line =~ $STATES_FLAG) {
	$num_states++;
    }
}
close (in_states_file);
open (in_states_file, "$in_states") || die "Cannot open file $in_states: $!\n";
print out_states_file "$STATE_FEATURE_SIZE_STR $DEF_NUM_FEATURES\n\n";
print out_states_file "$STATE_NUM_STATES_STR $num_states\n\n";

# write the dummy state used as start and stop states in models
#
print out_states_file "$STATE_STR $states_ind\n";
print out_states_file "$STATE_NUM_MIXES_STR 0\n\n";
$states_ind++;

# read the transitions file and index the transitions
#
while (!eof(in_states_file)) {

    $line = <in_states_file>;
    
    if ($line =~ $STATES_FLAG) {
	
	# write to the states file
	#
	print out_states_file "$STATE_STR $states_ind\n";
	$states_ind++;
	
	$line = <in_states_file>;

	# if state has multiple mixtures
	#
	if ($line =~ /$STATE_NUMMIXES/) {

	    @num_mix = split (/\s+/, $line);
	
	    print out_states_file "$STATE_NUM_MIXES_STR $num_mix[1]\n\n";
	    
	    # read in each mixture and output to the states file
	    #
	    for ($i = 1; $i <= $num_mix[1]; $i++) {
		print out_states_file "$STATE_MIXTURE_STR $i\n";
		$line = <in_states_file>;
		@temp = split (/\s+/,$line);
		print out_states_file "$STATE_MIX_WEIGHT_STR $temp[2]\n";
		$line = <in_states_file>;
		$line = <in_states_file>;
		print out_states_file "$STATE_MEAN_STR $line";
		$line = <in_states_file>;
		$line = <in_states_file>;
		print out_states_file "$STATE_COVAR_STR $line";
		$line = <in_states_file>;
		@temp = split (/\s+/,$line);
		print out_states_file "$STATE_SCALE_STR $temp[1]\n\n";
	    }	
	}

	# if single mixture state
	#
	else {
	    print out_states_file "$STATE_NUM_MIXES_STR 1\n\n";
	    print out_states_file "$STATE_MIXTURE_STR 1\n";
	    print out_states_file "$STATE_MIX_WEIGHT_STR 1.000000e+00\n";
	    $line = <in_states_file>;
	    print out_states_file "$STATE_MEAN_STR $line";
	    $line = <in_states_file>;
	    $line = <in_states_file>;
	    print out_states_file "$STATE_COVAR_STR $line";
	    $line = <in_states_file>;
	    @temp = split (/\s+/,$line);
	    print out_states_file "$STATE_SCALE_STR $temp[1]\n\n";
	}
    }
}


close(in_states_file);
close(out_states_file);

# 
# end of script



