#! @PERL@

$ISIP_HELP_FILE = <<__ISIP_HELP_HERE_FILE__;
name: create_models
synopsis: create_models -states <states list> -output <models list> 

descr:  this script takes as input the file defining the number of non-dummy 
        states in each model along with the model name and outputs a model 
        definition file. the input file is a series of number of states and 
        model name pairs, for example:

        4 _ax
        7 _v_ih_n
        .....
        .....

example: create_models -states states.list -output syllable_models.text

options: 
 -states: file listing the number of states in model and model name
 -output: model definition file

arguments:
 states.list: file defining number of states in each model
 models.text: model definition file

man page: none
__ISIP_HELP_HERE_FILE__

# define the program name
#
@tmp = split(/\//, $0);
$ISIP_PROG = pop(@tmp);

# define command line options
#
$CM_OPT_OUT = "-output";
$CM_OPT_STATES = "-states";

# load the command_line source
#
require "$ENV{ISIP_PROTO}/lib/scripts/perl/command_line.pm";

# parse the command line arguments
#
my ($stateslist, $output) = command_line(0, 
					 $CM_OPT_STATES, 1,
					 $CM_OPT_OUT, 1);

# error checking of command line arguments
#
if ($stateslist eq "") {
    isip_die("states file not specified on the command line.\n\ttry the -help option.");
}

if ($output eq "") {
    isip_die("output file not specified on the command line.\n\ttry the -help option.");
}

# open files
# 
open (states_file, "$stateslist") || die "Cannot open file $stateslist\n";
open (output_file, ">$output") || die "Cannot open file $output:\n";

# define local variables
#
$zero_count = 0;
$state_count = 1;
$count = 0;

# open file for line count
#
while (<states_file>) {
    $count++;
}
close (states_file);

# print the total number of models
#
print output_file "num_models = $count\n\n";
$count = 0;

# reopen file for processing
#
open (states_file, "$stateslist") || die "Cannot open file $stateslist\n";
while (<states_file>) {

    chop;
    @line  = split(/\s+/, $_);
    $num_states=$line[0];
    $phone=$line[1];
    
    # print index
    #
    print output_file "index: $count\n";
    
    # print phone
    #
    print output_file "phone: $phone\n";
    
    # print num_states
    #
    $num_states_to_print = $num_states+2;
    print output_file "num_states: $num_states_to_print\n";

    # if not a dummy model
    #
    if ($num_states != 0) {

	# print transitions
	#
	$transition = $count - $zero_count + 1;
	print output_file "transitions: $transition\n";
	
	# print states in the serial order
	#
	$first_state = $state_count;
	print output_file "states: 0 $first_state ";
	for ($i = 1; $i < $num_states; $i++) {
	    $second_state = $first_state + $i;
	    print output_file "$second_state ";
	}
	print output_file "0\n\n";    
	$state_count += $num_states;
    }
    
    # num states is set to zero treat all such models as a dummy model with
    # a dummy transition matrix and state list
    #
    else {
	print output_file "transitions: 0\n";
	print output_file "states: 0 0\n\n";
	$zero_count++;
    }
    $count++;
}

# 
# end of script
